Batch Apex or Apex Cursors with chained Queueables for processing 20 million records?
Model answer
| Batch Apex | Cursor + chained Queueable | |
|---|---|---|
| Volume | QueryLocator up to 50 million rows | Cursor over up to 50 million rows |
| Concurrency | Only 5 active batch jobs; others wait in the flex queue | Doesn't use batch slots |
| Chunking | Fixed scope (โค 2,000) per execute | You choose how many rows each step fetches |
| State and end step | Database.Stateful, finish | You carry state in the job; final step is your code |
| Error handling | BatchApexErrorEvent, failed chunks reported | Finalizers per step |
| Monitoring | Familiar Apex Jobs view per batch | One job record per chain step |
| Quotas | Async execution limits | Plus daily cursor instance and row quotas |
My choice: for a nightly maintenance job in an org with few batch jobs, Batch Apex is simpler and the team knows it. When batch slots are contended (many packages and jobs), when processing must start immediately from user actions, or when chunk size should adapt, Cursor + Queueable is attractive.
Either way: a selective start query, idempotent processing, error capture with reruns of failed IDs, and a runtime test with production-like volume in a full sandbox.
โ Key points to hit
โ Red-flag answers
- Iterating 20 million records in one transaction
- Ignoring concurrency or quotas
- No plan for failed records
โช Follow-ups
Where does the cursor live between queueable steps?
As an instance variable on the queueable. It's serialised with the job, so each step reuses the same cursor and position instead of creating a new cursor.
What else would you consider before writing Apex for 20 million records?
Whether the work belongs on-platform at all: Bulk API with an ETL tool, Data 360 transforms, or processing in a data warehouse may be cheaper and faster for pure data changes.