
Why hand work to a queue
- The user does not wait. Checkout returns at once, and the receipt email is sent a second later.
- A slow or broken dependency does not block you. If the email service is down, the jobs wait in the queue.
- Bursts get smoothed out. A thousand orders in a minute become a steady stream the workers can handle.
- Failed work can be tried again without the user doing anything.
The words
| Word | Meaning |
|---|---|
| Producer | The code that adds a job to the queue |
| Consumer or worker | The code that takes a job and does it |
| Acknowledge | The worker telling the queue that the job is done and can be removed |
| Retry | Putting a failed job back to be tried again |
| Dead letter queue | Where jobs go after they have failed too many times |
What it costs
- The answer comes later. The user is told the order was received, not that it succeeded. The product has to deal with that.
- Jobs can run twice. Most queues promise that a job is delivered at least once, not exactly once. Workers have to be safe to repeat. See idempotency.
- Order is not kept unless you ask for it and pay the price in speed.
- Failures are quiet. A job that keeps failing shows an error page to nobody. Someone has to watch the failed jobs.
When you do not need one
If the work is fast and the user needs the result now, call the function. A queue adds a moving part, a place for jobs to get stuck, and a delay. It earns its place when the work is slow, unreliable or arrives in bursts.
Questions you will hear in a meeting
- What happens to a job that fails?
- How many times is it retried, and where does it go after that?
- Is the worker safe to run twice on the same job?
- How would we notice that the queue is growing?
In an interview
Put a queue between two parts of a design and you will be asked what happens when the worker crashes in the middle of a job. The answer: the job was not acknowledged, so it is delivered again, which is why the work has to be idempotent.