SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 19
Design a message broker with per-message acknowledgement
Backend teams hand work to each other through queues: resize this image, send this receipt, sync this account. A producer publishes a message with a routing key and moves on; a pool of workers takes messages one at a time, and each message should be handled even if a worker or a broker machine dies halfway through.
Requirements
- Publish a message with a routing key; routing rules (exact key, wildcard pattern or fan-out to all bound queues) decide which queues receive a copy.
- Deliver each message in a queue to one consumer at a time; the consumer acknowledges it when done.
- Redeliver a message whose consumer crashes or does not acknowledge in time.
- Move a message that keeps failing to a dead-letter queue with the reason, where an operator can inspect and replay it.
Constraints and assumptions
- 100,000 messages per second published at peak, averaging 2 KB.
- 5,000 queues; the busiest queue takes 20,000 messages per second and has up to 500 consumers.
- Processing takes anywhere from 100 ms to 10 minutes depending on the queue.
- A message whose publish was confirmed must survive the loss of any one broker node.
- When consumers are down, a queue can build a backlog of 50 million messages.
Out of scope for the first sketch
Long-term retention and replay of already acknowledged messages · Cross-region replication · Exactly-once processing inside consumer code
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a message broker with per-message acknowledgement
Finish your first sketch before answering these. Cross out freely.
- A consumer takes a message and then crashes. When is the message delivered again, and how do you pick that timeout for a queue whose jobs take 100 ms to 10 minutes?
- One malformed message makes every consumer that touches it throw an error. What happens to it, and to the queue?
- The node holding a queue confirms a publish and immediately dies. What guarantees the message is still there?
- Consumers are down for two hours and a queue grows to 50 million messages. What breaks first in the broker, and how do you avoid it?
- A team wants messages for the same customer processed in order, while keeping 500 consumers busy. What do you give up to provide that?
Compare afterwards: worked approaches and reading at sketchbook /questions/message-queue/review