How two-phase commit records one decision

Short answer: Two-phase commit makes a set of participants install one outcome, commit or abort, for a single transaction. The coordinator collects a vote. A participant who votes yes first writes a record that it will accept the coordinator’s later decision. If every vote is yes, the coordinator writes a commit record and only then sends commit. Any no, or a timeout before every yes arrives, writes an abort. After a yes vote, a participant who cannot see the decision must wait. That wait is the blocking the protocol is known for.


What the problem actually asks

A transaction that updates more than one store has to end the same way everywhere. If one store applies the update and another discards it, the pair of stores has no single outcome a caller can name. Two-phase commit inserts a recorded decision in front of the apply step. The coordinator is the only process that writes the decision. Participants promise, before they vote yes, that they can still perform either outcome and that they will not forget the promise across a crash.

The promise is what creates blocking. Once a participant has voted yes, aborting on its own could contradict a commit the coordinator has already sent to someone else. Until the decision arrives, the participant holds locks and waits. A crash of the coordinator in that window does not give the participant a safe local choice.


What the algorithm guarantees

If every process that writes a record recovers that record after a crash, and if messages are eventually delivered between processes that are up, every participant that reaches a decision reaches the decision in the coordinator’s record. Participants who voted no, or who had not voted when an abort was decided, end in abort. Extra durable storage is one decision record at the coordinator and one vote record at each participant who votes yes.

The protocol does not guarantee that a waiting participant finds out the decision by a deadline. A coordinator that stays down, and a log that nobody else can read, leaves yes-voters blocked. Three-phase commit changes the failure story and brings its own partition cases. A consensus log such as Paxos or Raft is the modern way to keep the decision record available. Two-phase commit is the two-round vote plus a single decision record.


The working steps

  1. The coordinator writes a prepare record and sends a prepare request to every participant.
  2. A participant who can still go either way writes a yes record and replies yes. A participant who cannot, writes abort for its local work and replies no.
  3. If every reply is yes, the coordinator writes commit and sends commit. If any reply is no, or the votes do not all arrive, the coordinator writes abort and sends abort.
  4. A participant writes the outcome it receives, applies or discards the local work to match, and acknowledges.
  5. On recovery, a participant with a yes record and no outcome asks the holder of the decision record. A participant with no yes record aborts. The coordinator recovers the outcome from its own record.

Agreement here is about one transaction’s commit or abort under durable logs. It is not a bounded-delay agreement if the coordinator’s log is unreachable. Replicated consensus is a different algorithm. Two-phase commit is the prepare vote followed by one recorded decision.


Approaches that look simpler and fail

Approach Why it fails when a yes voter aborts because the coordinator is quiet
Abort locally after voting yes and hearing nothing The coordinator may already have written commit and informed other participants. The quiet participant’s abort would split the outcome.
Send commit before the commit record is durable A coordinator crash at that moment forgets a decision some participant may have applied. The write comes before the message.
Treat a missing vote as a yes A participant who never promised can still abort. Only an explicit yes enters the commit rule.
Expect the protocol to finish while the only copy of the decision record is down Yes voters are required to wait for that record. Availability of the decision is a replication problem sitting under the protocol.

A replicated decision record is a consensus algorithm. Two-phase commit is the blocking vote that installs one commit or one abort.


A working example

This Python fragment writes the commit record before sending it. Both participants vote yes, so the decision is commit. A participant that has voted yes and has not yet seen the record is still waiting. It prints commit waiting.

log = {"coordinator": None, "A": None, "B": None}
votes = {}
for name in ("A", "B"):
    log[name] = "yes"
    votes[name] = "yes"
assert all(v == "yes" for v in votes.values())
log["coordinator"] = "commit"  # durable before the messages
# A has the decision. B voted yes and has not received it.
received = {"A": "commit", "B": None}
state = []
for name in ("A", "B"):
    if received[name]:
        state.append(received[name])
    elif log[name] == "yes":
        state.append("waiting")
print(log["coordinator"], state[1])

Where it is used

Two-phase commit is the protocol of XA distributed transactions and of PostgreSQL prepared transactions: every participant votes, then one decision is written. A participant that has voted yes must be able to finish after a crash. A single-node transaction does not need the vote.


When you can drop this algorithm

Drop a single coordinator log when the decision must stay available through a coordinator crash. Put the decision in a replicated log. Keep the two phases when the stores can wait and the coordinator’s record is durable.


Why this stays published

We publish this so a search for two-phase commit returns the yes vote, the durable decision record, and the reason a yes voter waits. Two-phase commit installs one commit or abort by collecting yes votes and writing the decision before it is sent, and a participant who voted yes waits until that decision is visible.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *