APEX-Q05Trade-off AdvancedApex & triggers#ldv#apex#async-apex

Batch Apex or Apex Cursors with chained Queueables for processing 20 million records?

Your confidence:Not rated

Model answer

Batch ApexCursor + chained Queueable
VolumeQueryLocator up to 50 million rowsCursor over up to 50 million rows
ConcurrencyOnly 5 active batch jobs; others wait in the flex queueDoesn't use batch slots
ChunkingFixed scope (โ‰ค 2,000) per executeYou choose how many rows each step fetches
State and end stepDatabase.Stateful, finishYou carry state in the job; final step is your code
Error handlingBatchApexErrorEvent, failed chunks reportedFinalizers per step
MonitoringFamiliar Apex Jobs view per batchOne job record per chain step
QuotasAsync execution limitsPlus 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.