Maps to: CMU 15-440/640 Spring 2026, learning outcomes “distributed transactions” and “consensus: unanimity (two-phase commit)”; the reading Skeen and Stonebraker, A Formal Model of Crash Recovery in a Distributed System. Problem set: PS4 asks you to analyse a transfer routine against ACID, to write a 2PC participant for a remittance service, and to discuss complications of an exchange-rate service. This lecture teaches the concepts with a different scenario, and gives no answers. Project: P4 builds two-phase commit for group photo collages; no project code here. Sources:
- D. Skeen and M. Stonebraker, A Formal Model of Crash Recovery in a Distributed System (IEEE TSE 1983) — the reading;
- J. Gray, Notes on Data Base Operating Systems (1978) — 2PC as it is taught;
- B. Lampson and H. Sturgis, Crash Recovery in a Distributed Data Storage System (1979);
- D. Skeen, Nonblocking Commit Protocols (SIGMOD 1981) — three-phase commit;
- C. Mohan, B. Lindsay and R. Obermarck, Transaction Management in the R* Distributed Database Management System (ACM TODS 1986) — presumed abort and presumed commit;
- P. A. Bernstein, V. Hadzilacos and N. Goodman, Concurrency Control and Recovery in Database Systems (1987), ch. 7;
- J. Gray and L. Lamport, Consensus on Transaction Commit (ACM TODS 2006).
The slides are not public; this is not a transcript. Every number not attributed to a paper comes from
code/lecture18/, run on the author’s machine (Apple M4 Pro, macOS, Python 3.13 and 3.9). All demos are exhaustive or seeded and deterministic. Code:code/lecture18/. Reading time: ~90 minutes, plus ~5 to run the demos.
0. How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- Explain why committing a transaction that spans machines needs unanimity, and why simpler schemes (just do it, majority vote) fail.
- Run two-phase commit by hand: messages, forced log writes, the commit point, and the cost in messages, forced writes and lock time.
- State what each side must log, and when, and show what breaks if a rule is violated.
- Give the timeout and recovery action for every state of the coordinator and of a participant.
- Explain blocking: when it happens, what it costs, when cooperative termination helps, and why it cannot be avoided by local reasoning (Skeen and Stonebraker’s concurrency sets).
- Compare presumed nothing, presumed abort and presumed commit, and the read-only optimization.
- Explain three-phase commit: why it does not block under crash failures, what it costs, and why a network partition breaks it.
0.2 Prerequisites and scope
- Prerequisites:
- Lecture 17: write-ahead logging, the commit rule, strict two-phase locking;
- Lecture 16: commit points and stable storage;
- Lecture 14: failure models, timeouts and failure detectors;
- Lecture 1’s
dsimsimulator.
- Scope: one transaction, a fixed set of participants, crash-recover sites, and a network that may lose messages or partition. Replicating the coordinator (consensus) is Lectures 20–21; concurrent transactions and their locking are Lecture 17’s.
0.3 Section → code map
| Section | Idea | Demo in code/lecture18/ |
|---|---|---|
| §1 | why unanimity | 01_why_unanimity.py |
| §2 | the protocol and its cost | 02_protocol_trace.py |
| §3 | what to log, and when | 04_logging_rules.py |
| §4 | failures, timeouts, recovery | 03_crash_every_step.py |
| §5 | blocking; Skeen and Stonebraker’s model | 05_blocking.py, fsa.py |
| §6 | presumed abort / commit, read-only | 06_presumed_abort.py |
| §7 | three-phase commit | 07_three_phase_commit.py, threepc.py |
0.4 The toy world
A user renames their account. The data lives on three shards of a key-value store:
- P1 (names) claims the new name “ada”, and refuses (votes NO) if it is taken;
- P2 (old names) releases the old name “ada_l”;
- P3 (profiles) updates the display name.
All three must happen, or none. Otherwise the user ends up with two names, or none, or a profile pointing to a name someone else owns. A coordinator C drives the commit.
In the code, every node can crash just before any forced log write or any message send. A crashed node either recovers 200 ms later or never. Messages take 1–3 ms and can be lost. A run is INCONSISTENT if one shard committed and another aborted, and blocked if a live shard is still undecided at the end.
1. Why a distributed commit needs unanimity
1.1 Local atomicity is not enough
Each shard can make its own part atomic and durable, with the logging of Lecture 17. The new problem is to make the shards’ decisions agree. Skeen and Stonebraker state the goal as unanimity: the sites must either all commit or all abort, because a mixed decision leaves an inconsistent database.
The difficulty is that each shard may have its own reason to refuse:
- a constraint only it can check (P1: the name is taken);
- a deadlock in its lock manager;
- a crash before it did its part;
- a user abort.
The paper’s basic assumption is that during the first part of a transaction any site may abort unilaterally. So before anyone commits, everyone must have given up that right.
1.2 Two tempting shortcuts (demo 01, Part A)
- “Just do it”: the coordinator tells every shard to apply its change. Of the 8 patterns of can / can’t for three shards, 6 left the rename half-done: a shard that could not, among shards that did. Even when every shard could, 5 of 6 permanent crashes left it half-applied. For example, the coordinator died after telling P1, and P2 and P3 never heard.
- Majority vote: ask first, commit if most say yes. 3 of the 8 patterns still ended half-applied: the minority that said “no” cannot be forced to commit, because its “no” usually means it cannot.
- Unanimity (2PC): 0 of 8 patterns, and 0 of 19 permanent crashes, ended half-applied. A crash can delay or block the decision, but never split it.
1.3 The atomic commitment problem
Bernstein, Hadzilacos and Goodman state the requirements precisely:
- Agreement: all processes that reach a decision reach the same one.
- Irrevocability: a process cannot reverse its decision.
- Validity: the decision is commit only if all processes voted yes.
- Non-triviality: if there are no failures and all vote yes, the decision is commit.
- Termination: if all failures are repaired and no new failures occur for long enough, every process eventually decides.
Rules 1–3 are safety; 4 rules out “always abort”; 5 is liveness, and it is the one 2PC weakens: it terminates only once failures are repaired.
Atomic commit is a cousin of consensus (Lecture 20), with one twist: the decision is not “any proposed value”. It is a function of everybody’s vote, and a single “no” decides it. That is why it needs everyone, where consensus needs only a majority.
2. Two-phase commit
2.1 The protocol
coordinator each participant
phase 1 PREPARE(T) ------------------------------------> do the work, take the locks;
if it can: force PREPARED, vote YES
<----------------------------------- YES / NO if it cannot: vote NO, abort at once
all YES: force COMMIT (the commit point)
any NO or a timeout: ABORT
phase 2 COMMIT / ABORT ---------------------------------> force the decision, apply or undo,
release the locks
<------------------------------------------ ACK
all ACKs: write END, forget T
Two moments define the protocol:
- A participant’s PREPARED record is a promise. Once it has voted YES, it must be able to commit even if it crashes. So everything needed to commit and to abort must be durable before the vote: the redo and undo information, and the fact that it holds the locks.
- The coordinator’s COMMIT record is the global commit point. Before it, the transaction can still abort; after it, it will commit, however many crashes follow.
2.2 Watching it (demo 02)
0.00 C -> P1 PREPARE(T1) (and to P2, P3)
1.27 P1 force-log PREPARED
1.27 P1 -> C YES(T1)
2.53 P3 force-log PREPARED ... P3 -> C YES
2.69 P2 force-log PREPARED ... P2 -> C YES
4.59 C force-log COMMIT <- the commit point
4.59 C -> P1 COMMIT(T1) (and to P2, P3)
5.78 P3 force-log COMMIT; P3 -> C ACK(T1)
...
9.57 C log END (not forced)
When P1’s check fails, it simply votes NO and aborts on the spot (an unforced ABORT record). It never promised anything. The coordinator aborts as soon as it sees the NO, without waiting for the other votes, and tells only those who might have voted YES.
2.3 What it costs
| case | messages | forced writes |
|---|---|---|
| commit, 3 shards | 12 = 4 × 3 | 7 = 2 × 3 + 1 |
| P1 votes NO | 10 | 5 |
| P3 only reads (votes READ-ONLY) | 10 | 5 |
The real cost is in time and locks. Every shard holds its locks from its PREPARED write until its COMMIT record is forced: a round trip to the coordinator, the wait for the slowest vote, and three forced writes in a row (its PREPARED, the coordinator’s COMMIT, its own COMMIT). In demo 02 that took 5.6 ms on a 1–3 ms network with free forced writes. With 5 ms per forced write, the decision alone is ready only at ~14.6 ms. A local transaction that would hold its locks for microseconds holds them for milliseconds, and contention (Lecture 17 §8.5) follows.
2.4 The read-only optimization
A shard that only read has nothing to commit or undo. It votes READ-ONLY, releases its locks, and leaves the protocol: no PREPARED record, no phase 2. Demo 02 Part C: 10 messages and 5 forced writes instead of 12 and 7.
Releasing read locks at the vote is safe under two-phase locking. The transaction has finished its work, so every participant has reached its lock point. If every participant is read-only, the whole commit is one round of messages with no forced writes at all.
3. What to log, and when
3.1 The rules
| who | writes | forced? | before | why |
|---|---|---|---|---|
| participant | PREPARED (with redo/undo info and its locks) | yes | voting YES | the promise must survive a crash |
| participant | COMMIT / ABORT | yes (presumed nothing) | acknowledging | an ACK lets the coordinator forget |
| coordinator | COMMIT | yes | sending COMMIT | the commit point; recovery must find it |
| coordinator | ABORT | presumed nothing: yes | sending ABORT | (presumed abort: not needed, §6) |
| coordinator | END | no | — | marks the transaction forgettable |
3.2 Breaking them (demo 04)
Each row ran every crash point of every node (with and without recovery) and every single lost message:
| rule broken | inconsistent runs |
|---|---|
| none (correct 2PC) | 0 of 50 |
| a shard sends YES before forcing PREPARED | 3 |
| the coordinator sends COMMIT before forcing it | 2 |
| a shard in doubt gives up after 3 unanswered inquiries | 6 |
| the coordinator forgets an outcome as soon as it is sent | 6 (presumed nothing), 6 (presumed abort), 3 and 7 (presumed commit) |
The counterexamples are worth reading one by one:
- YES before PREPARED. P1 votes YES and crashes before the record is durable. It recovers with no memory of the promise, and aborts. Meanwhile C, having three YES votes, commits P2 and P3.
- COMMIT before forcing it. C tells P1 to commit and crashes before writing COMMIT. On recovery it finds nothing, so it presumes abort. P1 committed; P2 and P3 abort.
- Impatience. C forces COMMIT and crashes for 200 ms. Each shard asks three times, hears nothing, and aborts. C comes back and its log says COMMIT. A prepared participant may never decide alone, however long it waits.
- Forgetting early. C forgets the outcome, P1 crashes before recording it, recovers and asks, and C, having no record, answers with the presumption. This breaks exactly when the forgotten outcome is not the presumed one. Under presumed nothing and presumed abort, all 6 broken runs each were forgotten COMMITs. Under presumed commit, all 3 + 7 were forgotten ABORTs. Some of those came from a crash that made the coordinator time out and abort, which is easy to miss when reasoning by hand.
4. Failures, timeouts and recovery
4.1 Every state has an action
A failure shows up as silence. Each waiting state needs a timeout action, and each log state a recovery action:
| who, where | on timeout | on recovery (from the log) |
|---|---|---|
| coordinator, collecting votes | abort (it may: nobody has decided) | no decision logged → presume abort |
| coordinator, decision sent, ACKs missing | resend the decision | decision logged, no END → resend it |
| participant, before voting | abort unilaterally | nothing logged → it never promised: abort |
| participant, voted YES (in doubt) | ask (the coordinator, or everyone) — but never decide | PREPARED, no decision → in doubt again: re-take the locks, ask |
| participant, decided | nothing | redo the commit if needed (idempotent) |
The row in bold is where 2PC’s weakness lives.
4.2 Every crash point (demo 03)
Each node crashed just before each of its 19 steps, either recovering 200 ms later or never:
- Correctness: 0 inconsistent outcomes in 38 runs.
- With recovery, every run finished: 9 commits, 10 aborts. Crashes before the coordinator’s COMMIT abort; later ones commit. Locks were held up to 244 ms, the time a crashed node stayed down.
- Without recovery, 6 crash points left live shards blocked, and all 6 were coordinator crashes after at least one shard had prepared. A shard that crashes for good never blocks the others; only its own data stays unavailable.
4.3 Lost messages
With no crash, losing any one of the 12 messages never split the outcome:
- A lost phase-1 message (a PREPARE or a vote) turned the commit into an abort in 6 cases: the coordinator timed out after 30 ms, as it may, since nobody had decided yet.
- A lost phase-2 message (a decision or an ACK) was simply resent. The transaction still committed, at up to 16 messages instead of 12.
Retransmission and idempotent handlers (a duplicate COMMIT is re-acknowledged, a duplicate PREPARE re-answered) make message loss a matter of time.
5. Blocking
5.1 The window of uncertainty
A participant that has voted YES and not yet heard the decision is in doubt:
- It cannot commit: the coordinator may have aborted, for instance because another shard voted NO.
- It cannot abort: the coordinator may have committed, and other shards may already have applied it.
If the coordinator is down, the participant blocks, holding its locks, until the coordinator recovers. Skeen and Stonebraker spell out why this matters: the locks of a blocked transaction cannot be released, so its data is inaccessible. The availability of data on reliable sites becomes limited by the weakest component in the system.
In demo 05 Part B the coordinator crashes right after forcing COMMIT, before telling anyone. The outcome was commit every time, but P2 held its lock for as long as the coordinator was down:
| coordinator down for | P2 held its lock | other transactions turned away (50 per second) |
|---|---|---|
| 20 ms | 24 ms | 1 |
| 1 s | 1,005 ms | 50 |
| 10 s | 10,005 ms | 500 |
5.2 Cooperative termination
A blocked participant can ask the other participants instead of only the coordinator:
- if one of them knows the decision, it says so;
- if one of them has not voted yet, it can abort unilaterally, and says so. Then nobody can commit, and the asker may abort too.
Demo 05 Part A crashed the coordinator for good at each of its steps:
| coordinator crashes just before | blocked | blocked with cooperative termination |
|---|---|---|
| sending PREPARE to P2 | P1 | — (P2 and P3 had not voted: abort) |
| sending PREPARE to P3 | P1, P2 | — |
| forcing COMMIT | P1, P2, P3 | P1, P2, P3 |
| sending COMMIT to P1 | P1, P2, P3 | P1, P2, P3 |
| sending COMMIT to P2 | P2, P3 | — (P1 knew: commit) |
| sending COMMIT to P3 | P3 | — |
Cooperation rescued 4 of the 6 cases. In the other two, every live shard had voted YES and none knew the decision; only the dead coordinator did. No amount of talking among the survivors can settle that. The coordinator may have committed (the second row) or not (the first), and the survivors’ states are identical in both.
5.3 Why no local rule can fix this: Skeen and Stonebraker’s model
The reading turns “a site cannot decide alone” into a theorem.
- The model. Each site runs a nondeterministic finite-state automaton: q initial, w wait, p prepared, c commit, a abort. Commit and abort are final and irreversible. A global state is the vector of local states plus the messages in transit. The reachable global-state graph contains every possible execution.
- Operational correctness. No reachable state may hold both a commit and an abort (inconsistent), and every terminal state must be final.
- Concurrency set. The concurrency set C(s) of a local state s is every local state another site can be in at the same time as s. If C(s) contains both a commit and an abort state, a site in s that fails or loses contact cannot pick a final state on its own: whatever it picks, someone may have picked the other.
- Independent recovery. A recovering site that decides from its own log alone is independent recovery. Lemma 1: a protocol with a state whose concurrency set holds both commit and abort is not resilient to even a single failure under independent recovery.
fsa.py builds these graphs exactly (demo 05 Part C):
| protocol | sites | global states | inconsistent | states concurrent with both commit and abort |
|---|---|---|---|---|
| 2PC | 2 | 11 | 0 | slave in w |
| 2PC | 4 | 148 | 0 | every slave in w |
| 2PC + ACK | 2 | 12 | 0 | none |
| 2PC + ACK | 3 | 39 | 0 | every slave in w |
| 3PC | 2–4 | 13–156 | 0 | none |
For 2PC with two sites, the slave’s wait state is concurrent with the coordinator’s a1, c1 and w1, which is exactly the paper’s observation about its prepared state p2. The paper’s repair for two sites is to make the coordinator wait in a prepared state for the slave’s acknowledgement before committing (“2PC + ACK”). It then gives rules for assigning failure and timeout transitions that make a protocol resilient to any single site failure. The fix does not generalize: with a third site, another slave can be in c while this one is in w.
Two theorems follow:
- Theorem 3: two concurrent failures. No protocol using independent recovery is resilient to arbitrary failures by two sites. A recovering site must ask someone, and the survivors must remember the outcome for it.
- Theorem 4: partitions. No protocol is resilient to a network partition when messages are lost. The paper also shows that the “optimistic” case, where undeliverable messages are returned to the sender, is equivalent to independent recovery from a site failure.
The paper also notes that protocols exist in which an operational site never blocks, such as the family of three-phase protocols; recovering sites still have to ask. That is §7.
6. Presumed abort, presumed commit, read-only
6.1 The idea
In basic 2PC (“presumed nothing”), the coordinator must remember both outcomes until every participant has acknowledged them. So it forces both COMMIT and ABORT records and waits for ACKs for both.
Mohan, Lindsay and Obermarck observed that an inquiry about a transaction the coordinator has no record of can simply be answered with a fixed presumption. Then that outcome needs neither a forced record nor acknowledgements.
| variant | coordinator forces | participants force | acknowledged | “no record” means |
|---|---|---|---|---|
| presumed nothing | COMMIT, ABORT | PREPARED, COMMIT, ABORT | both outcomes | abort (it never decided) |
| presumed abort | COMMIT | PREPARED, COMMIT | COMMIT only | abort |
| presumed commit | COLLECTING (before phase 1), COMMIT, ABORT | PREPARED, ABORT | ABORT only | commit |
Presumed commit needs the extra COLLECTING record for a reason. If the coordinator crashes before deciding, “no record” would wrongly mean commit. So it must first record that a transaction is in progress, and on recovery abort it actively.
6.2 What each saves (demo 06)
Three shards:
| case | presumed nothing | presumed abort | presumed commit |
|---|---|---|---|
| commit: forced writes (coordinator + shards), messages | 1 + 6, 12 | 1 + 6, 12 | 2 + 3, 9 |
| abort, all shards prepared | 1 + 6, 12 | 0 + 3, 9 | 2 + 6, 12 |
| abort, P2 votes NO | 1 + 4, 10 | 0 + 2, 8 | 2 + 4, 10 |
| commit, P3 read-only | 1 + 4, 10 | 1 + 4, 10 | 2 + 2, 8 |
These match the counts in the R* paper. All three variants were correct at every crash point and lost message: 0 inconsistent of 90, 78 and 79 runs. Each blocks in the same window when the coordinator dies for good: a presumption changes the bookkeeping, not the blocking.
Pricing a forced write at 10 units and a message at 1:
| commits | presumed nothing | presumed abort | presumed commit |
|---|---|---|---|
| 99% | 82.0 | 81.6 | 59.3 |
| 90% | 82.0 | 77.7 | 62.3 |
| 50% | 82.0 | 60.5 | 75.5 |
Presumed abort is never worse than presumed nothing, and it is what most systems and the X/Open XA standard use. Presumed commit wins when nearly everything commits, by trading one forced COLLECTING record for every shard’s forced COMMIT and ACK.
6.3 The forgetting rule
Demo 04’s “forget early” rows give the rule its sharpest form: the coordinator may forget an outcome before all acknowledgements arrive only if that outcome is the presumed one.
7. Three-phase commit
7.1 The extra phase
Skeen’s three-phase commit (1981) inserts a pre-commit state p between w and c:
- Phase 1. PREPARE; votes; any NO aborts.
- Phase 2. If all voted YES, the coordinator forces PRECOMMIT and sends PRECOMMIT. Participants force P and acknowledge.
- Phase 3. After the acknowledgements (or timeouts: silent sites are taken to have crashed), the coordinator forces COMMIT and sends it.
The point is the concurrency sets:
- No site is ever in w while another is in c, because c requires everyone to have reached p first. A site in w therefore knows nobody committed.
- No site is in p while another is in a, so a site in p knows nobody aborted.
This satisfies Skeen’s fundamental non-blocking theorem: no state is concurrent with both commit and abort, and no noncommittable state is concurrent with a commit state. A state is committable if occupying it implies everyone voted yes. fsa.py confirms both conditions for 2 to 4 sites.
Termination. When survivors lose the coordinator, they elect a backup coordinator: the lowest-named participant that answers. It collects their states and decides:
- any c → commit;
- any a → abort;
- any p → pre-commit the others, then commit;
- all w → abort.
Recovering sites never decide alone; they ask.
7.2 What it buys (demo 07)
| 2PC | 3PC | |
|---|---|---|
| messages, forced writes (commit) | 12, 7 | 15, 11 |
| coordinator commits at | 4.6 ms | 9.6 ms |
| one permanent crash: crash points that leave live shards blocked | 6 of 19 | 0 of 26 |
| coordinator + one shard crash for good: pairs that block | 64 of 84 | 0 of 165 (0 inconsistent) |
Under crash failures with a timely network, the survivors can always decide among themselves.
7.3 What breaks it: partitions
3PC’s timeouts assume “silence means crashed”. A partition makes each side think the other crashed. Demo 07 Part D split the network into {C, P1} | {P2, P3} right after each message of a failure-free run:
| protocol | split points | commit | abort | blocked | INCONSISTENT |
|---|---|---|---|---|---|
| 2PC | 12 | 4 | 1 | 7 | 0 |
| 2PC + cooperative termination | 12 | 5 | 2 | 5 | 0 |
| 3PC | 15 | 8 | 5 | 0 | 2 |
After the split that follows the last YES vote:
- The coordinator’s side. C pre-commits, hears only P1, takes P2 and P3 to have crashed, and commits with P1.
- The other side. P2 and P3 are still in w. They elect P2 as backup coordinator, find no p and no c, and abort.
The outcome is split. 2PC under the same partitions blocks instead, which is safe but unavailable. This is Theorem 4 at work: with partitions that lose messages, a commit protocol can be consistent or non-blocking, not both.
7.4 Why 3PC is rare
- It costs an extra round trip and two extra forced writes per commit, on every transaction, to handle a rare failure.
- It needs a failure detector that is never wrong. Real networks partition and messages are delayed, which is precisely the case it gets wrong.
- The better fix is to make the coordinator fault-tolerant, rather than to let participants decide without it. Gray and Lamport’s Paxos Commit (2006) replicates the coordinator’s decision with consensus. Google’s Spanner runs 2PC where each participant and the coordinator is itself a Paxos-replicated group, so a “crashed coordinator” is replaced within seconds and blocking becomes a short delay. Lectures 20–21 build the consensus pieces.
8. Two-phase commit in practice
- Standards. The X/Open DTP model and its XA interface let a transaction manager run 2PC across databases and message queues from different vendors (presumed abort, “rollback” in XA’s terms). Java’s JTA and many application servers sit on top of it.
- In-doubt transactions and heuristic decisions. When a coordinator is gone for long, an operator may force a blocked participant to commit or abort (a heuristic decision), to free its locks. If the guess contradicts the real outcome, the result is a heuristic mixed outcome: exactly the inconsistency 2PC exists to prevent, now caused by a human, and reported so that someone can repair it.
- Latency and contention. Locks are held for at least a round trip and three forced writes, so distributed transactions on hot data are expensive. Systems shard data to keep most transactions on one machine, use read-only and one-phase optimizations, and choose coordinators close to the participants.
- Alternatives when atomicity can be weaker. Sagas (Lecture 17 §9) commit each step locally and undo with compensating steps; they give up isolation. Idempotent, retried messages with an outbox table give “exactly-once effects” without a global commit. Each trades the all-or-nothing guarantee for availability.
9. Design rules
- Unanimity, decided in one place. Nobody commits before everyone has given up the right to abort, and the decision is a single forced record.
- Log the promise before making it. PREPARED before YES; the decision before announcing it.
- Every waiting state needs a timeout action; every log state needs a recovery action. A prepared participant’s action is to ask, never to guess.
- Make handlers idempotent and retransmit, so that lost and duplicated messages only cost time.
- Forget only what the presumption will answer correctly. Presumed abort is the default for a reason.
- Budget for blocking. Locks held while a coordinator is down are an availability cost. Shorten it with cooperative termination, and remove it by replicating the coordinator, not by guessing.
- Do not trust “silence means crashed” in a network that can partition.
Summary
- Atomic commit needs unanimity: any site may have a private reason to refuse. “Just do it” left 6 of 8 vote patterns half-applied, a majority vote 3; 2PC left none.
- 2PC: PREPARE → forced PREPARED + YES → forced COMMIT (the commit point) → COMMIT → forced COMMIT + ACK → END. With 3 shards that is 12 messages and 7 forced writes. Locks are held across a round trip and three forced writes; read-only shards drop out after phase 1.
- Logging rules: PREPARED before YES, the decision before announcing it, and forget only the presumed outcome early. The crash search found every violation (2–7 inconsistent runs each) and none in the correct protocol (0 of 50).
- Failures: every crash point and every lost message was safe (0 inconsistent). Phase-1 losses abort, phase-2 losses are resent. A coordinator that dies for good after phase 1 blocks every prepared shard, holding locks as long as it is down (10 s down = 10 s of locks).
- Cooperative termination rescued 4 of 6 blocked cases. Not the cases where everyone voted YES and nobody knows the decision.
- Skeen and Stonebraker:
- in 2PC the slave’s wait state is concurrent with both commit and abort, so no independent recovery is possible;
- an extra ACK fixes it for two sites only;
- no protocol with independent recovery survives two concurrent failures;
- no protocol survives partitions that lose messages without blocking.
- Presumed abort saves the coordinator’s forced ABORT record and the abort ACKs (abort with all prepared: 0 + 3 forced writes and 9 messages, against 1 + 6 and 12) and is never worse. Presumed commit is cheapest when nearly everything commits.
- 3PC never blocked under 26 single and 165 double crash cases, at 15 messages, 11 forced writes and twice the latency. A partition split its outcome at 2 of 15 points. In practice the coordinator is replicated with consensus instead.
Common pitfalls
- Voting YES before the PREPARED record is durable.
- Announcing COMMIT before forcing it.
- Letting a prepared participant time out into abort (or commit).
- Forgetting an outcome that is not the presumed one before every participant has acknowledged it.
- Not re-acquiring locks for in-doubt transactions after a participant restarts.
- Treating a timeout as proof of a crash (3PC under partitions).
- Assuming blocking is rare enough to ignore: one coordinator outage holds every in-doubt participant’s locks.
- Using a heuristic decision without recording it, so the inconsistency is never repaired.
- Running 2PC on every transaction, when most could be single-shard.
Exercises
-
Why a vote? A coordinator could skip phase 1 and send COMMIT, telling each participant to abort if it cannot comply. Explain, with the rename example, why this does not give atomicity even without crashes, and what extra property phase 1 establishes.
Answer
If P1 cannot claim "ada" because it is taken, it aborts its part while P2 and P3 commit theirs: the old name is released and the profile updated, but no new name is held. Atomicity is lost with no failure at all. Phase 1 establishes that every participant has done its work, checked its constraints, holds its locks and has durably promised to be able to commit or abort on command. Only after that has every site given up its right to abort unilaterally, so a COMMIT can be obeyed by all. -
Timeline. Four sites: C and P1–P3, presumed abort. P1 and P2 vote YES, P3’s YES is still in flight, and C crashes for good. (a) What do P1, P2 and P3 do? (b) Can cooperative termination finish the transaction? (c) Would it be different if P3’s vote had reached C, C had forced COMMIT, and then crashed?
Answer
(a) All three have voted YES and are in doubt. They time out, ask C and get no answer, and block, holding their locks. (b) With cooperative termination they ask each other, but all three are in doubt and nobody knows a decision, so they stay blocked. Had one of them not yet voted, it could abort and tell the others. (c) Their states would be exactly the same as in (a). C's COMMIT record is invisible to them, so they are still blocked, until C recovers and resends COMMIT. This is why the in-doubt state is unavoidable in 2PC: the participants cannot distinguish (a) from (c). -
Recovery table. Give the recovery action of a 2PC participant (presumed nothing) whose log, after a crash, ends with: (a) nothing about T; (b) PREPARED; (c) PREPARED, COMMIT; (d) PREPARED, ABORT. For each, say whether it may contact anyone.
Answer
(a) It never promised: abort T locally. If the coordinator later asks, answer NO or ABORT; no contact needed. (b) In doubt: re-acquire T's locks, restore its prepared state from the log, and ask the coordinator (and, with cooperative termination, the other participants) until someone knows the outcome. It must not decide. (c) Committed: redo T's updates if they might not have reached the database (idempotent), release locks, and re-send the ACK if the coordinator asks again. (d) Aborted: undo if needed, release locks, ACK if asked. Only (b) depends on others. -
Counting. A transaction has n participants, all of which update data, and commits. Count forced log writes and messages under presumed nothing, presumed abort and presumed commit. Then do the same for an abort decided after all n voted YES.
Answer
Commit: presumed nothing and presumed abort: coordinator 1 forced (COMMIT); participants 2 each (PREPARED, COMMIT), so 2n + 1 in all; messages 4n (PREPARE, YES, COMMIT, ACK). Presumed commit: coordinator 2 (COLLECTING, COMMIT); participants 1 each (PREPARED; COMMIT unforced), so n + 2; messages 3n (no commit ACK). Abort after all YES: presumed nothing: 1 + 2n forced, 4n messages. Presumed abort: 0 forced at the coordinator and n at the participants (PREPARED only); 3n messages (no ABORT ACK). Presumed commit: 2 + 2n forced (COLLECTING, ABORT; PREPARED and ABORT each), 4n messages. These agree with demo 06 for n = 3. -
Read-only and serializability. Under strict 2PL, a participant that only read votes READ-ONLY and releases its shared locks immediately, before the decision. Why does this not break serializability? What would break it?
Answer
When PREPARE arrives, the transaction has finished all its operations at every participant: it will acquire no further locks anywhere, so it is past its global lock point. Releasing locks after the lock point is allowed by two-phase locking, and the reads it did are already fixed. Other transactions that now write those items are serialized after it. It would break if the transaction could still acquire locks after PREPARE was sent, for example if some participant were still executing operations for it, or if the read-only participant could later be asked to do more work for the same transaction. -
Concurrency sets by hand. For two-site 2PC (coordinator states q1, w1, c1, a1; slave q2, w2, c2, a2), list C(w1) and C(w2). Which one makes independent recovery impossible, and what single change to the protocol does the paper propose?
Answer
While the coordinator waits (w1), the slave may not have received the transaction yet (q2), may have voted YES (w2), or may have voted NO and aborted (a2): C(w1) = {q2, w2, a2}. This contains no commit, so a coordinator failing in w1 can safely abort. While the slave waits (w2), the coordinator may still be waiting (w1), may have committed (c1), or may have aborted (a1): C(w2) = {w1, c1, a1}. That contains both commit and abort, so by Lemma 1 the slave cannot recover independently. The paper's fix makes the coordinator enter a prepared state p1 after sending COMMIT, and commit only when the slave acknowledges. Then C(w2) = {w1, p1, a1} (no commit), and failure and timeout transitions can be assigned (Rules 1 and 2) for any single failure. It does not extend to three sites (`fsa.py`). -
3PC under a partition. In 3PC with sites C, P1, P2, P3, suppose C has sent PRECOMMIT to P1 only when the network splits into {C, P1} and {P2, P3}. Describe what each side does and the outcome. Why can’t P2 and P3 detect that this is a partition and not a crash?
Answer
On C's side: C is in p and P1 is in p; C waits for PREACKs, gets only P1's, times out on P2 and P3, treats them as crashed, and commits; P1 commits. On the other side: P2 and P3 are in w and hear nothing. They run termination, elect P2 as backup coordinator, see only w states, and abort. The outcome is split. P2 and P3 cannot tell a crashed C from an unreachable C: in both cases they observe only silence. Skeen and Stonebraker's Theorem 4 shows that no protocol avoids this without sometimes blocking, as 2PC does in this situation. -
The operator’s dilemma. A participant has been in doubt for an hour; its coordinator’s machine will not boot. The operator can commit or abort the participant’s part by hand. (a) What information should they look for first? (b) If they guess wrong, what exactly is inconsistent, and what should the system record? (c) How would you design the system so this never arises?
Answer
(a) Whether any other participant knows the outcome (cooperative termination by hand): if one committed or aborted, follow it. Also whether any participant never voted, which means abort. Also the coordinator's log, if its disk can be read on another machine. (b) Some participants would have committed while this one aborted, or the reverse. For the rename: two users holding the same name, or a released name with no new one. The system must record a "heuristic" decision, flag it as such, and report it when the coordinator returns with the real outcome ("heuristic mixed"), so that the damage is repaired by a compensating action. (c) Replicate the coordinator's decision, for example by running the coordinator as a Paxos- or Raft-replicated group, or by using Paxos Commit. Then "the coordinator is down" lasts only until a new leader is elected, and the decision is never lost with one machine.
Related project and problem set (concepts only)
- Problem Set 4 asks for an ACID analysis of a local transfer routine (Lecture 17 §2, §7–8), for the code of a 2PC participant in a remittance service, and for complications of an exchange service. §2 (the protocol), §3 (what to force and when) and §4 (timeouts and recovery) give the concepts. The scenario here (a cross-shard rename) is different, and the answers are not here.
- Project 4 implements two-phase commit for publishing group collages, with lost messages, node crashes and concurrent commits. §3’s logging rules, §4’s timeout and recovery table, and §5’s blocking analysis apply directly. The project handout also advises aborting everything that had not committed when recovering, which is the “presumed abort” reasoning of §6. No project code here.
Further reading
- D. Skeen and M. Stonebraker, “A Formal Model of Crash Recovery in a Distributed System”, IEEE Transactions on Software Engineering SE-9(3), 1983 — the reading.
- J. Gray, “Notes on Data Base Operating Systems”, in Operating Systems: An Advanced Course, LNCS 60, 1978.
- B. Lampson and H. Sturgis, “Crash Recovery in a Distributed Data Storage System”, Xerox PARC, 1979.
- D. Skeen, “Nonblocking Commit Protocols”, SIGMOD 1981.
- C. Mohan, B. Lindsay and R. Obermarck, “Transaction Management in the R* Distributed Database Management System”, ACM TODS 11(4), 1986.
- P. A. Bernstein, V. Hadzilacos and N. Goodman, Concurrency Control and Recovery in Database Systems, Addison-Wesley, 1987, chapter 7.
- J. Gray and L. Lamport, “Consensus on Transaction Commit”, ACM TODS 31(1), 2006 — Paxos Commit.
- J. C. Corbett et al., “Spanner: Google’s Globally-Distributed Database”, OSDI 2012 — 2PC over Paxos groups.
- H. Garcia-Molina and K. Salem, “Sagas”, SIGMOD 1987.
Preview of Lecture 19 — Replication and one-copy semantics: quorums and voting
2PC makes several machines agree on one transaction, and it blocks when one of them is down. Replication asks the opposite question: how to keep several copies of the same data, so that the data stays available while some copies are down, yet looks like a single copy to its users. Lecture 19 covers:
- primary-backup and read-one/write-all;
- Gifford’s weighted voting, where reads and writes must collect quorums whose sizes are chosen so that every read meets the latest write;
- Herlihy’s generalization of quorums to abstract data types;
- what each configuration gives in availability, computed and simulated.
术语表 (Glossary, English → 中文)
| English | 中文 |
|---|---|
| distributed transaction | 分布式事务 |
| atomic commitment (problem) | 原子提交(问题) |
| commit protocol | 提交协议 |
| coordinator / participant (cohort, slave) | 协调者 / 参与者 |
| two-phase commit (2PC) | 两阶段提交 |
| three-phase commit (3PC) | 三阶段提交 |
| unanimity | 全体一致 |
| unilateral abort | 单方面中止 |
| vote (YES / NO / READ-ONLY) | 投票(同意 / 反对 / 只读) |
| prepare / prepared state | 准备 / 已准备状态 |
| in doubt (uncertain) | 不确定(存疑) |
| commit point | 提交点 |
| decision record / END record | 决定记录 / 结束记录 |
| forced log write | 强制日志写 |
| acknowledgement (ACK) | 确认 |
| inquiry (status request) | 询问(状态查询) |
| timeout action | 超时动作 |
| recovery action | 恢复动作 |
| blocking / non-blocking protocol | 阻塞 / 非阻塞协议 |
| window of uncertainty | 不确定窗口 |
| cooperative termination protocol | 协作终止协议 |
| termination protocol / backup coordinator | 终止协议 / 后备协调者 |
| independent recovery | 独立恢复 |
| finite-state automaton (FSA) | 有限状态自动机 |
| global state / reachable state graph | 全局状态 / 可达状态图 |
| concurrency set | 并发集 |
| sender set | 发送者集 |
| committable / noncommittable state | 可提交 / 不可提交状态 |
| pre-commit state | 预提交状态 |
| presumed nothing / presumed abort / presumed commit | 无假定 / 假定中止 / 假定提交 |
| read-only optimization | 只读优化 |
| network partition | 网络分区 |
| failure detector | 故障检测器 |
| heuristic decision / heuristic mixed outcome | 启发式决定 / 启发式混合结果 |
| X/Open XA interface | X/Open XA 接口 |
| Paxos Commit | Paxos 提交 |
| saga / compensating transaction | Saga 长事务 / 补偿事务 |
| idempotent handler | 幂等处理程序 |
| retransmission | 重传 |