TWO-PHASE COMMIT

All or nothing, but the coordinator can block everyone

L215 MINDISTRIBUTED SYSTEMS
PREDICT

In two-phase commit a coordinator collects a YES or NO vote from each participant, then sends the decision. Both participants voted YES. The coordinator crashes before sending anything. What can a participant do with its locks?

Two-phase commit makes one transaction atomic across several databases. Phase one: the coordinator sends PREPARE; each participant force-writes everything it needs to commit, holds its locks, and votes YES or NO. Phase two: the coordinator logs a decision (COMMIT only if every vote was YES) and sends it. A YES vote is a promise: from that moment the participant will do whatever it is told, and cannot decide alone. If the coordinator dies before the decision arrives, the promise has no one to keep it to, and the locks stay held until the coordinator returns.

▶ SIMULATION
COORDINATOR
state: init
decision (memory): —
decision (log on disk): empty
up
PARTICIPANT P1
state: working
vote: —
locks: none
outcome: —
PARTICIPANT P2
state: working
vote: —
locks: none
outcome: —
t = 0Transaction T1 updates rows on P1 and P2
✓ every participant reaches the same decision✓ no participant holds locks forever
SCHEDULE
01[Coord]Coord: send PREPARE(T1) to P1, P2
02[P1]P1: force-write prepare record, hold row locks, vote YES
03[P2]P2: force-write prepare record, hold row locks, vote YES
04[Coord]Coord: count votes, decide (in memory)
05[Coord]Coord: force-write decision to its log (fsync)
06[Coord]Coord: send decision to P1
07[Coord]Coord: send decision to P2
Start of all yes → commit scenario. 7 steps.
// Coordinator, after collecting votes:
decision = allYes(votes) ? COMMIT : ABORT
send(decision, participants)   // 1. tell everyone
log.write(decision)            // 2. then make it durable

// Participant on PREPARE:
lockRows()
reply(YES)                     // 1. promise
log.write({ vote: YES })       // 2. then remember the promise

// A crash between 1 and 2 (either side) leaves one node
// that acted on a decision nobody else can reconstruct.

Watch the two invariants together. In COORDINATOR CRASH the first one stays green while the second turns red: 2PC is safe (nobody disagrees) and not live (nobody finishes). The FIX scenario crashes a little later, after the decision is logged but before it is sent. The locks are held the same way, and the number to read is the lock hold time when the restarted coordinator answers from its log. Restarting is what ends the wait: even in COORDINATOR CRASH a restart would end it, because a coordinator with no record presumes ABORT, which is safe since nothing was sent. The log is what keeps the answer right once a decision may have gone out: a coordinator that sent COMMIT to P1 must say COMMIT to P2 after a restart.

INJECT A FAULT NOT YET

Crash the coordinator at a chosen point in the clean commit. Find the window in which a crash leaves a participant holding its locks with no decision. Then try the second fault, a crash followed by a restart from the log, at the same points.

GOAL: a participant that voted YES ends with locks held and no decision

FIX IT NOT YET

Both halves of this protocol act before they remember. Fix onPrepare so a participant only replies YES once its vote is on disk and its locks are held, and decide so the coordinator logs the decision before any participant can see it. The grader crashes each side at every single step, lets another transaction try to write the row at every point in onPrepare, and checks what the survivors conclude after recovery.

Mental model:

a YES vote is a promise, and a promise you cannot remember after a crash is worthless, so both sides log before they speak: the participant before it says YES, the coordinator before it says COMMIT. That buys safety: every participant ends with the same decision no matter who crashes when. It does not buy liveness. From its YES until the decision arrives, a participant is a hostage of the coordinator, holding locks for exactly as long as the coordinator is away. The fix for that is not a third phase; it is a coordinator that is itself replicated and comes back fast.

IN PRODUCTION

XA transactions across two databases, PREPARE TRANSACTION in Postgres, Kafka's transactional producer, any saga that was 'temporarily' upgraded to a distributed transaction. The symptom is latency, not errors: a coordinator restart shows up as lock waits everywhere else.

IN INTERVIEWS

“How would you move money between two services atomically?” leads to 2PC; the follow-up is always “what happens if the coordinator dies right here?” Name the window (after a YES vote, before the decision lands), say what each side must have on disk, and say what bounds the blocking: the coordinator's time to recover. Mention that this is why coordinators get replicated, and why many teams prefer idempotent sagas, or a transactional outbox (one local transaction writes the state and an outbox row; a relay publishes it later), to holding locks across services.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: GOSSIP PROTOCOL →