Unit 8 of 14 · about 4 hours · derive, implement

Serializability, strict serializability, two-phase locking and deadlocks

Serializability is the contract every weaker level approximates. Two-phase locking buys it with waiting, and deadlocks are the price. The Systems unit on two-phase locking, timestamp ordering and MVCC taught the mechanisms and conflict graphs. Here the new work is a lock manager with predicate protection and deadlock detection, and the real-time order that strict serializability adds.

You'll be able to

  • Test a history for serializability by finding an equivalent serial order.
  • Implement two-phase locking with shared, exclusive and predicate locks.
  • Detect a deadlock cycle and resolve it by abort with client retry.

Covers

Serializability and strict serializabilityTwo-phase locking and deadlocks
1

Read

In this order, only the parts listed

0 of 4 done

  1. Serial execution, 2PL and predicate locksDDIA 2e - Chapters 7 and 8 Sharding and Transactions

    Chapter 8, Serializability (pp. 308-317): Actual Serial Execution, Two-Phase Locking with shared and exclusive modes and deadlocks, predicate locks for phantoms and materializing conflicts as a last resort

  2. Serializable orders with strong strict 2PLMIT 6.5840 - Distributed Transactions and Lab 5 Sharded KV

    Lecture 11 (Spring 2026), concurrency-control half: the bank-transfer serializable orders, strong strict two-phase locking rules, the T3/T4 deadlock with timeout and wound-wait resolution

  3. Row locks and the deadlock examplePostgreSQL 18 Documentation - Transaction Isolation and Explicit Locking

    Section 13.3, especially 13.3.2 row-level locks (FOR UPDATE and FOR SHARE) and 13.3.4 Deadlocks with the two-accounts update cycle

  4. Serializability versus strict serializabilityDDIA 2e - Chapters 7 and 8 Sharding and Transactions

    Chapter 10, the box Linearizability Versus Serializability (pp. 407-408): serializability allows stale reads, linearizability adds recency, and the combination is strict serializability

2

Focus on

  • Serializable means some serial order explains all outputs and effects; test it by enumerating orders, as the lecture does. Strict serializability adds real time: a transaction that starts after another commits must follow it in that order.
  • Strong strict 2PL acquires before use and holds past commit. Releasing early reopens the interleavings that locks were meant to close.
  • Read-write sharing needs two modes: shared for readers, exclusive for writers. Phantoms need predicate locks, not row locks.
  • Deadlock needs a cycle. Prevention orders lock acquisition; detection aborts a participant. Either way the client retries.
Common mistakes · 3
  • Holding locks to end of statement instead of end of transaction. Early release breaks serializability even with correct acquisition.
  • Locking rows to stop phantoms. No rows exist yet to lock; the fix is a predicate lock or materialized conflict rows.
  • Retrying a deadlock abort in the same order at once. The same cycle can reform; order locks consistently or back off before retry.
3

Try it yourself

Closed-book, before checking anything

Closed-book, run the lecture bank example with x = 10 and y = 10: T1 transfers 1 while T2 audits. List the two serializable outcomes and show two interleavings that are not serializable. Then show the T3/T4 deadlock on get(x) and get(y) and resolve it by aborting one. Finally give a two-transaction history that is serializable but not strictly serializable.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.What are the serializable outcomes of the lecture bank example?Answer

    T1 then T2 prints 11 and 9; T2 then T1 prints 10 and 10. Any other printed pair is not serializable.

  2. 2.T2 releases x after its first read and T1 runs between the two reads. Result and verdict?Answer

    T2 prints 10 and 9, which matches neither serial order. Holding the lock to commit would have blocked T1.

  3. 3.Two bookings check-then-insert concurrently. Why do row locks not help?Answer

    The conflicting rows do not exist yet, so there is nothing to lock. The fix is a predicate lock on the search condition or serializable isolation.

  4. 4.PostgreSQL detects a deadlock. Which transaction aborts?Answer

    Unpredictable by design. The application must retry any transaction that fails with a deadlock, not a chosen one.

5

Practice

  • AlgoMaster Analyze Transaction Serializability

    Implement isSerializable and serialOrder through the conflict graph. The W(x) then R(x) case is serializable with order [1, 2]; the x: 1 to 2 plus y: 2 to 1 case is not

    Drills conflict-serializability only. The section mini-project covers this: a shared and exclusive lock manager with growing and shrinking phases, predicate or range protection, wait-for-cycle detection with abort, and a real-time-order history test.

You're done when

You classify the bank interleavings, build a conflict graph for a new schedule and give a serializable history that strict serializability rejects, all without notes.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 14 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.

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

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.

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

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