Sub topic 1
Why asynchronous Apex: separate transactions and fresh limits
Prerequisites
This chapter assumes you already know:
- Basic Apex syntax and classes.
- Apex governor limits and transactions.
- SOQL and DML basics.
- How to run tests in Apex with
@IsTest.
The problem with doing everything in one transaction
Apex runs inside a transaction. A transaction is a unit of work that either commits together or rolls back together. Every transaction has governor limits, which are caps on resources such as queries, DML statements, CPU time, and heap size.
Synchronous code, such as a trigger or a controller action, runs while a user waits. It shares one set of limits with everything else in that transaction. A trigger that fires other triggers can use up the budget before your own logic runs.
You can check the limits of the current context in anonymous Apex:
System.debug('SOQL limit: ' + Limits.getLimitQueries());
System.debug('CPU limit (ms): ' + Limits.getLimitCpuTime());In a synchronous context this prints 100 and 10000.
What asynchronous Apex changes
Asynchronous Apex lets you request work now and have the platform run it later, in a different transaction. Two properties matter most.
- Separate transaction. The asynchronous job commits or rolls back on its own. If it fails, the transaction that requested it is not affected. If the requesting transaction rolls back, the request is discarded with it.
- Fresh limits. The job starts with its own limit counters. Some limits are also higher than in synchronous code.
| Limit | Synchronous | Asynchronous |
|---|---|---|
| SOQL queries | 100 | 200 |
| CPU time | 10,000 ms | 60,000 ms |
| Heap size | 6 MB | 12 MB |
| DML statements | 150 | 150 |
Some limits are the same in both contexts. Check the current Apex Developer Guide before you rely on a specific number.
Timing is not guaranteed
Asynchronous work does not run immediately. The platform starts it when resources are available, so a job may begin a few seconds later or after a longer wait. Do not write code that assumes the job has finished by the time the requesting code continues. The requesting code also cannot read a return value from the job.
Why this matters for the rest of the tutorial
Typical reasons to move work out of the synchronous path:
- The work is heavy and would exceed synchronous CPU or query limits.
- The work does not need to finish before the user sees a response.
- The work needs its own transaction, for example to mix setup and non-setup object DML, or to make a callout after a database change.
- A failure in the work should not roll back the user's original save.
Apex offers several asynchronous tools: future methods, Queueable Apex, Batch Apex, and Scheduled Apex. This tutorial focuses on Queueable Apex. It adds support for complex job state, job chaining, and transaction finalizers, which react when a job succeeds or fails. The next chapter writes a first Queueable class.
Exercise
A trigger saves an Account and enqueues an asynchronous job. The trigger then throws an unhandled exception and the transaction rolls back. What happens to the job, and why?
Show answer
The job request is discarded and the job never runs. The request belongs to the requesting transaction, so it is rolled back along with the Account save. Once a job has been requested and committed, its own failure no longer affects the original transaction, because it runs in a separate one.