Message queues, step by step
Why does a checkout reply in a blink while the email arrives a second later? Watch a shop slow down on Black Friday, pick the fix, and see queues, workers and dead-letter queues appear for a reason.
3 steps · about 5 minutes · free, no sign-up
Step 1: Black Friday
The shop waits for each email before replying: checkout takes 9 seconds.
Checkout waits for the slow email service, so it takes 9 seconds and shoppers give up. What do you do?
- Give the email service a bigger server (Helps for a while): Emails go out faster for now, but checkout still waits on another service. The next spike, or the next email outage, freezes checkout again.
- Put the email on a queue and reply right away (Right fix): The shop saves the order, drops a "send receipt" job on the queue and answers in 0.2 seconds. A worker sends the email a moment later. Message queue: A waiting line between the part that asks for work and the part that does it. The asker moves on at once; the worker catches up. Slow or broken services stop slowing down your users.
- Stop sending receipts (Not this one): Checkout is fast, but shoppers don’t know if they paid, so they order twice or call support. The work still needs doing, just not while they wait.
Step 2: The queue keeps growing
One worker can’t keep up: 50,000 receipts are waiting, some for an hour.
Jobs arrive faster than one worker can send them, so the queue grows and receipts arrive an hour late. What do you do?
- Delete jobs older than 10 minutes (Not this one): The queue looks short, but thousands of shoppers never get a receipt. A queue should hold work until it is done, not throw it away.
- Give the worker a faster machine (Helps for a while): It catches up for now, but one worker has a ceiling, and if it crashes nothing gets sent at all.
- Add more workers reading the same queue (Right fix): Two workers take jobs side by side, so the queue empties twice as fast. When sales spike, add more; when it’s quiet, run fewer. Competing consumers: Several identical workers read one queue. Each job goes to exactly one of them, so adding workers adds speed. Autoscaling can add workers when the queue gets long.
Step 3: One poison message
One broken job crashes a worker every time it is picked up, and the queue stalls.
One job has a bad email address. Each worker that takes it crashes, the job goes back on the queue, and everything behind it waits. What do you do?
- Restart the workers (Not this one): They come back, pick up the same broken job and crash again. The job is the problem, not the workers.
- After 3 failed tries, move the job to a dead-letter queue (Right fix): The broken job is set aside after its third failure. The workers carry on with the good jobs, and someone fixes the bad address later. Dead-letter queue: A side queue for jobs that keep failing. It stops one bad message from blocking everything, and keeps it safe for a person or script to inspect and replay.
- Keep retrying until it works (Helps for a while): Retrying is right for short glitches, like the email service being down for a minute. But this job will never work, so it keeps crashing workers forever.
What you built
- Message queue: Holds jobs between the shop and the slow work, so shoppers never wait for it.
- Workers: Take jobs from the queue and do them. Add more to go faster (competing consumers).
- Retries: A failed job goes back on the queue and is tried again, which fixes short glitches.
- Dead-letter queue: Jobs that keep failing are moved aside so they can’t block the rest.
| Problem | Fix | Trade-off |
|---|---|---|
| Users wait for slow work | Queue + worker | The work finishes a moment later, not instantly |
| Queue keeps growing | More workers | Jobs may finish out of order |
| One job keeps failing | Dead-letter queue | Someone must check it |
- Lesson: webhooks done right (retries, idempotency)
- Lesson: microservices and API gateways
- Read: message queues explained
Frequently asked questions
What is a message queue in simple terms?
A waiting line for jobs. One part of a system drops a job in and carries on; another part picks it up and does it when it can. That keeps slow or unreliable work away from your users.
When should I use a message queue?
When work is slow, can happen a moment later, or depends on another service: sending emails, resizing images, generating PDFs, calling partners. Not when the user needs the answer right now.
What is a dead-letter queue?
A separate queue where jobs go after failing a set number of times, so they stop blocking other jobs. You inspect them, fix the cause and replay them.
Do queues deliver each message exactly once?
Most deliver at least once, so a job can occasionally arrive twice. Make jobs safe to repeat (idempotent), for example by checking whether the receipt was already sent.