Highlights

Contents

SYSTEM DESIGN / UNIT 3 / FIELD GUIDERelationaldatabasesRelational databaseskeyrowsindexrowsFacts become pagesKeys choose an access path; the engine preserves the resultRead the mechanism. Follow its state.Test what happens when the path breaks.MECHANISMS, TRACES AND INTERVIEW PRACTICE
Before we start

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.

The running system

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 DD 8,640,000 Baseline request demand
Peak multiplier pp 10 Peak relative to daily average
Mean response time at peak WW 0.2 s Mean in-flight work
API response size bb 2,000 bytes Payload bandwidth
Stored record size rr 500 bytes An illustrative write workload
Record retention tt 30 days Storage calculation
Live events ee 20 per second Fan-out input
Live viewers vv 50,000 Fan-out recipients
Event payload mm 200 bytes Live payload bandwidth
Gateway capacity assumption gg 10,000 connections A sizing exercise, not a benchmark
Replication factor NN 3 Copies of a logical record
Rate policy LL 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.

The map

What is inside

Explain the physical work behind each query and the exact guarantee behind each acknowledged write.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Tables, keys andconstraints2B-tree, composite andcovering indexes3Query execution and joins4Transactions andisolation levels5MVCC, locks and deadlocks6WAL, recovery andconnection pools7Replication andreplication lag8Sharding and consistenthashing
  1. 01Tables, keys and constraintsSections 1 to 4. We give each stored fact an identity and an enforceable shape.
  2. 02B-tree, composite and covering indexesSections 5 to 9. We organize an index around the search the reader actually makes.
  3. 03Query execution and joinsSections 10 to 14. We follow the database's physical work after SQL describes the desired result.
  4. 04Transactions and isolation levelsSections 15 to 20. We decide which grouped changes can become visible together.
  5. 05MVCC, locks and deadlocksSections 21 to 24. We explain how a reader sees old committed state while a writer changes it.
  6. 06WAL, recovery and connection poolsSections 25 to 27. We recover a committed change after memory disappears.
  7. 07Replication and replication lagSections 28 to 30. We make another database copy useful without overstating what it has seen.
  8. 08Sharding and consistent hashingSections 31 to 36. We divide ownership only after understanding a working database.
  9. +Interview prepInterview question bank, graded exercises and worked solutions.