Definition
Two-phase commit (2PC) is a protocol that makes several computers, called participants, either all commit a shared transaction §8.7 or all abort it. One computer, the coordinator, runs it in two phases. In the voting phase it asks every participant whether it can commit; a participant that answers yes promises to commit if told to. In the decision phase the coordinator decides commit if every participant voted yes, and abort otherwise, and sends the decision to every participant.
The idea
An order system takes payment in one database and reserves stock in another database on a different machine. Each database can commit its own part. If the payment database commits and the stock database then aborts, perhaps because the item has sold out, the customer pays for nothing. A single message saying “commit now” does not help, because a participant may be unable to commit when the message arrives. The coordinator must first learn that every participant can commit, and a participant must not change its mind after saying so.
A wedding ceremony is the classic picture. The officiant asks each partner “Do you?” Each “I do” is a binding promise, and only when both promises are in does the officiant pronounce the marriage; if either partner says no, there is no marriage. The picture stops at the weakness of the protocol. At a wedding everyone is in one room and sees what happens. Over a network, a participant cannot tell a crashed coordinator from a slow one, and cannot see what the coordinator told the others. A participant that has promised and then hears nothing may neither commit nor abort on its own.
How it works
Each computer keeps a log on non-volatile storage, storage that keeps its contents without power. To force a record means to write it to the log and wait until it is stored permanently. The basic protocol for one coordinator and participants has six steps.
Prepare. The coordinator sends
PREPAREto every participant.Vote. A participant that can commit forces a prepared record, which holds everything it needs to commit later even after a crash, keeps its locks on the data, which stop other transactions from changing that data, and replies
YES. From this moment it may not abort on its own. A participant that cannot commit repliesNOand aborts its part.Decide. If every vote is
YES, the coordinator forces a commit record §8.21. This write is the commit point of the whole transaction. If any vote isNO, or a vote does not arrive in time, the coordinator decides abort.Announce. The coordinator sends
COMMITorABORTto every participant.Apply. Each participant forces the decision into its own log, makes its part permanent or undoes it, releases its locks and sends an acknowledgment.
Forget. When every acknowledgment has arrived, the coordinator may discard its records of the transaction.
After a restart, each computer’s log tells it what to do. A participant with a decision record carries out that decision. A participant with no prepared record aborts, which is safe because it never voted yes. A participant with a prepared record but no decision must ask for the outcome. A coordinator with a commit record sends COMMIT again. A coordinator with no commit record may decide abort, because it cannot have told any participant to commit.
The interval between a participant’s YES and its receipt of the decision is called its uncertainty period. In that interval the participant has given up its right to decide but does not know the decision. If the coordinator fails then, the participant can ask the other participants. That helps when one of them has received the decision, or has not yet voted and can therefore still abort. It does not help when every participant it can reach is uncertain too. The participant must then wait, holding its locks, until the coordinator recovers. This wait is called blocking.
Worked example
A coordinator C runs a transaction with participants and . Both vote YES and force their prepared records. C forces its commit record, sends COMMIT to , and crashes before the message to leaves. commits and releases its locks. holds a prepared record and no decision, and C does not answer.
cannot tell apart two histories.
History 1, the real one: C decided commit, and has committed. If aborts, the two participants disagree.
History 2: voted
NO, C decided abort, and C crashed before telling . If commits, it commits a transaction that another participant refused.
In both histories has the same log and has received the same messages, so no rule that uses only what knows can choose correctly in both. waits. If it can reach , reports that it committed, and commits as well. If is down too, stays stuck until C recovers, and every other transaction that needs the data locked by waits with it.
In the version described here, participants need messages (one PREPARE, one vote, one decision and one acknowledgment for each) and forced log writes (a prepared record and a decision record for each participant, plus the coordinator’s commit record). For that is messages and forced writes. Before the commit point, the transaction waits for one round trip of messages and for two forced writes in sequence: the prepared records, which the participants write in parallel, and then the commit record.
Stated precisely
The fault model §1.30: computers may crash and later restart with their logs intact; messages may be delayed or lost but are not altered; no computer lies. A computer cannot tell whether another computer has crashed or is only slow. Under this model two-phase commit guarantees two safety properties §1.11, statements that something bad never happens.
Agreement: no two participants reach different outcomes.
Validity: the outcome is commit only if every participant voted
YES; and if every participant votesYESand no failure or timeout §9.34 occurs, the outcome is commit.
It guarantees termination, the liveness property §1.12 that every participant eventually decides, only if the coordinator, or some computer that knows the decision, eventually becomes reachable from every uncertain participant.
The blocking follows from having a single coordinator, not from a defect of one implementation. Jim Gray and Leslie Lamport’s paper “Consensus on Transaction Commit” (2006) presents Paxos Commit, which replaces the single coordinator by coordinators that agree on the outcome with the Paxos consensus algorithm §14.1, a method by which a group of computers agree on one value while a majority of them works. Paxos Commit makes progress as long as of the coordinators work, where is the number of coordinator failures to be tolerated. The paper shows that two-phase commit is the case : with one coordinator, the failure of that one computer can stop progress. Three-phase commit, an older protocol, adds a round of messages and avoids blocking only if every message arrives within a known time and the network never splits into groups that cannot reach each other.
Limits and common mistakes
Two-phase commit gives up availability §1.10 to keep agreement. A participant that waits for the decision keeps its locks, so one coordinator failure can stall unrelated transactions that need the same data.
A timeout cannot resolve an uncertain participant. A timeout tells a participant that the coordinator is silent, not what it decided. Before voting, a participant may safely abort after a timeout; after voting yes, it may not.
Some transaction managers let an operator force an outcome on a stuck transaction. This is called a heuristic decision. It restores progress at the risk that the participants disagree, and the system must then report the disagreement so that someone can repair it.
Two-phase commit is not two-phase locking. Two-phase locking is a rule for isolation: a transaction acquires all of its locks before it releases any. The two often appear together, because a prepared participant must keep its locks until the decision arrives.
Two-phase commit needs independent participants, each of which can vote no. Preparing data on one device and then switching one stored selector is a two-step commit with a single decision maker; there are no votes and no remote coordinator, so the blocking described here does not arise. What it shares with two-phase commit is one idea: do everything that can fail before the single step that decides.
The protocol assumes that every computer follows it. Defending against Byzantine faults §1.31, failures in which a computer behaves arbitrarily or lies, such as a participant that votes yes and later refuses to commit, needs different protocols.
Questions answered
Why can participants block after an uncertain coordinator failure?
A participant can block because, once it has voted yes, it has promised to accept whatever the coordinator decides, and after the coordinator fails it may hold no information that reveals the decision. In two-phase commit, the coordinator collects a yes or no vote from every participant, decides commit only if all voted yes, and then announces the decision. Between voting yes and hearing the decision, a participant is uncertain. If the coordinator crashes in that interval, the participant’s own log and the messages it has received are the same in two possible histories. In one, the coordinator decided commit and another participant has already committed. In the other, another participant voted no and the coordinator decided abort. Aborting would be wrong in the first history, and committing would be wrong in the second. A timeout does not help, because silence says nothing about the decision. So the participant must wait, holding its locks, until the coordinator recovers or until it reaches another participant that knows the outcome, or that has not voted and can still abort.
This waiting is the price of guaranteeing that no two participants disagree. Protocols that avoid it replicate the decision across several coordinators, as Paxos Commit does, or assume known bounds on message delay, as three-phase commit does.
Related entries: Transaction (§8.7), Exactly-once effect (§8.32), Safety property (§1.11), Liveness property (§1.12). Further reading: [180].