How to read this chapter
Records, indexes, query plans, joins, transactions, isolation, MVCC, locks, recovery, replication, and sharding through a score write. Explain the physical work behind each query and the exact guarantee behind each acknowledged write.
Read each small topic as a separate whiteboard explanation. First draw the four event rows and three user rows from Part I. The index sections rearrange their keys, the joins combine their records, and the transaction sections change their state. Read the paragraph, execute its tiny example, then check that you could redraw the intermediate state yourself.
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 workload arithmetic lives in scripts/system-design/numbers.py; this chapter’s page, join, transaction, and migration traces live in scripts/system-design/database-review-numbers.py. Product-specific behavior is linked to primary documentation. 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.
What is inside
Explain the physical work behind each query and the exact guarantee behind each acknowledged write.
- 01Tables, keys and constraintsSections 1 to 4. We give each stored fact an identity and an enforceable shape.
- 02B-tree, composite and covering indexesSections 5 to 9. We organize an index around the search the reader actually makes.
- 03Query execution and joinsSections 10 to 14. We follow the database's physical work after SQL describes the desired result.
- 04Transactions and isolation levelsSections 15 to 20. We decide which grouped changes can become visible together.
- 05MVCC, locks and deadlocksSections 21 to 24. We explain how a reader sees old committed state while a writer changes it.
- 06WAL, recovery and connection poolsSections 25 to 27. We recover a committed change after memory disappears.
- 07Replication and replication lagSections 28 to 30. We make another database copy useful without overstating what it has seen.
- 08Sharding and consistent hashingSections 31 to 36. We divide ownership only after understanding a working database.
- +Interview prepInterview question bank, graded exercises and worked solutions.