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

Timestamp ordering, optimistic concurrency, MVCC and read-only snapshot coordination

Blocking every reader behind writers stalls read-heavy workloads. Snapshots let reads run lock-free on a consistent past, and optimistic checks move the cost of conflict to commit time.

You'll be able to

  • Contrast timestamp ordering and optimistic validation with pessimistic locking.
  • Decide tuple visibility from snapshots and version chains, and state what the collector may free.
  • Serve read-only snapshots across shards at one agreed timestamp, waiting on each shard until its safe time reaches it.

Covers

Timestamp ordering and optimistic concurrencyMVCC snapshots and garbage collectionRead-only distributed snapshot coordination
1

Read

In this order, only the parts listed

0 of 4 done

  1. Timestamp ordering and multiversioning lecturesCMU 15-445 - Concurrency Control, Logging and Recovery

    Fall 2025 Lectures #19 (Timestamp Ordering Concurrency Control) and #20 (Multi-Version Concurrency Control): basic timestamp ordering, optimistic concurrency and MVCC version storage

  2. Snapshots, visibility and lost-update preventionThe Internals of PostgreSQL - Concurrency Control

    Chapter 5, Sections 5.5 to 5.8 (page read 2026-10-15): transaction snapshots, visibility check rules, hint bits and phantoms in repeatable read, and first-updater-wins; Section 5.9 for the SSI dangerous structure

    Lock modes live in the PostgreSQL locking reference from the previous unit, not here.

  3. MVCC chains with optimistic controlDDIA 2e - Chapters 7 and 8 Sharding and Transactions

    Chapter 8 (pp. 295-299): multiversion chains with inserted_by and deleted_by, visibility and garbage collection; plus Pessimistic versus optimistic concurrency control (pp. 318-319)

  4. Lock-free reads at a safe timestampSpanner: Google's Globally-Distributed Database (Corbett et al., 2012)

    Sections 4.1.3 (Serving Reads at Timestamp) and 4.2.2 (Read-Only Transactions): the safe-time rule, and read-only transactions that pick one timestamp, may wait for safe time to advance, and take no locks

2

Focus on

  • Timestamp ordering stamps operations and rejects late arrivals; optimistic control runs free and validates at commit. In their basic forms, both abort instead of waiting.
  • MVCC keeps old versions tagged with writer IDs and a snapshot decides visibility. The collector frees only versions invisible to every active snapshot.
  • PostgreSQL repeatable read is snapshot isolation: plain reads never wait for row write locks. A second updater of the same row waits for the first; if the first commits, the second fails with a serialization error and retries the whole transaction.
  • A distributed read-only snapshot needs one timestamp valid on all shards. Each shard reading its own latest is not a consistent snapshot.
Common mistakes · 3
  • Freeing a version that one snapshot still sees. Long-running snapshots pin old versions; the collector cannot free what any snapshot reads.
  • Assuming snapshot isolation is serializable. The doctor example commits under snapshots; only SSI checks or locks stop it.
  • Reading each shard at its own latest timestamp and calling it consistent. Cross-shard reads need one agreed timestamp.
3

Try it yourself

Closed-book, before checking anything

Closed-book, draw two row versions of an account (500 marked deleted by transaction 13, 400 inserted by 13) and decide visibility for a snapshot that started before 13 committed. State when the collector may remove the 500 version.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.How is an update stored under MVCC?Answer

    As a delete plus an insert: the old version is marked with the deleter ID and a new version is inserted. Both stay until no snapshot can see the old one.

  2. 2.Two snapshot transactions update the same row. What happens?Answer

    In PostgreSQL repeatable read the second updater waits for the first. If the first commits, the second fails with a concurrent-update serialization error and retries the whole transaction; if the first aborts, the second proceeds.

  3. 3.An analytics snapshot stays open for hours. What is the storage effect?Answer

    It pins every version it can see. The table bloats until the snapshot closes and vacuum frees the dead versions.

  4. 4.How does Spanner serve a read-only transaction without locks?Answer

    It picks one read timestamp for the whole transaction. Each replica serves once its safe time reaches that timestamp, which accounts for applied Paxos writes and unresolved prepared transactions, so reads take no locks but may wait. The result is externally consistent, not stale by design.

5

Practice

  • CMU 15-445 Homework 5, Questions 3 and 4

    Question 3 (optimistic concurrency control) and Question 4 (multi-version concurrency control)

    Project 4 (https://15445.courses.cs.cmu.edu/fall2025/project4/) is optional C++ practice if your earlier BusTub projects already work. The section mini-project covers this: two shards with version selection and reclamation, and one shared read timestamp that waits for each shard's safe time.

You're done when

You decide visibility for a version chain from a snapshot and can say which anomalies your snapshot still admits.

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.

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

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

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

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.

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

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

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

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