Unit 4 of 14 · about 4 hours · explain, implement

Replication-aware placement, online shard movement and ownership epochs

Moving a shard while clients write to it is where data is lost or doubled. Numbered configs order the moves, and freezing the source before the destination serves keeps exactly one owner accepting writes for each key.

You'll be able to

  • Place three replicas of each shard in three failure domains so losing any one domain leaves a majority.
  • Move a shard by freeze, install and delete under numbered configs while unaffected shards keep serving.
  • Recover an interrupted move from stored current and next configurations.

Covers

Replication-aware placementOnline shard movementOwnership epochs during rebalance
1

Read

In this order, only the parts listed

0 of 4 done

  1. Hot spots, splits and who runs rebalancingDDIA 2e - Chapters 7 and 8 Sharding and Transactions

    Chapter 7, Skewed Workloads and Relieving Hot Spots and Operations: Automatic Versus Manual Rebalancing (pp. 263-265): hot keys, shard splits, automatic versus manual control and the cascading-failure warning

  2. Replicas on distinct machines and racksAlgoMaster System Design - Selected Sharding and Transaction Problems

    Section 12, Replication on the Ring: walk clockwise to distinct physical servers, skip virtual nodes of a machine already used, and spread replicas across racks or zones

    For a majority of three replicas to survive any one domain loss, use three independent failure domains.

  3. Freeze, install, delete with numbered configsMIT 6.5840 - Distributed Transactions and Lab 5 Sharded KV

    Lab 5 Sharded KV (Spring 2026), Parts A and B: ChangeConfigTo as FreezeShard, InstallShard and DeleteShard fenced by config Num, InitController recovery from stored current and next configurations, and the Part D note that cross-shard transactions need two-phase commit with two-phase locking

  4. Ownership epochs as the movement contractReplicated Log and Stream Processor - Partition and Workflow Slice

    The partition rebalance with ownership epochs stretch goal with the correctness rule that stale epochs cannot mutate current ownership

2

Focus on

  • Placement is two decisions: which keys form a shard and which failure domains hold its replicas. Correlated failures kill shards stacked on one rack.
  • Lab 5 freezes the moving shard before copying, installs it at the destination, deletes the frozen copy and publishes the new configuration. Writes to the moving shard retry during the pause while other shards keep serving; copying while still serving needs a catch-up or forwarding protocol the lab does not supply.
  • The config number is an ownership epoch for migration: a group that has seen a higher number rejects shard RPCs carrying older ones. Client Get and Put carry no number, so the frozen or deleted shard state is what turns them away.
  • Automatic rebalancing can cascade. A slow node misdiagnosed as dead triggers moves that overload the rest, so DDIA keeps a human in the loop.
Common mistakes · 3
  • Deleting the old copy before the new owner serves. A crash between copy and cutover then loses the shard.
  • Ordering owners by wall-clock time. Epochs must be monotonic numbers from the controller, never timestamps.
  • Moving many shards at once for speed. Parallel moves multiply transfer traffic and stall serving; move, verify, then repeat.
3

Try it yourself

Closed-book, before checking anything

Closed-book, walk a move of one shard from group 1 to group 2: freeze, state copy, install at the new config number, delete. Show what a write to the moving shard gets during the copy (the frozen source rejects it and the clerk retries) and which number the destination checks on install.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A shard keeps 3 replicas on one rack and the rack switch dies. What happens?Answer

    The whole shard goes dark despite 3 copies. Put the three replicas in three failure domains so one correlated failure leaves a majority.

  2. 2.A client with an old config sends a Put for a moved shard to its old owner. What stops it?Answer

    The old group froze and then deleted the shard, so it answers ErrWrongGroup; the clerk queries the controller and retries at the new owner. Migration RPCs carrying an older config number fail the number check.

  3. 3.Why does DDIA warn against fully automatic rebalancing?Answer

    Moves are expensive and failure detection can be wrong. A slow node suspected dead triggers moves that worsen overload into cascading failure.

  4. 4.A controller dies mid-move and a new one starts. What must it recover?Answer

    The stored current and next configurations, so it completes the interrupted move instead of starting a conflicting one.

5

Practice

  • MIT 6.5840 Lab 5 Sharded KV, Parts A and B

    Implement the shardctrler, shardgrp server and clerk in src/shardkv1. Run Part A (make RUN="-run 5A" shardkv, from TestInitQuery5A through the join and leave movement tests) and Part B (controller recovery after failure)

    Optional integrated path if your MIT Labs 2 to 4 already work: complete Parts A and B and keep your solution private. Otherwise the section mini-project covers this: a local freeze, install and delete fixture with controller restart, persisted current and next configs and delayed stale-RPC tests. Part C adds concurrent controller arbitration.

You're done when

You move a shard between two local groups under continuous writes, no write lands on two owners, and a killed and restarted controller resumes the move.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 13 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.

  • 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.

  • 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.

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:18 image.

  • 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.

  • 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.py script and its --lab and --part options. 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.