How Two-Phase Commit Works: Prepare, Decision, and Why Blocking Happens

A payment service writes a debit. An inventory service reserves a unit. Either both must become durable facts, or neither may. That is an atomic commit across two resource managers that do not share a log.

Two-phase commit (2PC) is the classic protocol that tries to deliver that all-or-nothing outcome. A coordinator asks every participant to prepare. If every participant answers yes, the coordinator records a commit decision and tells them to finish. If anyone answers no, or is missing, the decision is abort.

This article is about that protocol: the two phases, the votes, the coordinator log that makes the decision durable, the blocking window after a yes vote, and why people confuse 2PC with consensus. It is not a second tour of Raft’s leader election or of a single-node write-ahead log. Those solve different contracts. The search intent here is one question: how does two-phase commit force every participant to commit or every participant to abort?

The contract

Participants are independent systems. Each already knows how to commit or roll back a local transaction. What they cannot do alone is learn whether the others will succeed.

2PC introduces a coordinator and a two-step agreement:

  • Phase 1 (prepare / vote). The coordinator sends PREPARE. Each participant does all work that can still be undone, writes enough durable state to finish either way, and answers YES or NO.
  • Phase 2 (decision). If every vote is YES, the coordinator durably records COMMIT and sends COMMIT. Otherwise it records ABORT and sends ABORT. Participants then finish locally and acknowledge.

The safety property is simple: no participant applies the transaction as committed unless the coordinator’s durable decision is commit, and if that decision is commit, no participant is allowed to abort it later. The cost is a window where a participant that voted YES cannot decide alone.

Why a single COMMIT message is not enough

Suppose the coordinator just sent COMMIT to both services and crashed after the first packet left the NIC. Inventory committed. Payments never heard the message and, after a timeout, rolled back. The customer kept the item and the money.

The missing piece is a moment when every participant has promised it can go either way, and a single durable bit that records which way was chosen. Phase 1 creates the promise. The coordinator log creates the bit. Phase 2 broadcasts the bit.

That is different from replicating one log. Raft’s job is to make a majority of replicas agree on the same sequence of commands. 2PC’s job is to make several already-separate logs accept the same commit/abort bit for one transaction. You can run 2PC over resources that never elect a leader together. You can run Raft inside one of those resources. They stack; they do not substitute.

Phase 1 in detail

Take two participants, Payments and Inventory, and a coordinator C.

1) The application asks C to commit transaction T, which already has local work prepared at each participant (a debit row, a reserved SKU).

2) C writes a durable record that T exists and is in the preparing state, then sends PREPARE(T) to both.

3) Payments checks constraints, writes a prepare record to its own WAL, and fsyncs. After that fsync it is allowed to answer YES. The prepare record must contain enough undo and redo information that a later COMMIT or ABORT can finish even if the process dies. Until the vote is sent, Payments may still abort on its own (constraint failure, disk full, operator cancel).

4) Inventory does the same.

5) Votes return to C.

A NO is final for that participant: it has already undone or never installed the change. A missing vote is treated as NO for the decision, or the coordinator retries until a timeout policy fires. Either way the only legal global decision after a NO is abort.

A YES is a lock on the future. The participant has promised: I will not unilaterally abort, I will not forget T, and I will wait for C’s decision. Locks, version chains, or prepared-transaction state stay held. That is the source of 2PC’s blocking reputation.

The decision record

C now knows the votes. Before it tells anyone the outcome it must write a decision record and force it to stable storage.

If the decision is not durable, C can crash after sending COMMIT to Inventory and come back believing T was never decided. Recovery would abort. Inventory already committed. The protocol is broken.

Once the decision record is stable, C may send COMMIT or ABORT as many times as it needs. Retransmission is safe because the bit does not change. Participants that already finished ignore duplicates. Participants that missed the first message catch up from the retry or by asking C during recovery.

Only after acknowledgments arrive may C forget T. Forgetting early turns a later inquiry from a recovering participant into a guess.

Phase 2 and participant recovery

On COMMIT, a participant writes a local commit record, releases locks, and makes the new versions visible. On ABORT it applies undo from the prepare record and releases the same locks.

