How to read this chapter
Message queues, pub/sub, event streams, consumer groups, partitions, offsets, acknowledgements, retries, dead-letter queues, delivery semantics, backpressure and the transactional outbox. By the end you can draw request, service, queue, workers, database, and name every crash window between accepting a job and finishing it.
Read each small topic as a separate whiteboard explanation. Start with the concrete failure, follow the mechanism, and check the worked example before looking at the picture. A paragraph introduces one idea; the next picture shows the state or message that changes. You should be able to redraw it and say where it can fail.
The orange map marks the part you are reading. Picture this boxes give a physical analogy, notes explain a useful variant, warnings name a mistake, and interview boxes give an answer you can say aloud. Each part closes with In one breath. The interview page has questions, graded exercises, and worked answers; use it after reading, with the diagrams hidden.
These are design lessons, not measured capacity claims. All Heron workloads, durations, limits, and record sizes are illustrative assumptions. The shared arithmetic lives in scripts/system-design/numbers.py and this unit’s queue arithmetic in scripts/system-design/queues-numbers.py; real product behavior is linked to its primary documentation. A decimal MB is a million bytes; a GiB is a power-of-two unit. Capacity still needs a load test on your workload.
Meet Heron
Heron is an illustrative live-score and media service. A scorer changes a match, viewers read its score, subscribers receive updates, and users upload clips. We keep the workload fixed across the series so a faster path, a replica, or a cache changes the same calculation instead of quietly changing the question.
| Assumed setting | Symbol | Value | What it controls |
|---|---|---|---|
| Daily API requests | 8,640,000 | Baseline request demand | |
| Peak multiplier | 10 | Peak relative to daily average | |
| Mean response time at peak | 0.2 s | Mean in-flight work | |
| API response size | 2,000 bytes | Payload bandwidth | |
| Stored record size | 500 bytes | An illustrative write workload | |
| Record retention | 30 days | Storage calculation | |
| Live events | 20 per second | Fan-out input | |
| Live viewers | 50,000 | Fan-out recipients | |
| Event payload | 200 bytes | Live payload bandwidth | |
| Gateway capacity assumption | 10,000 connections | A sizing exercise, not a benchmark | |
| Replication factor | 3 | Copies of a logical record | |
| Rate policy | 100 requests per user per minute | Admission rule |
The computed baseline is 100 requests per second on average and 1,000 at peak. At the assumed mean latency, 200 requests are in flight. Live fan-out is 1,000,000 deliveries per second, carrying 200,000,000 payload bytes per second before protocol overhead. These are different workloads; never size the live path using the API request rate.
This unit adds one asynchronous workload on top of the shared table. Heron transcodes uploaded clips, and publishes score events to three independent readers.
| Assumed queue setting | Value | What it controls |
|---|---|---|
| Clip uploads at peak | 6 per second | Job arrival rate |
| Transcode time per clip | 30 s | Worker service time |
| Transcoding workers | 200 | Service rate of 6.67 jobs per second |
| Final-whistle burst | 40 per second for 120 s | Backlog example |
| Score topic partitions | 12 | Consumer group parallelism |
| Score subscriptions | 3 | Pub/sub fan-out |
| Stream retention | 7 days | Replay window and disk |
At peak the pool runs at 90% utilization. The burst leaves a backlog of 4,000 jobs that takes 100 minutes to drain with 200 workers and about 17 minutes with 300.
What is inside
Trace every crash window between accepting a job and finishing its business effect.
- 01Why message queuesSections 1 to 4. A queue lets a service promise work now and finish it later.
- 02Queues, pub/sub and event streamsSections 5 to 9. Three topologies hide behind the word "queue", and they answer different questions about who receives a message and when it disappears.
- 03Consumer groups, partitions and offsetsSections 10 to 15. A stream needs parallel readers without losing the order that some messages depend on.
- 04Acknowledgements and delivery semanticsSections 16 to 19. An acknowledgement is the consumer telling the broker that a message's work is safe, and its timing decides whether a crash loses work or repeats it.
- 05Retries and dead-letter queuesSections 20 to 23. Some failed messages succeed on the next try and some will fail forever, and treating the two the same either drops recoverable work or jams the queue.
- 06Exactly-once and backpressureSections 24 to 28. Exactly-once and backpressure are the two topics where a short slogan hides the real mechanism.
- 07Transactional outbox and complete flowSections 29 to 33. The last gap is at the very start, where a service must both save its own state and publish a message.
- +Interview prepInterview question bank, graded exercises and worked solutions.