Case study demo
Distributed message queue
Send events through a Kafka-like queue and watch partitions, consumer groups, replication and delivery.
An interactive demo of how this system behaves. It runs in your browser.
The whole system
How the parts fit together
- Producers — send messages — healthy
- Broker — topic in partitions — healthy
- Follower replicas — copies of the log — healthy
- Consumer group — one reader per part — healthy
- Offsets — where each group is — healthy
- Request/response
- Replication link
- Asynchronous
Producers add messages to the end of a partition, and followers copy them. Each consumer reads in order and saves how far it got, so it can carry on after a crash.
The demo below lets you try one part of this system.
The part you can try here is the lab Kafka: partitions, replication & delivery. It is the same live model.
Design walkthrough: Distributed message queue
A message queue lets one service hand work to another without waiting for it. This walkthrough designs a queue built on a log, like Kafka. Messages are added to the end of a log and kept for a set time.
Requirements
What it must do
- Producers add messages to a named topic.
- Consumers read messages in order, and can read old ones again.
- Several consumers share the work of one topic.
How well it must do it
- No confirmed message is lost when one broker fails.
- Handle hundreds of thousands of messages a second.
- Keep the order of messages that share a key, such as one user's events.
Rough numbers
- 100,000 messages a second at 1 KB each is 100 MB of writes a second.
- Keeping 7 days is 100 MB × 86,400 × 7, about 60 TB, before copies.
- With 3 copies of everything, plan for about 180 TB of disk.
How a request flows
Write pathPublish a message
- Producers
- Broker
- Partition log
- Follower replicas
Producers to Broker
A producer sends a message with a key, such as user:42. The key's hash picks the partition, so one user's messages stay together.
Broker to Partition log
The partition's leader adds the message to the end of its log on disk.
Broker to Follower replicas
Followers copy the new message into their own logs.
Broker to Producers
With acks=all, the leader confirms only after the in-sync followers have the message.
Read pathConsume messages
- Consumer group
- Broker
- Offsets
Consumer group to Broker
A consumer asks for the messages after its last position. That position is called an offset.
Broker to Consumer group
The broker returns the next batch, in order.
Consumer group to Offsets
After it has handled the batch, the consumer saves the new offset.
Offsets to Consumer group
If the consumer crashes, another one takes over the partition and starts from the saved offset.
Scaling it
- Add partitions to raise throughput. Each partition is its own log on some broker.
- Add consumers to a group, up to one per partition. Extra consumers sit idle.
- Send messages in batches and compress them. Adding to the end of a file is the fastest kind of disk write.
What breaks, and what happens then
The leader of a partition crashes
- What users see
- Writes to that partition pause for a few seconds.
- What the design does
- An in-sync follower becomes the leader. It has every confirmed message, so none is lost.
A consumer crashes after its work but before saving its offset
- What users see
- The next consumer receives the same batch again.
- What the design does
- This is at-least-once delivery. Make the work safe to repeat, for example by recording each message id.
A consumer is slower than the producers
- What users see
- It falls further behind, and old messages may be deleted before it reads them.
- What the design does
- Alert on how far behind it is. Add consumers and partitions, or make each message cheaper to handle.
Trade-offs
- Confirm a write after one copy, or after all in-sync copies
- A good default
- All in-sync copies, when losing a message is not acceptable.
- What it costs
- Each write waits for the slowest in-sync follower.
- Few partitions, or many
- A good default
- Enough for the peak throughput and the number of consumers you need.
- What it costs
- More partitions mean more files and a slower recovery after a broker fails.
- Save the offset before the work, or after it
- A good default
- After, so a crash repeats work and never skips it.
- What it costs
- Consumers must cope with duplicates.
Common mistakes
- Expecting order across a whole topic. Order holds only inside one partition.
- Adding more consumers than partitions and expecting more speed.
Questions an interviewer might ask
How do you get exactly-once processing?
You cannot stop a message arriving twice, so make its effect happen once. Store the message id with the result in one transaction, and skip ids you have seen.
What happens to a message that always fails?
After a few retries, move it to a dead letter queue so it does not block the partition. Someone looks at it later.
When would you choose a classic queue such as RabbitMQ?
When each message is a task for one worker and is confirmed on its own. Also when nobody needs to read old messages again.