
# 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:

```typescript
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.

> **Note:** 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 `concurrency` jobs, and the workers of the pool process at most `max` jobs together. When one queue is quiet, the other workers can use the free slots, up to their own `concurrency`.
- **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](https://docs.bullmq.io/bullmq-pro/batches.md).
- **Other limits still apply.** [Group concurrency](https://docs.bullmq.io/bullmq-pro/groups/concurrency.md), [rate limits](https://docs.bullmq.io/bullmq-pro/groups/rate-limiting.md) and the other options of the workers work as usual within the pool.

The pool works with [sandboxed processors](https://docs.bullmq.io/bullmq-pro/groups/sandboxes-for-groups.md) too. It only applies to workers created with a processor: a worker without one, used to [fetch jobs manually](https://docs.bullmq.io/patterns/manually-fetching-jobs), ignores it.

## Changing the limit at runtime

You can change `max` at any time, for example to adapt to the load of the machine:

```typescript
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:

```typescript
console.log(pool.active); // slots in use
console.log(pool.waiting); // workers waiting for a free slot
```

## Concurrency 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](https://docs.bullmq.io/bullmq-pro/groups/concurrency.md) | the jobs of each group of a queue | all the workers of the queue, in every process |
| [Global concurrency](https://docs.bullmq.io/guide/queues/global-concurrency) | the jobs of a queue | all the workers of the queue, in every process |

## Read more:

- 💡 [ConcurrencyPool API Reference](https://api.bullmq.pro/classes/v7.ConcurrencyPool.html)
- 💡 [WorkerPro API Reference](https://api.bullmq.pro/classes/v7.WorkerPro.html)
