Design a Distributed Message Queue
Design a message queue that services use to hand work to each other. A producer publishes a message to a named queue, such as orders.created, and gets an answer only once the message is safe. Consumers receive messages, process them and acknowledge them; a message that is never acknowledged comes back for another try. A queue can also be bound to a topic, so one publish is copied into several queues.
The cluster carries 30 billion messages a day, about 1 million a second at peak, across 50,000 queues owned by hundreds of teams. Messages average 1 KB and can be up to 256 KB. The hard parts: a message the producer was told is saved must never be lost, even when a machine dies; every message must reach a consumer at least once, even when consumers crash halfway; and one team’s busy queue or broken consumer must not slow everyone else down.
Good questions to ask first
Decide what you would assume for each before you look at ours.
- 1Queue or replayable stream?
- 2What delivery guarantee?
- 3Do messages need to stay in order?
- 4How big are messages?
- 5How long do we keep a message?
- 6Push or pull?
- 7Delayed messages?
- 8How many queues and how busy?
How to spend the 45 minutes
- 5minPart 1: Requirements and delivery guarantees
- 5minPart 2: Estimates
- 5minPart 3: API
- 15minPart 4: Architecture, stage by stage
- 15minPart 5: Deep dives and trade-offs