Design a Message Queue
📬 What is a message queue? It is a buffer between the services that produce work and the services that do it. An order service writes "order 17 was placed" and moves on; a billing service and a shipping service each read that message when they are ready. Kafka-style queues keep every message in an append-only log for a while, so many readers can read the same messages at their own pace, and a reader that crashes can pick up where it left off.
This is the durable cousin of pub/sub. In pub/sub, a message is copied into inboxes and is gone once read. Here, messages stay in a log and readers keep a bookmark. That one change brings in the four ideas this problem is really about: partitions, offsets, consumer groups and retention. We also make it safe for many producer and consumer threads at once, which is where most interviewers spend the last part of the hour.
Requirements
"Design a message queue like Kafka. Producers write messages to topics, and consumers read them. Several consumers should be able to share the work."
A good interviewer stops there. "Like Kafka" is a hint, not a spec: you have to find out which Kafka ideas they actually want.
Clarifying Questions
You: When a consumer reads a message, is it removed from the queue?
Interviewer: No. Messages stay until they get too old. Several independent services read the same topic, and each one must see every message.
You: Do messages need to come out in the order they went in?
Interviewer: Not globally. But all messages about the same thing, like the same order id, must be read in order. Producers can attach a key for that.
You: How do several consumers share the work of one service?
Interviewer: They join a consumer group. Each message should be handled by one consumer in the group, not by all of them. Consumers join and leave at any time.
You: What if a consumer crashes after reading a message but before finishing it?
Interviewer: The message must not be lost. Handling a message twice is acceptable; losing it is not.
You: How long do we keep messages?
Interviewer: For a fixed retention period. Older messages are deleted to free space, even if some consumer has not read them.
You: Should the queue read the clock?
Interviewer: No, pass timestamps in. It keeps the tests deterministic.
You: Do producers and consumers run on separate threads? Can a consumer wait for new messages?
Interviewer: Yes to both. Many producer and consumer threads at once, and a consumer with nothing to read should be able to wait instead of spinning.
The second and third answers together are the heart of the design. "Order per key" plus "share the work" is exactly what partitions give you: split the topic into several logs, send each key to one of them, and give each log to one consumer. The fourth answer decides when we save a consumer's progress.
💡 Interview tip: When the prompt says "like Kafka", list the Kafka ideas you plan to model (partitions, offsets, consumer groups, retention) and the ones you will skip (replication, disk storage, transactions). It shows you know the system and keeps the scope honest.
Final Requirements
- A topic is created with a fixed number of partitions. Each partition is an append-only log; each record gets the next offset in its partition, and offsets are never reused.
- A producer sends a record with a key, a value and a timestamp. Records with the same non-empty key always go to the same partition. Records without a key are spread evenly.
- A consumer joins a consumer group for one topic. Within a group, each partition is owned by exactly one consumer; when consumers join or leave, partitions are reassigned (a rebalance).
- A consumer polls for up to N records from its partitions, in offset order within each partition.
- Different groups read the same topic independently: each group has its own progress.
- A consumer commits its progress. After a rebalance, the group continues from the committed offsets, so records that were read but not committed are delivered again (at-least-once).
- Records older than the retention period are deleted. A group that falls behind skips what was deleted.
- The group's lag (records stored but not yet committed) can be queried for monitoring.
- Many producer and consumer threads can use the queue at once, and a consumer can wait for new records.
Out of scope
- Writing to disk and replication across machines (we sketch them in Extensibility).
- Exactly-once processing and transactions.
- Changing a topic's partition count after it is created.
- Network protocol, authentication and quotas.