Two-Phase Commit
Two-phase commit is the atomic commit protocol that lets a coordinator and a set of participants agree to apply one distributed transaction, or agree to apply none of it. The transaction commits only if every participant votes YES; a single NO aborts everyone, including the participants that already voted YES.
Picture a floor manager asking three clerks whether they can complete their parts of transfer T: each clerk who says yes locks their row and writes the new value in pencil, turning it into ink only on COMMIT or erasing it on ABORT. Once a clerk votes yes, they cannot commit or erase on their own, so if the manager disappears before sending the decision, the clerks hold their locks and wait.
How two-phase commit locks without applying
The topology is a star. The coordinator talks to each participant, and participants never talk to each other. P1, P2, and P3 are three independent resource managers rather than three replicas of one row: P1 owns account A, P2 owns account B, and P3 owns the audit log. That is why a prepared participant cannot poll its peers, and why a majority of YES votes cannot decide the transaction.
Phase 1 is prepare. The coordinator broadcasts PREPARE, and each participant runs a local check. A YES writes a prepare record, holds locks, and keeps the current value on the row with the proposed value beside it. A NO leaves the row unlocked. The coordinator waits for every vote; two YES votes are not a decision, and even after three YES votes arrive, the transaction is not committed until the coordinator records the decision.
Phase 2 is the decision. Unanimous YES produces COMMIT, and any NO produces ABORT, with the same decision sent to every participant. On COMMIT, the proposed value replaces the current value and the lock is released. On ABORT, the proposed value is discarded and the original value stays. A YES voter that later receives ABORT still rolls back, preserving all-or-nothing atomicity.
The blocking window opens at YES and closes when the decision arrives. Inside it, a participant cannot commit locally (another vote may have been NO) and cannot abort locally (the coordinator may already have decided COMMIT). The default run pauses at this state: C is already COMMITTED, P1 is still PREPARED, and A is still 100. If the coordinator fails in that window, the cohort waits with locks held.
Two-phase commit step by step
The default All yes → commit run starts with A = 100, B = 50, and log = []. Transaction T is "debit A $30, credit B $30, write T in the log."
The key proof beats are 7, 9, 10, and 12, showing unanimity, the coordinator decision, the blocking window, and local apply.
- Four idle columns. Coordinator C is IDLE. P1, P2, and P3 are WORKING. The tally strip is C's log, empty, and the notch after slot 3 reads
need ALL YES. - T arrives. C becomes WAITING. No lock is held, and A, B, and the log are unchanged.
- PREPARE to P1. P1 is still WORKING.
- PREPARE to P2. Independent delivery, with zero votes so far.
- PREPARE to P3. All Phase 1 requests are out; no votes have arrived yet.
- P1 votes YES. The local check is
100 - 30 = 70. P1 locks A and shows proposed70in purple while current A stays100. Votes =1 YES / 3. - P2 votes YES. B is
50 -> 80, locked. Votes =2 YES / 3. The notch is not crossed, so C stays WAITING. - P3 votes YES. The log is
[] -> [T], locked. Votes =3 YES / 3. - C decides COMMIT. The third YES completes the vote, so C records COMMIT. No COMMIT message is on the wire yet, and resources stay purple.
- Blocking window. C has COMMIT, while P1 is still PREPARED and A is still 100. P1 cannot commit (other votes are unknown to it) and cannot abort (C may already have decided COMMIT).
- COMMIT to P1. P1 remains PREPARED while the message travels.
- P1 applies. A snaps
100 -> 70, the lock is released, and P1 becomes COMMITTED. Applied =1 / 3. - COMMIT to P2.
- P2 applies. B becomes
80. Applied =2 / 3. - COMMIT to P3.
- P3 applies. The log becomes
[T]. Applied =3 / 3. - Done. A = 70, B = 80, log = [T]. Every participant voted YES before any update applied; two YES votes would not have been enough.
Load One no → abort next. A starts at 20, so P1's debit fails and P1 votes NO. P2 and P3 still vote YES and lock their rows, and C decides ABORT. The tally strip counts resolved rather than applied because the decision reached everyone without applying the transfer. P2 rolls 80 back to 50; the YES voters abort because atomicity requires all-or-nothing agreement rather than a majority.
Load Coordinator crash last. It follows the default run through the COMMIT decision, then C crashes before sending any COMMIT message. Because the strip is C's own log, 3 YES / 3 is no longer readable by P1; P1's view is "I know only my YES; decision not received." P1–P3 stay PREPARED with locks held and wait. This preset omits coordinator recovery to expose the blocking state.
Why two-phase commit waits
Raft can commit on three of five nodes, whereas 2PC cannot commit on two of three participants. The two protocols solve different problems. Raft replicates one log so any majority overlap carries history forward. 2PC applies one transaction across several distinct resources; if the audit log rejects the write, accounts A and B should not change either.
Storing the coordinator's decision in a Raft log removes the coordinator as a single point of failure by replicating the COMMIT record. It does not let two of three resource managers commit while the third voted NO. A Raft majority does not substitute for 2PC unanimity.
That is also why voting YES is costly: the participant promises to accept whichever decision arrives and cannot retract that promise just because the coordinator is late. Timeouts-as-NO, presumed abort, 3PC, and cooperative termination exist to reduce or bound that wait, and lie outside basic 2PC.
When two-phase commit is a fit
Use 2PC when a transaction crosses independently owned resources, requires all-or-nothing updates across them, and can tolerate locked rows if the coordinator fails. The transfer-plus-audit-log scenario illustrates that requirement: changing accounts A and B while leaving the audit log empty leaves the system inconsistent.
Skip 2PC when availability matters more than strict atomicity. Compensating steps (a saga) are a common alternative: each resource commits locally and later failures trigger unwinding steps, which provides a weaker guarantee. Also skip 2PC when the participants are replicas of a single log, which is Raft's job.
Message and lock cost scales linearly with the number of resource managers: one PREPARE, one vote, and one decision per participant, with locks held from YES until the decision arrives. Because PREPARE and vote messages can travel in parallel, larger N adds more messages and locks rather than extra serial rounds. This canvas fixes N at 3 so the contrast between 2 / 3 and ALL stays readable; adding a fourth participant adds one more set of messages and locks rather than a majority shortcut.
Two-phase commit edge cases
- Two YES, one NO. The strip reads
2 YES + 1 NO. C aborts, and the two locked YES voters roll back. - Insufficient funds. Default
A = 100prepares; the abort presetA = 20votes NO without locking. - Decision before apply. After
decideCommit, A is still 100. COMMIT is a message sent to each participant, not an instantaneous global flag. - Idempotent ABORT. P1 already refused locally and still receives ABORT, with nothing to roll back.
- Crash after a private COMMIT. C knows COMMIT, while the participants do not. Applying unilaterally would match this trace by chance and corrupt state if another participant had voted NO.
Two-phase commit common mistakes
- Calling two YES votes a majority commit. With N = 3, two votes can look like a quorum, but the tally notch sits after slot 3 and two votes never cross it.
- Treating a YES as an apply. Purple
70next to100is a prepared value. The current value changes onapplyCommit, not on the vote. - Letting a prepared participant decide locally. After voting YES, a participant cannot commit (a NO may exist) and cannot abort (COMMIT may already be decided).
- Assuming participants can elect a new coordinator. Participants never message each other in this protocol, which is why coordinator loss leaves them blocked.
- Confusing 2PC with Raft. Raft's
3 / 5quorum replicates one log, whereas 2PC'sneed ALL YESrule updates several separate resources together. Swapping the thresholds breaks atomicity, and replicating the coordinator does not change the vote rule.
A Note on Simplification
This lesson fixes one coordinator and three participants and animates one RPC per beat. Decision ACKs, nested transactions, timeouts treated as NO, presumed-abort optimizations, 3PC, cooperative termination, and coordinator recovery are omitted. The crash preset leaves the cohort blocked instead of replaying a decision log. The vote rule and the blocking window follow standard 2PC, while the timing is serialized so both behaviors stay readable.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Premium players need paid access. Every Explain doc is free.
Found a mistake or something unclear?