Design a distributed message queue
Role: Software Engineer
Category: System Design
Difficulty: hard
Interview Round: Other
Design a distributed message queue. Producer services publish messages to named topics, and consumer services read them asynchronously, so a slow or failed consumer does not block the producer. Scope the requirements first, then walk through the API, how brokers store messages, the overall architecture, and how the system behaves when a broker or a consumer fails.
```hint Storage access pattern
Think about the access pattern a queue's storage sees. Which kind of disk I/O can a broker make cheap, and what file layout follows from it?
```
```hint Where ordering has to hold
One totally ordered stream per topic caps throughput at what a single machine can handle. Ask which messages really need to stay in order relative to each other.
```
```hint Define "committed"
Pin down exactly when a write counts as committed and which replicas must hold it at that moment. Failover and what consumers may read both follow from that definition.
```
### Clarifying Questions to Ask
- What peak publish rate and average message size should the system handle, and how many consumer groups read each topic?
- How long must messages be kept, and should they stay after every consumer has read them?
- Which delivery guarantee do consumers need: at-most-once, at-least-once or exactly-once?
- Must ordering hold across a whole topic, or only among messages that share a key such as a user ID?
- What publish latency and availability targets apply, and must the system survive losing a whole availability zone?
### What a Strong Answer Covers
- Requirements with concrete targets for throughput, latency, retention and delivery guarantees, plus a rough capacity estimate.
- A storage layout on each broker that sustains high write throughput, and how old data is removed.
- How a topic is split for parallelism, how producers find where to send a message, and what ordering guarantee follows.
- When a write is acknowledged, what consumers are allowed to read, and why a broker failure loses no acknowledged message.
- How consumers share a topic, record their progress and recover when one of them dies, and the delivery semantics that result.
### Follow-up Questions
- How would you give exactly-once processing to a service that reads from one topic and writes results to another?
- Retention grows from days to months and local disk becomes the largest cost. What changes in the storage design?
- One tenant's producers start saturating broker disks. How do you protect the other tenants?
- How do you add partitions to a keyed topic without breaking per-key ordering for its consumers?
Overview: Design a distributed message queue where producers publish to topics and consumers read them asynchronously at very high throughput. It tests how you scope requirements, store and partition messages on brokers, keep data safe through broker failures, and coordinate consumer groups with clear delivery guarantees.