Sub topic 15
Choosing between future, Queueable and Batch
This chapter assumes you have read the earlier chapters. You should know why a trigger cannot call out directly, and you should have seen the @future(callout=true) and Queueable versions of the pattern. Here you decide which one to use.
The short answer
For a callout started by a trigger, use Queueable Apex unless you have a specific reason not to. Use a future method only for very small, fire-and-forget work. Use Batch Apex when the volume is too large for one job, or when the work runs on a schedule rather than in response to one transaction.
What differs between the three
| Feature | Future method | Queueable | Batch |
|---|---|---|---|
| Callout permission | `@future(callout=true)` | `Database.AllowsCallouts` | `Database.AllowsCallouts` |
| Parameters | Primitives and collections of primitives | Any serializable member variable, including sObjects and custom types | Records from a query locator or iterable |
| Job Id returned | No | Yes, from `System.enqueueJob` | Yes, from `Database.executeBatch` |
| Chaining | Cannot call another future method from a future method | One child job per `execute` | Chain from `finish` |
| Finalizer support | No | Yes | No. Use the `finish` method |
| Enqueue limit per transaction | 50 future calls | 50 jobs | 100 jobs can be held in the flex queue |
| Typical volume | A few records | Hundreds to a few thousand | Thousands to millions |
Each job type runs in its own transaction with its own set of governor limits. The 100-callout limit applies per execute call, not to the whole job.
Decision flow
Reasons to pick each one
Future methods
A future method is the least code. That is its only real advantage. It returns no job Id, so you cannot monitor one specific call. It also cannot be chained and has no Finalizer. A future method cannot be called from another future method or from a Batch job. This restriction causes trouble when a trigger fires from async code.
Queueable
Queueable is the default choice for trigger callouts. It lets you pass a Set<Id> as a normal member variable, attach a Finalizer for failures, and chain a second job when you need a follow-up callout. Because it returns a job Id, you can look the job up in AsyncApexJob.
Batch
Batch Apex splits work into scopes and runs each scope as a separate transaction. Starting it from a trigger is risky for two reasons. The platform allows only five queued or active batch jobs at a time, and extra jobs wait in the flex queue. A busy org can hit these limits quickly. Prefer Batch for nightly reconciliation, backfills and re-sending failed records. A trigger can leave a status field on the record, and a scheduled Batch job can pick up the unsynced ones later.
Common mistakes
- Choosing a future method, then needing to pass an sObject or a list of custom objects. Future methods do not accept them.
- Enqueuing one Queueable per record in a loop, which hits the 50-job limit. Enqueue once for the whole batch of Ids.
- Starting Batch Apex from a trigger without checking the active job count.
- Forgetting that a future call fails if the trigger runs in a context that is already future or batch.
Exercise
A trigger on Order__c must send each new order to a billing API. After the callout succeeds, you need to start a second callout to a shipping API. You also want to log failures if the first callout throws an unhandled exception. Which approach should you use, and why?
Show answer
Use Queueable Apex. Chain the shipping callout by enqueuing a second job from the first job's execute method, and attach a Finalizer to log the unhandled exception. A future method cannot be chained or finalized, and Batch Apex is more than a per-transaction trigger callout needs.