Sub topic 2
What changes in async: limits and org-wide caps
This chapter assumes you can read and write basic Apex, run SOQL and DML, and already know the synchronous transaction model from triggers. It also assumes you know governor limits at a high level, meaning the per-transaction ceilings Salesforce enforces on shared infrastructure. The chapter compares the limits you meet in synchronous and asynchronous code. It then covers the org-wide caps that sit above every individual job.
The numbers below are the documented values at the time of writing. Salesforce adjusts them between releases, so check the Apex Developer Guide limits page before you rely on a specific figure.
Two kinds of limits
Async Apex is subject to two separate layers of limits.
- Per-transaction limits apply to one execution of your code. Examples are the number of SOQL queries, the heap size, and the CPU time. Every transaction gets a fresh set of counters.
- Org-wide caps apply across all jobs in your org. Examples are how many batch jobs can be active at once and how many async executions you can use in 24 hours. These counters are shared by every job and every developer in the org.
Moving work to async raises some per-transaction limits. It also adds org-wide caps that synchronous code never hits.
Per-transaction limits: what gets bigger
Apex runs async code in its own transaction, separate from the user request that started it. Salesforce allows these transactions more room for the three limits that most often cause trouble in synchronous code.
| 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 |
| DML rows | 10,000 | 10,000 |
| Records returned by SOQL | 50,000 | 50,000 |
| Callouts | 100 | 100 |
Most limits do not change. A future method that tries to update 20,000 records in one DML call still fails, because the 10,000-row limit is the same. Async gives you more CPU time, more heap, and more queries. It does not give you more rows.
Limits reset per transaction, not per job
A transaction here means one run of Apex code that either commits or rolls back as a unit. The tools differ in how many transactions one job contains.
- A future method or a Queueable job is one transaction.
- A Batch job is many transactions. Each call to
executeis its own transaction with fresh limits, as arestartandfinish. - A Scheduled job's
executemethod is one transaction. It usually only enqueues other work.
This is why Batch Apex can process millions of records. No single transaction touches more than one scope of records, so the per-transaction row limits never accumulate.
Reading limits at run time
The Limits class returns how much of each limit you have used and what the ceiling is. The following class logs the main limits. It shows different ceilings depending on the context that calls it.
public class LimitReport {
public static void log(String label) {
System.debug(label + ' | SOQL ' + Limits.getQueries() + '/' + Limits.getLimitQueries()
+ ' | Heap ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize()
+ ' | CPU ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime()
+ ' | Enqueue ' + Limits.getQueueableJobs() + '/' + Limits.getLimitQueueableJobs());
}
}
public class LimitProbeJob implements Queueable {
public void execute(QueueableContext ctx) {
LimitReport.log('Inside Queueable');
}
}Run LimitReport.log('Anonymous'); in Anonymous Apex, then run System.enqueueJob(new LimitProbeJob());. Compare the two debug lines in the log. The synchronous call reports a CPU ceiling of 10000 and the Queueable call reports 60000. The enqueue ceiling also drops from 50 to 1, which the next section covers.
Limits that are tighter in async
Some limits go the other way. These ones catch developers who assume async is simply a larger version of sync.
- Enqueueing from async. A synchronous transaction can call
System.enqueueJobup to 50 times. A Queueable, future, or Batch context can enqueue only 1 job. This is why Queueable chaining is one child per parent (see the chaining chapter). - Call stack depth. Chained Queueable jobs have no depth limit in production orgs. Developer Edition and trial orgs limit the chain to a depth of 5.
- Method parameters. Future methods accept only primitives and collections of primitives. This is a restriction on the method signature, not a counted limit, but it shapes how you pass data.
- Order of execution. Async jobs do not run immediately and do not run in a guaranteed order. They start when Salesforce has capacity, and you cannot assume that job A finishes before job B starts.
Org-wide caps
Org-wide caps do not belong to one transaction. They count activity across the whole org, and they are why one badly designed async process can slow down every other team that uses the org.
Daily async Apex executions
The org gets a rolling 24-hour allowance of async Apex executions. The allowance is the greater of 250,000 or 200 multiplied by the number of user licenses in the org. The unit being counted is an execution, not a job:
- Each future method call counts as one.
- Each Queueable job counts as one.
- Each Batch
executecall counts as one, so a batch over 10 million records with scope 200 uses about 50,000 executions. - Each Scheduled
executecall counts as one.
Because Batch counts every scope, a small scope size can use up the allowance faster than you expect. A 1-million-record job with scope 10 uses 100,000 executions, where scope 200 uses 5,000.
You can read your current consumption in Apex with System.OrgLimits:
Map<String, System.OrgLimit> limitsMap = System.OrgLimits.getMap();
System.OrgLimit asyncLimit = limitsMap.get('DailyAsyncApexExecutions');
System.debug('Async executions used: ' + asyncLimit.getValue() + ' of ' + asyncLimit.getLimit());Concurrency caps per tool
Each tool has its own cap on how much work can be waiting or running at once.
| Tool | Cap | What happens at the cap |
|---|---|---|
| Batch Apex | 5 queued or active jobs | Extra jobs go to the flex queue in Holding status |
| Flex queue | 100 jobs in Holding status | `Database.executeBatch` throws an exception |
| Scheduled Apex | 100 scheduled jobs | `System.schedule` throws an exception |
| Queueable | 50 enqueues per synchronous transaction | The 51st `System.enqueueJob` throws a `LimitException` |
| Future | 50 calls per synchronous transaction | The 51st call throws a `LimitException` |
The flex queue is the waiting area for batch jobs. A job that cannot start because 5 are already active waits there instead of failing. An admin can reorder held jobs on the Apex Flex Queue page in Setup.
Per-callout and other caps
Callout limits are the same in sync and async: 100 callouts per transaction, with a cumulative timeout of 120 seconds. Async code does not get extra callout time. Its benefit is that a slow callout no longer blocks the user's request.
What this means for design
These limits point to a few practical habits.
- Do not enqueue one job per record. In a trigger that handles 200 records, enqueue one job that takes all 200 Ids. A loop that calls
System.enqueueJob200 times throws aLimitExceptionat the 51st call and wastes executions from the daily pool. - Check before enqueueing. If your code might run in several contexts, compare
Limits.getQueueableJobs()withLimits.getLimitQueueableJobs()before you callSystem.enqueueJob. - Pick batch scope on purpose. Scope size trades per-transaction headroom against the daily executions counter. The batch chapters return to this.
- Treat the daily cap as shared. Your job competes with every other process in the org. A nightly job that uses most of the allowance can starve other jobs later in the day.
Exercise
A trigger handles 200 Accounts and calls System.enqueueJob once per Account. The same code also runs from a Queueable job's execute method. What happens in each context, and what is the fix?
Show answer
In the synchronous trigger, the 51st System.enqueueJob call throws a LimitException because the cap is 50 enqueues per synchronous transaction. In the Queueable context the cap is 1, so the second call throws. The fix is to collect all 200 records (or their Ids) into one list and enqueue a single job that processes the whole list, which uses one enqueue and one daily async execution.
Exercise
A batch job runs over 2,000,000 records with a scope size of 50. How many daily async executions does it use for its execute calls, and how many would it use with scope 200?
Show answer
With scope 50 it uses 2,000,000 / 50 = 40,000 executions. With scope 200 it uses 2,000,000 / 200 = 10,000 executions. The larger scope uses a quarter of the daily allowance, provided each scope still fits within the per-transaction limits.