Message queue
Twelve orders arrive in a burst, and each needs a slow email. Send it inside the request and everyone waits; put it on a queue and nobody does. Then add workers, retries for the blips, and somewhere to park the message that never works.
What you'll take away
- Explain why slow work like an email belongs behind a queue, not in the request
- Read a growing queue as work outpacing consumers, and add more to drain it
- Set retries with backoff and jitter, and a dead-letter queue for what always fails
- Know why at-least-once means maybe twice, so a consumer must be idempotent
The 1-minute version
Tap a point and the picture below plays that step.
An email takes 1.2 s and an order arrives every 0.4 s: the model's numbers
The checkout page hangs while it sends the confirmation email. What do you gain by moving the email behind a queue?
The picture: orders come from the phones to the shop (the web app). The queue is a conveyor of crates (amber: back for another try) for the workers; emails go down the street to the mail service, sent ones stack on its roof, and a crate that keeps failing ends up in the dead-letter shed. The light with a white ring is the order the step follows. Every timing comes from a simulation (1.2 s an email), not a real broker.
Compare them all: Message queue
Every tile gets the same 12 orders, one every 0.4 s, each needing a 1.2 s email. Order 5's email always fails, order 8's only the first time. See how long users wait, how long the queue gets, and which orders are lost.
Use it when Work that's quick and whose result the user needs right now: charging the card, checking stock
Skip it when Slow or flaky work the user needn't wait for: emails, thumbnails, reports. Everyone waits, and when it fails the whole request fails
A timing model, not a real broker. Up to 3 tries, waiting longer each time (backoff). Press any tile to see it large above, with when it fits.
Why a queue?
| Inside the request | Through a queue | |
|---|---|---|
| The user waits for | All of it | Saving the message |
| The email service is down | The request fails | Messages wait, then go |
| A burst of traffic | Everyone slows down | The queue grows, workers catch up |
| To go faster | A faster request path | More consumers |
At-least-once means "maybe twice"
A worker that dies after doing the work but before its ack gets the message handed to someone else. Most queues work this way, so make consumers idempotent: record what's done by a key (the order id), and a second delivery does nothing.
Watch the queue, not just the workers
A queue that keeps growing is the first sign of trouble: consumers too few or too slow, or a poison message being retried for ever. Alert on its length and its oldest message, and look at the dead-letter queue every day.
In real code
XADD emails 1-0 order 1001 to mai@example.com
XADD emails 2-0 order 1002 to beam@example.com
XADD emails 3-0 order 1003 to pim@example.com
XGROUP CREATE emails mailers 0
XREADGROUP GROUP mailers worker-1 COUNT 2 STREAMS emails >
XREADGROUP GROUP mailers worker-2 COUNT 2 STREAMS emails >
XACK emails mailers 1-0 3-0
XPENDING emails mailers
XAUTOCLAIM emails mailers worker-2 0 0-0 COUNT 10 JUSTID
XLEN emails
redis-cli < emails.redis (checked against redis-server 7.4 in Docker)It printed:1-0 2-0 3-0 OK emails 1-0 order 1001 to mai@example.com 2-0 order 1002 to beam@example.com emails 3-0 order 1003 to pim@example.com 2 1 2-0 2-0 worker-1 1 0-0 2-0 3
A queue with consumer groups, in redis-cli: three messages, two workers each take some, worker-1 acks one but never the other (it "died"), XPENDING shows what's unacked, and worker-2 claims it with XAUTOCLAIM. Run against redis-server 7.4 in Docker; below is what redis-cli answered.
Common questions
Why not send the email right inside the request?
You can, but every user waits for the email to go out (1.25 s an order in the picture), and when the mail service blips the request fails even though the order was saved. Slow or flaky work the user doesn't need to wait for belongs behind a queue.
What are a producer, a queue and a consumer?
The producer drops a message (send the email for order 1001), the queue keeps it until someone takes it, and the consumer, a worker, does the work and acks it. The two sides don't need to know each other or even run at the same time.
The queue keeps growing. What does it mean?
Work arrives faster than the consumers finish it. Add consumers if the jobs are independent, or make each job faster, and alert on the queue's length and its oldest message. A poison message retried for ever can swell it too.
Why should a retry wait first (backoff)?
If the other end is struggling, retrying at once over and over only pushes it further down. Wait longer each time (exponential backoff), with a little randomness (jitter), so it has time to recover. In the picture, order 8 fails once, waits, and the second try works.
What is a dead-letter queue for?
Some messages fail however often you try, like a bad email address. With nowhere to park them they go round for ever, wasting workers and maybe holding others up. Cap the tries, move them to a dead-letter queue for a person to look at, and check it every day.
What does at-least-once mean, and why must consumers be idempotent?
Most queues promise at least once, which means maybe twice: a worker sends the email, dies before its ack, and the message goes to another worker. Record what's done by a key such as the order id, and a second delivery does nothing.
Do messages always come out in order?
One queue with one consumer usually keeps the order, but several consumers or retries can shuffle it: in the picture, order 8 is retried and finishes after orders that came behind it. Work that must stay in order needs designing for (Kafka keeps order only within one partition).
Next lesson
IdempotencyLevel: Intermediate
Messages can come twice, so a worker must be safe to repeat; idempotency shows how to make the work happen once.