Back to sketch

SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 19

Design a message broker with per-message acknowledgement

Advanced · 45 min · Queues & streams

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

Done on paper? Compare with the worked approaches