A message queue lets one part of a system hand work to another without waiting for it. The web server writes “send this receipt” into the queue and answers the customer at once. An email worker picks the message up when it is free.
That small idea brings its own problems: messages that arrive twice, messages that never arrive, work done out of order, and one bad message that blocks everything behind it. This page shows each one happening, then the fix.
In the labs after the walkthrough, every coloured chip is a single message in a simulation running in your browser. The letter is its key, such as a customer, and the number is its place in that key’s order. A3 is customer A’s third message. A dark chip marked !B2 always fails. A small number on a chip’s corner counts how many times it has been delivered. A faded chip is waiting out its visibility timeout.
00 The life of one message
Before the live labs, follow a single message from start to finish. Step through at your own pace, or press Play all.
Step 1 of 10
Meet the pieces
A queue has three parts. The producer creates work, the broker stores it, and the consumer does it. The producer and consumer never talk to each other directly; everything goes through the broker.
01 Why put a queue in the middle
Without a queue, the caller waits on the callee. If the email service is slow, checkout is slow. If it is down, checkout fails. With a queue, the producer only waits for the broker to store the message, a few milliseconds, and the consumer works at its own pace.
The queue also absorbs bursts. Producers can briefly send faster than consumers can work, and the extra messages wait in line instead of failing.
- Waiting
- 0
- Oldest wait
- 0.0s
- Processed
- 0
Click any chip to follow that one message.
Watch two numbers on any queue: how many messages are waiting, and how long the oldest one has waited. A depth that keeps growing means consumers are slower than producers.
02 Acks: lost or duplicated, pick one
The broker cannot see inside a consumer. It only knows a message is finished when the consumer says so with an ack. When to send that ack is the most important choice in queue design.
- Ack when received (at-most-once). If the consumer dies mid-way, the broker already thinks the message is done. It is lost.
- Ack after the work (at-least-once). Nothing is lost, but if the consumer dies after doing the work and before the ack, the message is delivered again and the work runs twice.
- Processed
- 0
- Duplicates
- 0
- Lost
- 0
Click any chip to follow that one message.
Exactly-once delivery over a network is not something a broker can promise on its own. Real systems choose at-least-once and make processing safe to repeat (section 04).
03 The visibility timeout
With ack after processing, a received message is not deleted. It is hidden for a while (Amazon SQS calls this the visibility timeout; Google Pub/Sub calls it the ack deadline). If no ack arrives in time, the broker assumes the consumer died and hands the message to someone else.
That is how crashed work gets retried. It is also a trap: if the work takes longer than the timeout, the message is handed out again while the first consumer is still working on it.
- Processed
- 0
- Duplicates
- 0
Click any chip to follow that one message.
Set the timeout comfortably above your slowest job, or have long jobs extend it while they run.
04 Idempotency: making repeats harmless
Since duplicates will happen, make processing safe to run twice. The usual ways:
- Record each message id you have processed, in the same transaction as the work, and skip ids you have seen.
- Pass an idempotency key to outside services, so a repeated charge returns the first result.
- Write with upserts (“set the status to shipped”), not increments (“add one to shipped”).
- Processed
- 0
- Duplicates
- 0
- Repeats skipped
- 0
Click any chip to follow that one message.
At-least-once delivery plus idempotent processing gives you the effect of exactly-once. This is what “exactly-once” usually means in practice.
05 Poison messages and the dead-letter queue
Some messages fail every time: a malformed payload, a deleted account, a bug. With unlimited retries a poison message comes back again and again, wasting consumer time forever.
The fix is a cap on attempts. After the last attempt the broker moves the message to a dead-letter queue (DLQ), where a person can look at it, fix the cause and replay it.
- Processed
- 0
- Failed attempts
- 0
- Dead-lettered
- 0
- Waiting
- 0
Click any chip to follow that one message.
Alert when anything lands in the DLQ. A dead-letter queue nobody reads is a slower way of losing messages.
06 More consumers, less order
Adding consumers to a queue scales throughput almost in a straight line, because they compete for the next message. The cost is order. A2 can be picked up just after A1 by a faster consumer and finish first.
- Processed
- 0
- Out of order
- 0
Click any chip to follow that one message.
If order matters for each customer or account but not across them, you want order per key, not one global order.
07 Partitions, offsets and consumer groups
A log-based broker such as Kafka works differently. Messages are appended to partitions, and the key decides which one, so every message for customer A lands in the same partition. Each partition is read by exactly one consumer in a group, in order. The consumer records how far it got by committing an offset.
- Order is kept within a partition, so it is kept per key.
- Parallelism is capped by the number of partitions. Extra consumers sit idle.
- When consumers join or leave, the group rebalances: reading pauses, partitions are reassigned and each one rewinds to its last committed offset. Uncommitted work runs again.
- Messages stay in the log after they are read, so a new group can replay history from the start.
- Processed
- 0
- Out of order
- 0
- Waiting
- 0
Click any chip to follow that one message.
08 One bad message can block a partition
A log cannot skip a message without breaking order, so a consumer that keeps failing on one message stops its whole partition. Everything behind it waits, while the other partitions carry on.
- Waiting
- 0
- Failed attempts
- 0
- Dead-lettered
- 0
Click any chip to follow that one message.
09 Fan-out: publish once, many readers
Often several services care about the same event. An order placed should send an email, update analytics and refresh the search index. With publish/subscribe, the producer publishes once to a topic and each subscription gets its own copy, its own queue and its own pace.
- Processed
- 0
- Waiting
- 0
Click any chip to follow that one message.
The viral feed system uses this at scale: one post, fanned out to every follower’s timeline.
10 When the queue fills up
A queue hides overload until it doesn’t. An unbounded queue keeps accepting work, so the wait grows and grows: by Little’s law, time in queue is roughly depth divided by throughput. A bounded queue rejects new messages when it is full, which tells producers to slow down. That signal is called backpressure.
- Waiting
- 0
- Oldest wait
- 0.0s
- Rejected
- 0
Click any chip to follow that one message.
Pick what happens when you are full: reject and let producers retry, drop the least important work, or add consumers. Autoscaling on queue depth, not CPU, is the usual answer for worker pools.
11 Choosing a broker
Every broker makes the trade-offs above in its own way. A rough map:
| Broker | Model | Order | Replay | Good for |
|---|---|---|---|---|
| Amazon SQS | Queue, visibility timeout, DLQ by redrive policy | Best effort (standard); per message group (FIFO) | No | Background jobs with little to run |
| RabbitMQ | Queues fed by exchanges (direct, topic, fanout), acks, dead-letter exchanges | Per queue, until consumers compete | Only with streams | Routing rules, task queues |
| Apache Kafka | Partitioned log, consumer groups, offsets | Per partition | Yes, within retention | Event streams, high volume, many readers |
| Redis Streams | Log with consumer groups and a pending list | Per stream | Yes, bounded by memory | Lightweight streams next to an existing Redis |
| Google Pub/Sub | Topics and subscriptions, ack deadline | With ordering keys | With seek and retention | Managed fan-out |
Check the current docs before you choose: these products add features every year, and limits differ by plan.
12 Check yourself
A consumer charges a card, then crashes before it acks the message. The queue is at-least-once. What happens?
Jobs take up to 40 seconds. The visibility timeout is 30 seconds. Nothing crashes. What do you see?
All events for one account must be handled in order, and you need ten consumers. What do you do?
A consumer group has 12 consumers reading a topic with 8 partitions. How many consumers do work?
One malformed message fails every time. Retries are unlimited. What goes wrong?