If the participant crashes after voting YES and before hearing the decision, restart finds a prepared transaction with no local outcome. It cannot guess. It must ask the coordinator (or a collaborator that holds a copy of the decision). Until that answer arrives, readers that need a committed snapshot of the locked rows wait or see the previous version, depending on the isolation design.

If the coordinator crashes after the decision record is on disk, recovery reads the record and resends COMMIT or ABORT. If it crashes before the decision record, recovery must abort: no participant can have committed, because no COMMIT message is legal before that write.

The blocking window

After a YES vote, a participant is stuck until it learns the decision. If C dies and its decision log is unreachable, those transactions stay prepared. Locks do not drain. New work that touches the same keys stalls.

That is not a bug in an implementation. It is the price of letting the coordinator be a single decider. Three-phase commit and various non-blocking atomic-commit protocols add extra rounds and extra assumptions (more live nodes, more messages) to shrink that window. They do not delete the need for a durable decision somewhere.

Operators deal with the window in practice by:

  • keeping the coordinator’s log on replicated storage so “C is down” is rarer than “one disk died”
  • forcing prepared-transaction timeouts only when a human or an automated fence can prove the coordinator will never come back with COMMIT
  • avoiding 2PC for paths that can accept a weaker model (outbox plus retry, saga compensation, or a single resource manager)

A compact example

T reserves SKU 441 and charges customer 19.

C                Payments              Inventory
| PREPARE(T)        |                     |
|------------------>|                     |
|                   | WAL prepare + fsync |
|              YES  |                     |
|<------------------|                     |
| PREPARE(T)        |                     |
|---------------------------------------->|
|                   |                     | WAL prepare + fsync
|              YES  |                     |
|<----------------------------------------|
| fsync DECISION=COMMIT                   |
| COMMIT(T)         |                     |
|------------------>|                     |
|                   | commit + unlock     |
| ACK               |                     |
|<------------------|                     |
| COMMIT(T)         |                     |
|---------------------------------------->|
|                   |                     | commit + unlock
| ACK               |                     |
|<----------------------------------------|
| forget T          |                     |

Change Inventory’s vote to NO and the DECISION line becomes ABORT. Payments, which already said YES, must still roll back. That is the whole point of the promise: YES means “I can still undo,” not “I already committed.”

Where 2PC shows up

Relational engines expose it as XA or an internal two-phase API: a transaction manager calls xa_prepare / xa_commit on each resource. Message queues that support transactional dequeue participate the same way. Some distributed SQL systems use a 2PC-shaped commit for multi-shard writes even when they use Raft inside each shard: Raft replicates the shard’s prepare and commit records; 2PC chooses the global bit.

That last pairing is easy to misread. Per-shard Raft makes a shard durable and consistent with its replicas. It does not by itself make shard A and shard B accept the same outcome for one user transaction. The cross-shard protocol is still an atomic commit, often 2PC or a variant that stores the decision in a replicated record.

Misconceptions

“2PC is consensus.” Consensus agrees on a value (or a log) among replicas of one state machine. Atomic commit agrees on commit versus abort among participants that each own different data. A participant that votes NO is not a replica that lagged. It is a resource that cannot install the change.

“Once I vote YES I have committed.” YES means the participant can finish either outcome after a crash. Visibility to clients usually happens on phase 2. Treating prepare as commit is how you leak reserved stock that later aborts.

“If the coordinator times out, abort locally.” After a YES, a local abort can diverge from a COMMIT that already reached another participant. Timeout-to-abort is legal only before the YES leaves, or after an authoritative abort decision.

“More phases remove the need for a durable decision.” Extra phases can move who is allowed to decide after a crash. Someone still has to remember the bit.

How this sits next to other machinery

A participant’s prepare record is a WAL record. The same steal / no-force rules that apply to ordinary commits apply here; the extra requirement is that the prepare itself is stable before the vote.

Raft can store the coordinator’s decision so that “the coordinator” is a small replicated group rather than one process. That removes a common outage mode. It does not change the two phases the participants see.

Related articles on this site:

Takeaways

Two-phase commit splits a distributed commit into a vote and a decision. The vote makes undo and redo possible after a crash. The coordinator’s forced decision record is the one bit every participant must obey. A YES vote is a promise to wait, which is why a dead coordinator can stall locks. Consensus protocols replicate one history. 2PC coordinates several histories so they accept the same ending.

Next Post Previous Post