Skip to content
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

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
1prod → gw · POST messagesThe client library sends a batch of 100 messages it gathered in 5 ms, each with a dedupe_id.1 / 7

The numbers, at a glance

Each message is written……3 times before it counts as one
1 message3 copies on 3 brokers
What one fsync buysAppends a second per broker
Redeliveries are rareShare of deliveries that are a second try
Rare, but never zero, so consumers must be idempotent.

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.

Database table
Fine when small
publish
INSERT rowUPDATE lease
DELETE on ack
+ Simple and transactional.− Insert, update, delete per message.
In memory
Loses messages
publish
list in RAM
crash → gone
+ Fast and simple.− A crash loses what we said was safe.
Replicated log + acks
Our pick
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.

Stage 1 · ≈ 2,000 msg/sMake it work
ProducersConsumers
Broker
One broker, one log, an fsync per message.
Stage 2 · ≈ 30,000 msg/sNever lose one
ProducersConsumers
Leader brokerFollowers
Metadata
Group commit, 3 copies, a Raft metadata service.
Stage 3 · ≈ 1,000,000 msg/sSpread the load
ProducersConsumers
Gateway
48 brokersMetadata
Partitions over 48 brokers, a gateway in front.
Stage 4 · 1,000,000 msg/s + bad daysHandle bad days
ProducersConsumers
GatewayDelay scheduler
48 brokersDead lettersObject storage
Dead letters, delays, a cold tier for backlog.
Was this walkthrough useful?Your vote tells us what to improve next.