Sub topic 1
Why Run Code Asynchronously
Apex normally runs synchronously. A user saves a record, your trigger runs, and the user waits until all of it finishes. This chapter explains why that becomes a problem and how asynchronous Apex addresses it. Later chapters cover future methods in detail.
Prerequisites
This chapter assumes you already know:
- Basic Apex syntax: classes, static methods, and collections.
- SOQL and DML basics.
- How Apex triggers fire on insert, update, and delete.
- That Salesforce enforces governor limits on each transaction.
The synchronous model and its limits
A transaction is one unit of work. It starts when a user action or API call reaches Apex, and it ends when the code finishes and the database commits, or when an error rolls everything back. In synchronous Apex, everything happens inside that single transaction, and the caller waits for it.
This causes four common problems:
- Slow responses. Long work in a trigger delays the save button for the user.
- Tight limits. Synchronous transactions get a smaller CPU time limit (10,000 ms) and a smaller heap size (6 MB) than asynchronous ones (60,000 ms and 12 MB). Check the current Salesforce documentation, because limits can change between releases.
- Callout restrictions. A trigger cannot make an HTTP callout directly, because the transaction holds database locks and the platform will not keep them open while it waits on an external server.
- Mixed DML errors. You cannot change a setup object, such as
UserRole, and a non-setup object, such asAccount, in the same transaction.
What asynchronous Apex does
Asynchronous Apex moves part of the work into a separate transaction that runs later. The original transaction finishes and commits. The platform then queues the deferred work and runs it when resources are available.
The new transaction is independent in these ways:
- It gets its own set of governor limits.
- It does not hold up the user.
- It can do things the original transaction could not, such as make a callout.
- If it fails, it does not roll back the original transaction, which has already committed.
Asynchronous options in Apex
Apex offers several ways to run work asynchronously. This tutorial focuses on the first.
| Option | Typical use |
|---|---|
| Future method | Short, simple work with primitive arguments, such as a callout or setup object DML |
| Queueable Apex | Work that needs complex arguments or follow-up jobs |
| Batch Apex | Processing very large data sets in chunks |
| Scheduled Apex | Running code at a set time |
The last chapter of this tutorial compares future, Queueable, and Batch Apex in detail.
Trade-offs to expect
Asynchronous code is not a free speed-up. Keep these costs in mind:
- No guaranteed timing. The work runs when the platform has capacity, not immediately.
- No direct result. The caller cannot receive a return value from the deferred work.
- Data may change. Records can be edited between the time you queue the work and the time it runs.
- Harder debugging. Errors appear in a separate log, away from the user action that caused them.
- Extra limits. The platform caps how many asynchronous calls one transaction can queue.
Because of these costs, use asynchronous Apex only when a real need exists. Good reasons are a callout from a trigger, a mixed DML conflict, or work that is too heavy for the user to wait on.
Summary
Synchronous Apex makes the user wait and runs under tighter limits. Asynchronous Apex defers work to a separate transaction with fresh limits, which allows callouts from triggers and avoids mixed DML errors. The price is uncertain timing and no return value.
Exercise
A trigger on Account must send the new record to an external billing system over HTTP. Why does this call belong in asynchronous code, and what happens to the original save if the callout later fails?
Show answer
A trigger cannot make a callout directly, because the transaction holds database locks and the platform will not keep them open while waiting on an external server. Moving the callout into a separate asynchronous transaction avoids this. If the callout fails, the original save is unaffected, because that transaction already committed and runs independently.
Exercise
Name two costs of moving work into an asynchronous transaction.
Show answer
Any two of: the timing is not guaranteed, the caller gets no return value, the data may change before the job runs, errors are harder to trace because they appear in a separate log, and the platform caps how many asynchronous calls one transaction can queue.