Sub topic 17
Batch Apex versus Queueable, Bulk API and other options
This chapter assumes you can write Apex classes, run SOQL and DML, know the main governor limits, and have already built a basic batch class with start, execute and finish. Here you decide when Batch Apex is the right tool and when something else fits better.
The options at a glance
Batch Apex is one of several ways to move work out of the user's request. Each option has a different sweet spot.
| Option | Best for | Main limit to remember |
|---|---|---|
| Batch Apex | Processing many records in chunks, each in its own transaction | A fixed number of jobs can be active at once, and extra jobs wait in the Flex Queue |
| Queueable Apex | One background task, such as a follow-up after a trigger or a callout | Works on data you pass in, with no built-in chunking of a large query |
| Future method | A simple, fire-and-forget task with primitive arguments | No job Id, no chaining, and limited arguments |
| Bulk API | Loading or exporting data from outside Salesforce | Runs no custom Apex logic of its own beyond triggers, flows and rules |
| Flow or scheduled flow | Simple record updates configured without code | Less control over error handling at very high volume |
Choosing between them
Start from the shape of the work.
- Many records already in Salesforce, and you must run Apex logic on each. Use Batch Apex. The query locator can return up to 50 million records, and each chunk gets fresh governor limits.
- A small, known set of records or one unit of work. Use Queueable Apex. It is lighter than a batch job, accepts objects such as lists and custom types as inputs, and returns a job Id you can monitor.
- Data coming from or going to another system. Use the Bulk API. It loads and extracts large volumes efficiently and does not consume your Apex execution time. Call it from an integration tool, not from inside Apex.
- A simple field update with clear criteria. Consider a scheduled flow first. Less code means less to maintain.
Queueable as a batch replacement
A Queueable job can process a list in slices and re-enqueue itself for the rest. This works for modest volumes, but you manage the slicing yourself.
public class RatingQueueable implements Queueable {
private List<Id> remaining;
public RatingQueueable(List<Id> ids) {
this.remaining = ids;
}
public void execute(QueueableContext qc) {
Integer sliceSize = Math.min(200, remaining.size());
List<Id> slice = new List<Id>();
for (Integer i = 0; i < sliceSize; i++) {
slice.add(remaining[i]);
}
List<Account> accts = [SELECT Id, Rating FROM Account WHERE Id IN :slice];
for (Account a : accts) {
a.Rating = 'Cold';
}
update accts;
List<Id> rest = new List<Id>();
for (Integer i = sliceSize; i < remaining.size(); i++) {
rest.add(remaining[i]);
}
if (!rest.isEmpty() && !Test.isRunningTest()) {
System.enqueueJob(new RatingQueueable(rest));
}
}
}The list of Ids must fit in heap memory, and you must write the progress tracking yourself. For millions of records, Batch Apex does this work for you through QueryLocator.
Common mistakes
- Using Batch Apex for a single callout or small task. Starting a batch job adds queue overhead, and a Queueable job is simpler.
- Using Bulk API when the logic needs Apex that is not in a trigger. Bulk API moves data. It does not run your batch class.
- Looping a Queueable over millions of records. A chain of jobs is hard to monitor and hard to restart.
Exercise
A nightly process must recalculate a custom score on 8 million Contact records using Apex logic that already exists in a service class. Which option do you choose, and why not Queueable Apex?
Show answer
Batch Apex. The data is already in Salesforce and needs Apex logic on every record. A QueryLocator streams up to 50 million records in chunks with fresh governor limits per chunk. A Queueable job would need you to hold Ids in memory, slice them by hand and track progress yourself.