Sub topic 14
Concurrency Limits and the Flex Queue
This chapter assumes you can write and run a basic Database.Batchable class and call Database.executeBatch. It explains what happens when you start more batch jobs than the org can run at once.
How a job moves through the queue
A batch job does not always start when you call Database.executeBatch. Salesforce allows only five batch jobs to be queued or active at the same time. This is a per-org limit, not a per-user limit. Every call to executeBatch creates an AsyncApexJob record whose Status shows where the job is.
- Holding: the job is in the Flex Queue, waiting for a free slot.
- Queued: the job has a slot and is waiting for resources.
- Preparing: the
startmethod is running. - Processing:
executecalls are running. - Completed, Failed or Aborted: the job has ended.
The Flex Queue is a holding area for jobs that cannot yet get a slot. It can hold up to 100 jobs in Holding status. When a slot frees up, Salesforce moves the job at the top of the Flex Queue into the queued state. By default, jobs run in the order they were submitted.
What happens at the limits
If five jobs are active and the Flex Queue is not full, a new job goes to Holding. It does not throw an error. If the Flex Queue already holds 100 jobs, executeBatch throws a System.LimitException. Code that starts many jobs, such as a loop or a trigger, should check capacity first.
Integer holding = [
SELECT COUNT() FROM AsyncApexJob
WHERE JobType = 'BatchApex' AND Status = 'Holding'
];
if (holding < 100) {
Database.executeBatch(new AccountRatingBatch(), 200);
} else {
System.debug('Flex Queue is full. Try again later.');
}A separate limit applies inside a running job. Salesforce runs execute calls of one job one after another, so a single job never competes with itself. Different jobs can run in parallel, and they can lock the same records. If two jobs update the same Accounts, you may see UNABLE_TO_LOCK_ROW errors. Design jobs so that they do not overlap on the same data, or chain them.
Reordering the Flex Queue
In Setup, open Apex Flex Queue to see held jobs and drag them to a new position. You can also reorder them in code with the System.FlexQueue class.
// Move a held job to the front of the Flex Queue
Id jobId = '707000000000001AAA'; // replace with a real AsyncApexJob Id
Boolean moved = System.FlexQueue.moveJobToFront(jobId);
System.debug('Moved: ' + moved);moveBeforeJob, moveAfterJob and moveJobToEnd work the same way. Only jobs in Holding status can be moved. A job that is already Queued or Processing cannot.
Practical rules
- Count your org's other batch jobs, including managed packages, before assuming you have all five slots.
- Check the Flex Queue count before submitting jobs in bulk.
- Abort held jobs you no longer need with
System.abortJob(jobId). - Run jobs that touch the same objects one after another, using chaining from
finish.
Exercise
You call Database.executeBatch six times in a row in an org with no other batch activity. What status does the sixth job have, and does the call throw an error?
Show answer
The sixth job has the status Holding. The first five take the active slots, so the sixth waits in the Flex Queue. No error is thrown, because the Flex Queue holds up to 100 jobs and has room. An error occurs only when the Flex Queue is full.