Distributed systems / Two-phase commit

Two-phase commit

How a change that spans two databases can be made all-or-nothing: prepare, vote, then commit or abort together. Seven steps follow a $100 transfer, including the case where it gets stuck.

Intermediate7 steps~7 min
App — asks for the $100 transfer
Coordinator — collects the votes and decides
Database — a participant: prepares and votes
// THE SHORT VERSION

In 30 seconds

Two-phase commit (2PC) makes a change that spans several databases happen everywhere or nowhere. A coordinator runs it.

In phase one it asks every database to prepare and vote. In phase two it tells them all to commit if every vote was yes, or all to abort if any vote was no.

The weakness is blocking: if the coordinator fails after the votes, the databases must wait with their rows locked. Many systems therefore use sagas instead: separate local steps, each with an undo.

Two-phase commit, step by step

STEPS01 / 07

Simplified: real implementations add timeouts, acknowledgements and recovery from the logs. A saga gives up isolation, so it is a different trade-off rather than a drop-in replacement.

STEP 01 / 07 One change, two databases
THE MATH
A: 500 − 100 = 400 B: 200 + 100 = 300 both, or neither
  1. Step 1: One change, two databases. Moving $100 from an account at bank A to an account at bank B takes two writes in two separate databases. If the first succeeds and the second fails, $100 has left A and arrived nowhere. The two writes must be all-or-nothing, yet neither database knows what happened in the other.
  2. Step 2: Phase 1: prepare. Two-phase commit puts a coordinator in charge. In phase one it sends PREPARE to every database taking part. Each one does the work, saves it to disk and locks the rows involved, but does not make the change final. The balances still read $500 and $200.
  3. Step 3: The vote. Each database answers with a vote. YES is a binding promise: whatever happens next, even a crash and restart, it will be able to commit this change if told to. A database that cannot do its part, for example because the account is closed, votes NO. Here both vote YES.
  4. Step 4: Phase 2: commit. Every vote was YES, so the coordinator decides COMMIT. It first writes that decision to its own log, so that it survives a crash, and then sends COMMIT to all. Each database makes its change final and releases its locks. Both writes have happened, as one unit.
  5. Step 5: One NO aborts everything. Suppose instead that bank B votes NO. One refusal is enough: the coordinator decides ABORT and tells everyone. Bank A, which had prepared and voted YES, throws its pending change away. No database is left with half the transfer; the balances are exactly as before.
  6. Step 6: The flaw: blocking. Now the coordinator crashes after both databases voted YES and before it tells them the decision. Each is stuck in doubt. It cannot commit, because the other may have voted NO. It cannot abort, because the coordinator may already have decided COMMIT. Both must wait, rows locked, until the coordinator returns.
  7. Step 7: An alternative: sagas. A saga avoids the shared wait. Each step is an ordinary local transaction that commits at once, and each has a compensating step that undoes it. If step 2 fails, the saga runs the undo for step 1. Nothing blocks, but between the steps other readers can see the half-done state, and every step needs an undo that works.
VALUES SO FAR
bank A$500balance
bank B$200balance
transfer$100A to B
votes····phase 1
decision····phase 2
result····final
any NO····rule
no coord····blocked
saga····fallback

Spotted a mistake on this page?