SD-19 · Back to the write-up
Design a Distributed Message Queue
Write each message to a log on 3 brokers before saying yes, lease it to one consumer at a time, and bring it back until someone acks it.
Follow a request
Publish a message
Receive and ack
Consumer crash and redelivery
Broker failover
Backoff and delayed messages
Clients
Front door
Brokers
Storage
ProducerPublishes
ConsumerReceives + acks
GatewayAuth, routing
MetadataRaft, 5 nodes
Leader brokerPartition log
Followers2 other zones
Delay schedulerTiming wheel
Dead lettersAfter 5 tries
Object storageCold backlog
dedupe_id.1 / 7The numbers, at a glance
1 message3 copies on 3 brokers
4,000fsync each
30,000batch 0.5 ms
100,000batch 2 ms
2% redelivered98% first try
A publish, in order
The producer waits for 2 of 3 copies; the third follower catches up without delaying the answer.
Producer
Gateway
Leader
Follower A
Follower B
1 POST batch of 100
2 append
3 fetch from offset
4 fetch from offset
5 batch
6 batch
7 on disk to offset
8 offsets (2 of 3)
9 201
RequestResponseAsync, does not block
How a queue is stored
The decision this question is really about.
publish
INSERT rowUPDATE lease
DELETE on ack
+ Simple and transactional.− Insert, update, delete per message.
publish
list in RAM
crash → gone
+ Fast and simple.− A crash loses what we said was safe.
publish batch
append + group commit2 of 3 copies
lease → ack → drop segment
+ Sequential, batched, durable.− Acks are tracked per message.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
ProducersConsumers
Broker
ProducersConsumers
Leader brokerFollowers
Metadata
ProducersConsumers
Gateway
48 brokersMetadata
ProducersConsumers
GatewayDelay scheduler
48 brokersDead lettersObject storage
Was this walkthrough useful?Your vote tells us what to improve next.