Unit 5 of 14 · about 4 hours · derive, measure
Hot keys, load skew and scatter-gather tail latency
Uniform hashing balances data, not traffic. One hot key can saturate a shard while the cluster idles, and every cross-shard query waits for the slowest shard.
You'll be able to
- Quantify per-shard load from a key distribution and price a hot-key split as write relief against read fan-out.
- Derive scatter-gather latency from per-shard latency distributions, including the tail.
- Measure skew and tails on your machine with fixed seeds and labeled setups.
Covers
Read
In this order, only the parts listed
0 of 2 done
- Hot spots and the split-key remedyDDIA 2e - Chapters 7 and 8 Sharding and Transactions
Chapter 7, Skewed Workloads and Relieving Hot Spots (pp. 263-264): hot shards and keys, isolating a hot key, the random-digit suffix split with its read and bookkeeping costs
- Why scatter queries amplify tailsDDIA 2e - Chapters 7 and 8 Sharding and Transactions
Chapter 7, Sharding and Secondary Indexes on local indexes (p. 269): scatter to all shards with tail latency amplification and the scaling limit it sets
Focus on
- Skew is about access, not data size. Equal key counts still overload one shard when one key takes most traffic.
- Splitting a hot write key with a random suffix divides writes by the suffix count but multiplies the read fan-out by the same factor.
- With concurrent dispatch, the shard-wait part of scatter-gather latency is the slowest shard's time, plus dispatch and merge. Without added contention, parallel dispatch beats serial calls at every percentile, but waiting for all shards amplifies the tail relative to one shard.
- Read-hot and write-hot keys need different fixes: replicate or cache hot reads, split hot writes.
Common mistakes · 3
- Adding shards to fix a hot key. The key still hashes to one shard; only splitting or replicating the key helps.
- Averaging shard latencies for scatter-gather. The query waits for the slowest shard, so report the maximum and the tail.
- Splitting every key preemptively. Tracking which keys are split costs more than it saves for cold keys.
Try it yourself
Closed-book, before checking anything
[10, 20, 30, 40] over 4 shards ([2, 0, 2, 0]) and name the hot shards. Then take one hot key at 10,000 writes per second, split it across 100 suffix keys, and state the expected write rate per split key with the new read fan-out.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Four shards hold loads
[2, 0, 2, 0]. Is the cluster balanced?AnswerNo. Shards 0 and 2 hold everything while 1 and 3 idle. The skew comes from key choice, not data size.
2.A hot key takes 10,000 writes per second and gets a two-digit suffix split. What is the per-key rate and the read cost?Answer
About 100 writes per second per split key. Each read now fans out to 100 keys and combines the results.
3.Ten shards each show p99 of 20 ms and a query waits for all ten. What is the scatter-gather p99?Answer
Not determined by the p99s alone. If shard times are independent with the same continuous distribution, all ten finish within 20 ms with probability , so the scatter p99 sits near the single-shard p99.9. Perfectly correlated shards would not amplify it at all.
4.A celebrity key is read-hot but rarely written. Split it or replicate it?Answer
Replicate or cache it. Splitting relieves writes but multiplies reads, which is exactly the wrong trade here.
Practice
- AlgoMaster Calculate Hash Shard Load, skewed case
Example 2 ([10, 20, 30, 40], 4) gives
[2, 0, 2, 0]. Extend it with a 100-way hot-key split and report per-key writes with read fan-outThis drills load arithmetic only. Tail-latency measurement runs in the skew measurement unit and the mini-project.
You're done when
You quantify a skewed distribution by hand and can price a split as write relief against read fan-out and a scatter as tail from the maximum.
Only when you can do this without notes.
Leads to
Notes
Resources · 1 for this unit, 16 more in the section
- DDIA 2e - Chapters 7 and 8 Sharding and TransactionsMartin Kleppmann and Chris Riccomini · primary
Read both chapters completely. Build a decision table connecting access pattern, partition key, isolation, atomicity, failure and recovery before implementing either the partition map or workflow.
Also in this section
- A Critique of ANSI SQL Isolation Levels (Berenson et al., 1995)arXiv (Microsoft Research report) · required
Read the phenomena P0 to P3, the snapshot-isolation anomalies A5A (read skew) and A5B (write skew) and the summary tables. Cite these definitions, not the SQL-92 prose they correct.
- Weak Consistency: A Generalized Theory and Optimistic Implementations for Distributed Transactions (Adya, 1999)MIT LCS · support
Read the dependency-graph definitions of G0, G1 (a, b and c) and G2 and the levels PL-1 to PL-3 they define. They are the anomalies Elle checks on recorded histories.
- Serializable Snapshot Isolation in PostgreSQL (Ports and Grittner, 2012)arXiv · support
Read the dangerous-structure rule (two adjacent read-write antidependencies) from Cahill et al. and how PostgreSQL implements and limits it. False positives abort transactions, so measure abort rates.
- Sagas (Garcia-Molina and Salem, 1987)Cornell course copy · required
Read the whole paper: a saga is a sequence of transactions with compensating transactions, and compensation is application-defined. Name the steps whose effects cannot be compensated.
- Spanner: Google's Globally-Distributed Database (Corbett et al., 2012)Google (author copy) · support
Read TrueTime and the concurrency-control section (timestamps, commit wait, read-write and read-only transactions, two-phase commit over Paxos groups). TrueTime needs clock hardware, so emulate bounded uncertainty in simulation.
- Large-scale Incremental Processing Using Distributed Transactions and Notifications (Peng and Dabek, 2010)USENIX OSDI · support
Read the snapshot-isolation transaction protocol over Bigtable (primary lock, timestamp oracle) and notifications. Snapshot isolation still admits write skew.
- CMU 15-445 - Concurrency Control, Logging and RecoveryCarnegie Mellon Database Group · required
Follow Fall 2025 lectures 17 to 22, the transactions homework and the concurrency-control project where available. Reproduce lost update and write skew outside the project before claiming an isolation level.
- PostgreSQL 18 Documentation - Transaction Isolation and Explicit LockingPostgreSQL Global Development Group · required
Read Section 13.2 (Read Committed, Repeatable Read and Serializable with its serialization failures) and 13.3 (row locks and deadlocks). Reproduce write skew under Repeatable Read and its rejection under Serializable in the
postgres:18image. - The Internals of PostgreSQL - Concurrency ControlHironobu Suzuki · support
Read Chapter 5 Sections 5.5 to 5.9 (snapshots, visibility rules, preventing lost updates and serializable snapshot isolation) to see one real engine implement the anomalies and isolation levels you reproduce. Note the date you read it.
- MIT 6.5840 - Distributed Transactions and Lab 5 Sharded KVMIT PDOS · required
Use lecture 11 and Lab 5 for coordinator failure, shard ownership and migration. Treat the lab as the integrated advanced path; a smaller deterministic partition map is sufficient for the compulsory gate if time is constrained.
- DSLabs - Lab 4 Sharded Store and TransactionsUniversity of Washington (Ellis Michael) · support
Labs are Java 21 message-passing state machines tested on an emulated network and by model-checking search tests that print a minimal violating trace; run them with the
run-tests.pyscript and its--laband--partoptions. Lab 4 adds a shard master, shard movement and multi-key transactions across groups with two-phase commit and locking (Part 3): use it for the commit protocol across groups, which MIT Lab 5 does not exercise. - Gossip Glomers - Totally Available TransactionsFly.io and Jepsen · support
Each challenge runs locally under Maelstrom (v0.2.4; the pages were tested on 0.2.3) with its checker and, where the page says so, the partition nemesis; keep the run directory as the history. Do 6a to 6c (read-uncommitted, then read-committed transactions under partition). The 6b page says the checker cannot detect G0 anomalies, so a pass is weaker than its label.
- Building Distributed Systems - The Sharder and The CoordinatorBuild Distributed Systems · optional
Only the first three tasks of each track are free: in The Sharder, a shard controller, consistent hashing and configuration change; in The Coordinator, two-phase commit, coordinator failure and three-phase commit. Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.
- HLD Handbook - Sharding, Distributed Transactions and IdempotencyHLD Handbook · required
Read Part 2 database sharding, then Part 3 distributed transactions, idempotency and consistent hashing. Use Part 5 event patterns only for the chosen workflow design.
- AlgoMaster System Design - Selected Sharding and Transaction ProblemsAlgoMaster · support
Use the sharding, consistent hashing, ACID, saga, idempotency and outbox lessons as vocabulary, and the problems Calculate Hash Shard Load, Count Keys Moved by Resharding, Merge Cross-Shard Top-K Results, Analyze Transaction Serializability, Build a Saga Compensation Order and Consume Outbox Events Idempotently and in Order as drills. The capstone must supply real persistence, fault history and load evidence.
- Replicated Log and Stream Processor - Partition and Workflow SliceRoadmap-Final · support
Add partition routing, ownership epochs, rebalance, one cross-partition invariant and a durable transaction or saga path. Inject stale routes, coordinator death, duplicate steps and failed compensation.