Distributed Message Queue
Ch. 20 Case Studies System designThe problem
A message queue is the component that lets two parts of a system stop knowing about each other. The producer writes a message and moves on; the consumer reads it whenever it is ready; and the broker in between is the shock absorber that keeps a burst of writes from becoming a burst of failures. That decoupling — in time, in rate, and in topology — is the entire reason the component exists.
But the word 'distributed' means the broker is itself a cluster, and that is where the real design lives. A single queue cannot be scaled out, so the queue is cut into partitions, each one ordered and independently scalable. A single consumer cannot keep up, so consumers join a group and split the partitions. A message that is lost or misprocessed is a correctness problem, so the broker acknowledges only when the consumer has confirmed, and retries a bounded number of times before giving the message a home where it can be inspected. We will build the core decoupling, the delivery guarantees, the partitioning, the consumer groups, the retention, and the dead-letter path, then close the loop with one message that is published, delivered, retried, and finally lands in the dead-letter queue — and we know exactly which guarantee each step exercised.
Requirements
Functional — what it must do
- Publish: a producer writes a message to a topic and is acknowledged, without knowing who will read it or when.
- Consume: a consumer reads messages from a topic in order and processes them, acknowledging each one when done.
- Isolate failures: a message that cannot be processed is retried a bounded number of times, then moved to a dead-letter queue for inspection — it is never silently dropped.
Non-functional — how well it must do it
- Delivery is at-least-once: the broker does not acknowledge a producer until the message is durable, and does not let a consumer skip it, even if the consumer crashes mid-processing.
- Per-partition ordering: messages to the same key arrive in the order they were published, so a stream of state changes is applied in the right sequence.
- The system scales out in both directions: more partitions raise the throughput ceiling, and more consumers in a group raise the processing ceiling.
Scale — how much it must take
- On the order of hundreds of thousands of messages per second at peak, across many topics, with retention measured in hours to days so failed consumers can replay.
Assumptions — the numbers this design is sized for
A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.
Takeaways
- A queue decouples producer and consumer in time, rate, and topology; the broker is a durable store, not a pass-through, and that durability is what makes replay and dedupe possible.
- Delivery is at-least-once: the broker acks on durable write, the consumer acks on processing, and a crash in between redelivers — so the consumer must be idempotent or dedupe on the message id.
- Ordering is per key, not global: the key hashes to one partition, so all messages for the same entity are ordered, while different keys may interleave across partitions for parallelism.
- A consumer group splits the partitions among its members, each partition owned by exactly one consumer, so order holds even with many consumers — and the scaling ceiling is the number of partitions.
- Retention keeps consumed messages for a window, so a broken consumer is fixed and replayed from an offset rather than the work being re-triggered; the window is long enough to recover, bounded enough to store.
- A message that fails a bounded number of retries is moved to a dead-letter queue — out of the main flow so it cannot clog the partition, and preserved so an operator can inspect and re-publish it deliberately.
What a queue buys
The queue is a buffer between a producer and a consumer that do not need to be running at the same time, at the same rate, or on the same machine. Fixing that framing makes the rest a durability and ordering problem.
- 1Decoupled in three ways
A producer and a consumer are coupled by a direct call when the producer cannot finish until the consumer answers. A queue breaks that in three ways: in time, because the message outlives the call; in rate, because the buffer absorbs a burst; and in topology, because the producer does not need to know the consumer's address.
- 2The message is the unit
The unit of work is a message: an opaque payload with a topic, a key, and an offset. The producer does not know what the payload means; the consumer does. The topic routes it to the right stream, the key routes it to the right partition, and the offset orders it within that partition.
- 3The broker is the source of truth
The broker is not a pass-through; it is the durable store. A message is not delivered until it is written to the broker's log, and it is not acknowledged until it is durable. That makes the broker the single source of truth for what has been published, which is what makes replay, exactly-once-in-effect, and multi-consumer all possible later.
At-least-once, with an ack
The delivery guarantee is 'at least once', enforced by an acknowledgment that only happens after the consumer has confirmed processing. A crash in between is what makes the guarantee 'at least' rather than 'exactly'.
- 1Publish, then the broker acks
The producer writes the message and waits. The broker writes it to the log, replicates it within its partition, and only then acknowledges the producer. The producer's write is durable the moment the ack arrives — a broker crash before the ack would fail the write, not lose it.
- 2The consumer's ack is the real one
The broker's ack only says 'stored'. The message is not delivered until the consumer processes it and sends its own ack. That consumer ack is what advances the partition's offset — the point from which the next consumer will read. Until it arrives, the message is still live.
- 3A crash makes it 'at least'
If the consumer processes the message and crashes before sending its ack, the offset has not advanced, so the next consumer will read the same message again. The message was delivered twice. That is exactly what 'at-least-once' means, and it is why the consumer must be idempotent — or dedupe on the message id.
Partition the queue, keep the order
A single queue cannot scale out, so the topic is cut into partitions. Ordering is preserved within a partition, and a key guarantees that all messages for the same entity land in the same partition — so order holds where it matters.
- 1One queue is a single lane
A single partition is a single lane: strictly ordered, but bounded by the speed of one writer and one reader. To raise the throughput ceiling, the topic is cut into several partitions, each with its own offset sequence and its own consumers. The ceiling is now the sum of the partitions.
- 2The key picks the lane
The router hashes the message's key and sends it to the partition that owns it. All messages for the same key — the same order, the same customer — land in the same partition, so they are processed in the order they were published. Messages for different keys may interleave across partitions; that is the trade for parallelism.
- 3Ordering is per partition, not global
Because different keys can be in different partitions, there is no single global order across the topic — that is unachievable at this scale and usually not what is wanted. What is guaranteed is the weaker, useful property: for any one key, its messages are ordered. A checkout's 'create, pay, confirm' stays in sequence; two different checkouts may interleave.
Consumer groups and scaling
A single consumer is a bottleneck, so consumers join a group. The group splits the partitions among its members, so adding a consumer adds processing capacity — up to the number of partitions.
- 1A group shares the topic
A consumer group is a set of consumers that together read a topic. The broker assigns each partition to exactly one member of the group, so a message is processed by exactly one consumer in the group. Two different groups can read the same topic independently — that is how one stream feeds many downstream systems.
- 2Each partition has one owner
Within a group, a partition is owned by exactly one consumer. That owner does all the processing for that partition and sends the acks that advance its offset. The other members of the group never touch that partition. This is what keeps the per-partition ordering intact even with many consumers — order is preserved because only one hand is on the wheel.
- 3Scale by adding a consumer, up to a limit
Adding a consumer to the group reassigns the partitions so the load is spread, raising the processing ceiling. But the ceiling is the number of partitions: a group cannot process faster than its partitions can feed it, because each partition has one owner. To raise the ceiling further, add partitions first, then consumers.
Retention and replay
Messages are kept after they are consumed, for a retention window. That is what lets a broken consumer be fixed and replayed from a known offset, rather than the work being re-triggered and possibly double-processed.
- 1A message is not deleted on read
Unlike a classical queue, a consumed message is not deleted the moment it is read. It stays in the partition's log for the retention window — hours, or days. The offset marks where a consumer has gotten to, but the data behind it is still there, addressable by offset.
- 2Replay from an offset
A consumer that was broken can be fixed and told to start from an earlier offset — the one from before the bug. It re-reads the messages it already processed, this time correctly. That is replay: the work is redone from the durable log, not re-triggered by the producer, and the producer never knew anything was wrong.
- 3The retention window is the safety net
The retention window is how long that safety net lasts. It is long enough that a broken consumer can be found, fixed, and replayed — but short enough that the log does not grow without bound. It is a deliberate balance: long enough to recover, bounded enough to store.
When a message will not be processed
A message that fails a bounded number of times is not retried forever. It is moved to a dead-letter queue, where it sits out of the main flow and can be inspected, fixed, and re-published deliberately.
- 1Retries are bounded
When a consumer fails to process a message, the broker does not retry it instantly — that would just fail again. It re-offers it after a back-off, a bounded number of times. Each retry is a chance for a transient failure to clear, but the count is capped so a poison message cannot clog the partition forever.
- 2The dead-letter queue
After the retries are exhausted, the message is moved to a dead-letter queue. It is out of the main flow — it no longer blocks the partition or its consumer — but it is preserved, with its original key and the reason it failed. A dead letter is a message that the system has decided it cannot process, parked where a human can look at it.
- 3The whole guarantee, in one line
Put the pieces together: publish is durable, consume is at-least-once, order is per key, scale is per partition, and failure is isolated to a dead letter. None of these is a feature of a single component — they are the properties that fall out of treating the broker as a durable, partitioned, acknowledged log.
End to end
One message's life: published durably, partitioned by its key, delivered at-least-once, retried on failure, and — if it is a poison — parked in the dead-letter queue out of the main flow.
- 1The whole system
Every piece in one frame. Producers publish to topics; the broker stores them in partitions, each ordered by key; consumer groups split the partitions and process them; failures go to a dead-letter queue; and the downstream systems read from the stream they care about.
- 2Published, partitioned, delivered
A producer writes a message with a key. The broker hashes the key to a partition, writes it to that partition's log, and acknowledges the producer. A consumer in the group that owns that partition reads it, processes it, and acks — the offset advances, and the downstream system sees the result.
- 3A poison message, retried then parked
A message that is malformed fails processing. The broker re-offers it a bounded number of times with a back-off. When the retries are exhausted, it is moved to the dead-letter queue — out of the main flow, so it no longer blocks the partition, and preserved, so an operator can inspect and fix it.
- 4Durable, ordered, and recoverable
The one message that landed is durable — it survived the broker, so a crash did not lose it. It was ordered — its key kept it in one partition, in sequence. And it is recoverable — if the consumer was broken, the retention window lets it be replayed from an offset. Every guarantee in the design was exercised by that single message.
In the wild
Distributed message queues are the connective tissue of most large backends. The boxes below are anchored to the classes of well-known systems that run each piece — an illustrative mapping, not a claim about any one product's internals.
- 1The pattern, piece by piece
Each box is a standard building block of a distributed queue. Anchoring them to the classes of production systems that run that piece turns the abstract design into 'the shape behind the streams that wire your backend together'.
- 2Why the same log keeps winning
Independent stream platforms converge on the same pieces because each one removes a real failure: the durable log removes the lost-message failure, the partition removes the single-lane ceiling, the consumer group removes the single-consumer bottleneck, the ack removes the skip-a-message failure, and the dead-letter removes the poison-message clog. That convergence is the evidence the design is load-bearing.