Skip to main content

SF-0374 · Scenario · Hard

Do the batches of records execute in the order they are received from the start method?

✓ Verified by Vikas Singhal · Last reviewed 5/17/2026 · Updated for Spring '26

No. Salesforce makes no guarantee that batch chunks execute in the order returned by start. Chunks may run in parallel, complete out of order, and even retry — your execute method must not assume any sequencing.

What the docs explicitly say

From the Apex Developer Guide:

“Batches of records tend to execute in the order in which they’re received from the start method. However, the order in which batches of records execute depends on various factors. The order of execution isn’t guaranteed.”

Translation: never depend on order.

What can cause out-of-order execution

CauseEffect
Apex Flex Queue prioritizationHigher-priority jobs jump ahead
Platform parallel workersMultiple chunks of the same job can run concurrently
Retries after chunk failureThe retry slot may run after newer chunks
Apex Job server distributionChunks dispatched to different processing nodes

Why this matters in practice

Code that looks correct but breaks under out-of-order execution:

// BAD — assumes chunks run in order
public void execute(Database.BatchableContext ctx, List<Opportunity> scope) {
    for (Opportunity o : scope) {
        // We want to set Sequence_Number__c to a running counter
        // starting from 1 in chunk 1
        o.Sequence_Number__c = nextNumber++;
    }
    update scope;
}

If chunk 5 runs before chunk 3, the sequence numbers are wrong. Every assumption of “chunks run sequentially” breaks here.

What you can rely on

GuaranteeStatus
start runs onceYes
execute runs once per chunkYes
finish runs after all chunks completeYes
All records returned by start get processedYes
Chunks run sequentiallyNo
Chunk order matches start query’s ORDER BYOften true, but not guaranteed
All chunks see the same database snapshotNo (each chunk re-queries)

How to write order-independent code

  1. Don’t share state across chunks unless you use Database.Stateful and don’t depend on the order of updates.
  2. Compute relative values from the record itself, not from a running counter.
  3. Idempotent operations — running a chunk twice should produce the same result as running it once.
  4. External numbering — if you really need sequence numbers, generate them in a single sync step before batching.

The exception: Database.Stateful with care

Database.Stateful preserves member variables across chunks, but does not guarantee chunk order. So this still has the bug above:

public class StatefulCounter implements Database.Batchable<sObject>, Database.Stateful {
    private Integer counter = 1; // survives, but assigned non-deterministically
    // ...
}

State persists; sequence doesn’t.

Common interview follow-ups

  • Does ORDER BY in start guarantee chunk order? — It guarantees the records within a chunk are ordered. Across chunks, you can’t depend on it.
  • Are chunks ever truly parallel? — Sometimes, yes — Salesforce can run multiple chunks of the same job concurrently in production.
  • How do I unit-test for out-of-order behavior? — Hard. Most teams test the contract (every record gets processed) rather than the order.

Verified against: Apex Developer Guide — Using Batch Apex. Last reviewed 2026-05-17.