Concurrency pool
A worker's concurrency option limits how many jobs that worker processes at the same time. When a process runs several workers, for example one per queue, each worker counts only its own jobs: three workers with concurrency: 10 can process up to 30 jobs at once.
A ConcurrencyPool adds a limit for all of them together. Workers that share a pool never process more than the pool's max jobs at the same time, even when they consume different queues:
import { ConcurrencyPool, WorkerPro } from '@taskforcesh/bullmq-pro';
const pool = new ConcurrencyPool({ max: 20 });
const emails = new WorkerPro('emails', processEmail, {
connection,
concurrency: 10,
concurrencyPool: pool,
});
const reports = new WorkerPro('reports', processReport, {
connection,
concurrency: 10,
concurrencyPool: pool,
});
const images = new WorkerPro('images', processImage, {
connection,
concurrency: 10,
concurrencyPool: pool,
});
// At most 20 jobs are processed at the same time in this process,
// and at most 10 of them by each worker.This is useful when the jobs of several queues compete for the same resources, such as the CPU or memory of a machine, or connections to a database, and all of the queues can have more jobs ready than the process can handle at once.
INFO
The pool lives in memory, so it coordinates the workers of a single process. Every process creates its own pool: if you run several processes on the same machine, each one is limited to max jobs.
How it works
- Each worker keeps its own limit. A worker processes at most its own
concurrencyjobs, and the workers of the pool process at mostmaxjobs together. When one queue is quiet, the other workers can use the free slots, up to their ownconcurrency. - Jobs stay in the queue until there is a free slot. A worker takes a slot from the pool before it moves a job to active, so jobs that cannot be processed yet are not taken from Redis, and remain available to workers in other processes.
- No queue is starved. When the pool is full, the workers waiting for a slot are served in turn, so a busy queue cannot keep all the slots for itself.
- A batch counts as one slot, in the same way that it counts as one for the worker's
concurrency. See Batches. - Other limits still apply. Group concurrency, rate limits and the other options of the workers work as usual within the pool.
The pool works with sandboxed processors too. It only applies to workers created with a processor: a worker without one, used to fetch jobs manually, ignores it.
Changing the limit at runtime
You can change max at any time, for example to adapt to the load of the machine:
pool.max = 30;When you raise it, waiting workers can start new jobs right away. When you lower it, running jobs are not interrupted: no new job starts until the number of active jobs goes below the new limit.
max must be an integer greater than 0.
Monitoring
The pool tells you how it is being used:
console.log(pool.active); // slots in use
console.log(pool.waiting); // workers waiting for a free slotConcurrency options compared
| Option | Limits | Scope |
|---|---|---|
Worker concurrency | the jobs of one worker | one worker |
ConcurrencyPool | the jobs of all the workers that share the pool | one process |
| Group concurrency | the jobs of each group of a queue | all the workers of the queue, in every process |
| Global concurrency | the jobs of a queue | all the workers of the queue, in every process |