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.

Level: IntermediateWatch: 10 steps · ~80 secondsSend a burst of ordersRecall: 3 questions · 8 cardsCompare 4 waysRedis Streams · TypeScript

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.

4Compare them all
Inside the requestsend the email, then answer
Web app · sends emails
nobody waiting
Mail service · 0 sent
#1 · sending the email → …
Step 1 / 10 · step by step
1250 msThe user waits
1In the queue now
0Sent
0Errors shown
0dead-letter

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.

Step 4 · Every option at once, on one clock

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.

0.0 s (simulated)
Inside the requestsend the email, then answeruser waits 1250 ms · queue 1 · lost 0 · DLQ 0

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 requestThrough a queue
The user waits forAll of itSaving the message
The email service is downThe request failsMessages wait, then go
A burst of trafficEveryone slows downThe queue grows, workers catch up
To go fasterA faster request pathMore 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

emails.redis
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
Run: 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.

Table: why 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.

Read: watch the queue 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.

Read: at-least-once means maybe twice

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.