Design a Distributed Message Queue
Understanding the Problem
📬 What is a distributed message queue? A message queue sits between services that produce work and services that do it. Producers append messages, the queue stores them durably, and consumers read them at their own pace. "Distributed" means the queue itself runs on many machines, so it can absorb far more traffic than one server could and keep working when some of those servers die.
This question comes in two flavors that sound different and turn out to be the same problem. One is "design SQS": create a queue, send a message, receive it, delete it when you're done, and get it redelivered if you crash. The other is "design Kafka": topics split into partitions, replicated across brokers, read by consumer groups that track their own offsets. Underneath, both need a durable, replicated, partitioned store of messages and a way to hand those messages to consumers. We'll build the log-based core first, because it's the harder part to get right, then layer queue semantics (per-message acks, visibility timeouts, delays, FIFO groups) on top. Every one of those features shows up in the requirements below.
Functional Requirements
Start by agreeing on the operations. A message queue has a small API, which makes it tempting to rush. Don't: the interesting questions all hide in what "receive" and "delete" mean.
Core Requirements
- Users should be able to create and delete queues, choosing the number of partitions, the retention period and whether the queue is FIFO.
- Producers should be able to send messages to a queue, optionally with a key (for ordering) and a delay.
- Consumers should be able to receive messages and delete (acknowledge) them once processed. A message that isn't deleted in time should be delivered again.
- Several independent consumer groups should be able to read the same queue, each seeing every message (publish/subscribe).
Below the line (out of scope)
- Message filtering and routing rules (exchanges, header matching).
- Cross-region replication of queues.
- A schema registry and message transformation.
- Billing and per-tenant quotas beyond basic rate limits.
Non-Functional Requirements
Ask your interviewer what scale you're designing for. We'll assume a shared, multi-tenant service of the kind a large company runs internally: 1 million messages per second written at peak across all queues, averaging 1 KB each, with on average 3 consumer groups per queue and 7 days of retention.
Core Requirements
- The system must be durable: once a send is acknowledged, the message survives the loss of any single machine.
- The system should be highly available for writes and reads (99.99%), including during broker failures and deploys.
- The system should sustain 1M messages per second in and about 3M out, and scale horizontally beyond that.
- Publish latency should be low: p99 under about 20 ms with full replication.
- Delivery is at least once by default. FIFO queues keep order per key and drop duplicate sends inside a short window.
Below the line (out of scope)
- Exactly-once processing end to end. That needs the consumer's side effects to be idempotent or transactional, which is the consumer's job.
- Global ordering across a whole queue. We order per partition or per key, never globally.
Now the numbers. Inbound is 1M × 1 KB = 1 GB/s. With 3x replication, the cluster writes 3 GB/s to disk. Outbound is 3 consumer groups × 1 GB/s = 3 GB/s of reads, plus 2 GB/s of follower replication traffic between brokers. Seven days of retention is 1 GB/s × 86,400 × 7 ≈ 605 TB of data, or about 1.8 PB on disk after replication.
If a broker can comfortably sustain on the order of 300 MB/s of disk writes and has ~40 TB of usable disk, we need roughly 3 GB/s ÷ 300 MB/s = 10 brokers for throughput but 1.8 PB ÷ 40 TB ≈ 45 brokers for storage. Storage, not throughput, sizes this cluster. That pushes us toward an append-only log on cheap disks (sequential I/O, no indexes per message) and toward offloading old segments to object storage if retention grows.
Notice what the math rules out. A design that stores each message as a row in a database, with an index and a status column updated on every receive, would need millions of random writes per second. The log design needs a handful of large sequential appends per broker per millisecond. That is the core reason queues at this scale are built on logs.