Sub topic 15
Queueable, future method, or Batch Apex?
This chapter assumes you can write a Queueable class, chain jobs, and read the governor limits for synchronous and asynchronous transactions. Here you will compare the three main asynchronous tools and pick one for a given workload.
What each tool does
A future method is a static method marked @future. It takes only primitive arguments (or collections of primitives), returns void, and cannot be chained. You cannot get a job ID back from it in a form you can monitor easily, and you cannot attach a finalizer.
A Queueable is a class that carries state in member variables, including sObjects and custom types. System.enqueueJob returns a job ID. A queueable can enqueue one child job, can attach a finalizer, and can be configured with AsyncOptions.
Batch Apex is a class that implements Database.Batchable. It splits a data set into batches, usually from a QueryLocator that can cover up to 50 million records. Each batch runs in its own transaction with fresh limits. It has start, execute, and finish methods.
Side-by-side comparison
| Feature | Future method | Queueable | Batch Apex |
|---|---|---|---|
| Parameters | Primitives and collections of primitives | Any serializable member variables | Query or iterable, plus member variables |
| Returns a job ID | No usable ID | Yes | Yes |
| Chaining | No | One child job per execution | `finish` can start another batch |
| Finalizer | No | Yes | No (use `finish`) |
| Best data size | Small, one operation | Small to medium, or sliced by a chain | Large, up to millions of records |
| Built-in slicing | No | No, you write it | Yes, scope size up to 2,000 |
Decision flow
Rules of thumb
- Start with Queueable for new code. It does everything a future method does and adds state, chaining, finalizers, and monitoring.
- Choose Batch Apex when the record count is large or unknown. A chain of queueables can slice a list, but it processes one slice at a time and you must write the slicing logic yourself.
- Keep a future method only when existing code depends on it. Some older code and certain mixed-DML workarounds still use it.
- Check limits that differ by context. A batch
startorexecutemethod can enqueue one queueable, while a transaction in a synchronous context can enqueue up to 50.
Example: the same job in two shapes
The queueable below needs an object parameter, so a future method would not work.
public class RatingUpdateJob implements Queueable {
private List<Account> accounts;
public RatingUpdateJob(List<Account> accounts) {
this.accounts = accounts;
}
public void execute(QueueableContext context) {
for (Account a : accounts) {
a.Rating = 'Hot';
}
update accounts;
}
}A future method can only receive IDs, so it must query the records again.
public class RatingFuture {
@future
public static void updateRatings(Set<Id> accountIds) {
List<Account> accounts = [SELECT Id, Rating FROM Account WHERE Id IN :accountIds];
for (Account a : accounts) {
a.Rating = 'Hot';
}
update accounts;
}
}Exercise
A nightly process must update 8 million Contact records and needs each slice to commit separately. Which tool fits best, and why not a queueable chain?
Show answer
Batch Apex. It handles a QueryLocator over millions of records and slices the data automatically, with fresh limits per batch. A queueable chain would run one slice at a time with hand-written slicing code, and it would take far more code to manage for that volume.
Exercise
You need to retry a background job up to three times after an unhandled exception. Which tool can do this, and which feature does it use?
Show answer
Queueable Apex, using a transaction finalizer. The finalizer reads the result from FinalizerContext and re-enqueues the job until it reaches the attempt limit.