Sub topic 2
What changes in async: limits and org-wide caps
This chapter assumes you know basic Apex and SOQL, the synchronous transaction model, and governor limits at a high level. It covers two kinds of limits. Per-transaction limits change when code runs asynchronously. Org-wide caps apply to the platform as a whole, and every async tool counts against them.
Per-transaction limits are higher
Asynchronous Apex still runs inside a transaction with governor limits. Salesforce raises several of them because no user is waiting on the result.
| Limit | Synchronous | Asynchronous |
|---|---|---|
| SOQL queries | 100 | 200 |
| Heap size | 6 MB | 12 MB |
| CPU time | 10,000 ms | 60,000 ms |
| DML statements | 150 | 150 |
| Records returned by SOQL | 50,000 | 50,000 |
| Callouts | 100 | 100 |
Query count, heap, and CPU time get more room. DML statements and row counts stay the same. Async code does not avoid "too many DML rows" or "too many query rows" errors. Large volumes need Batch Apex, which gives each chunk of records its own transaction and its own set of limits.
You can confirm the limits from inside a running job with the Limits class.
public class LimitProbe implements Queueable {
public void execute(QueueableContext ctx) {
System.debug('SOQL: ' + Limits.getLimitQueries());
System.debug('Heap: ' + Limits.getLimitHeapSize());
System.debug('CPU: ' + Limits.getLimitCpuTime());
}
}
// Run in Anonymous Apex, then check the debug log of the job:
System.enqueueJob(new LimitProbe());The log shows 200, 12000000, and 60000.
Limits on starting async work
A transaction can only start so much async work. The main per-transaction caps are:
- Future method calls: 50 per transaction.
- Queueable jobs enqueued: 50 per synchronous transaction, but only 1 from inside a running Queueable job. This is why chains are sequential.
- Batch jobs: a job's
start,execute, andfinishmethods can each start only one more batch job.
A future method cannot call another future method. In general, a future method can't be called from a batch or future context either. If your design needs that, it points to Queueable.
Org-wide caps
Some limits belong to the org, not the transaction.
- Daily async executions. An org gets the greater of 250,000 or 200 times its number of user licenses per 24 hours. Future calls, Queueable jobs, Batch
executecalls, and scheduled runs all count against this number. A batch job over 2 million records with scope 200 uses about 10,000 executions on its own. - Concurrent batch jobs. At most 5 batch jobs can be queued or active at once. Further jobs go into the flex queue, which holds up to 100 jobs in a Holding status until a slot opens.
- Scheduled jobs. An org can have at most 100 scheduled Apex jobs.
Why this matters for tool choice
Per-transaction limits tell you what one job can do. Org-wide caps tell you how many jobs you can afford. A design with one job per record fails on caps even when each job passes its own limits. A design with one job per 200 records usually passes both. The later chapters use this idea to compare the tools.
Exercise
A synchronous trigger processes 20,000 records and fails with "Too many DML rows" at 10,000. A teammate suggests moving the same code into a Queueable job to fix it. Will that work, and which tool fits better?
Show answer
It will not work. The DML row limit is 10,000 in both synchronous and asynchronous transactions, so the Queueable job would fail the same way. Batch Apex fits better because each execute call handles a chunk of records in its own transaction with its own limits.