TWO-PHASE COMMIT
All or nothing, but the coordinator can block everyone
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.
// 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.// Coordinator, after collecting votes:
decision = allYes(votes) ? COMMIT : ABORT
log.write(decision) // durable FIRST (fsync)
send(decision, participants) // then tell everyone
// on restart: decision = log.read() ?? ABORT, resend
// Participant on PREPARE:
lockRows()
log.write({ vote: YES }) // durable FIRST
reply(YES) // then promise
// on restart with a YES on disk: hold locks, ask the
// coordinator, do exactly what it says. 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.