Skip to content

Two-phase commit and why it blocks

A single database can commit a transaction on its own. When one transaction touches two databases, for example an order in one and a payment in the other, each can fail separately, and the risk is that one commits and the other does not. The classic answer is two-phase commit. The Gray and Lamport paper states the problem as reaching agreement on whether a transaction is committed or aborted, and names the classic protocol with its weakness: it blocks if the coordinator fails.

There is a coordinator, which the paper calls the transaction manager, and there are participants, which it calls resource managers.

  1. Prepare. The coordinator asks every participant to prepare. A participant that can commit makes the transaction durable and votes yes. One that cannot votes no.
  2. Decision. If every vote is yes, the coordinator tells everyone to commit. If any vote is no, or a vote does not arrive in time, it tells everyone to abort.

Voting yes is a promise. The PostgreSQL documentation describes the state after PREPARE TRANSACTION: the transaction’s state is fully stored on disk, and there is a very high probability that it can be committed successfully even if a database crash occurs before the commit is requested. The participant can no longer decide for itself. It must wait to be told.

Mental model. A wedding. The officiant asks each side whether they will. Once both have said yes, neither may walk out; only the officiant’s words make it final. If the officiant faints between the two yeses and the declaration, both sides stand there, bound by their yes, and cannot say whether they are married.

If a participant fails before it votes, or a vote is lost, the coordinator aborts, and nothing is stuck. If the coordinator fails after sending Prepare and before sending the outcome, the participants that voted yes are in a state of doubt. Gray and Lamport spell out the worst moment: if the coordinator fails right after every participant has sent its Prepared message, the other participants have no way of knowing whether it committed or aborted the transaction.

A participant in doubt cannot commit, in case the decision was to abort. It cannot abort, in case the decision was to commit and another participant has already done so. It must wait, and the coordinator being repaired is the only thing that ends the wait.

The wait has a cost beyond the delay. The PostgreSQL documentation says the transaction continues to hold whatever locks it held, and that it is unwise to leave transactions in the prepared state for a long time: this interferes with the ability of VACUUM to reclaim storage and in extreme cases could cause the database to shut down to prevent transaction ID wraparound. Its intended use is that a prepared transaction is committed or rolled back as soon as an external transaction manager has verified that the other databases are also prepared. If no manager is set up to close prepared transactions out promptly, it recommends keeping the feature off, by setting max_prepared_transactions to zero. Other transactions that need the same rows have to wait for those locks, so a failed coordinator can stall work that has nothing to do with the transaction itself.

Check what the protocol does and does not promise.

Check your understanding: two-phase commit

This quiz needs JavaScript to check your answers.

Question 1 of 3

Three participants have voted yes. The coordinator fails before it sends the outcome.

What can a participant do?

Choose one answer.

Question 2 of 3

Which failure does not leave the participants waiting?

Choose one answer.

Question 3 of 3

What removes the stall caused by a failed coordinator?

Choose one answer.

What happens if the coordinator is a consensus group?

Section titled “What happens if the coordinator is a consensus group?”

The block comes from one process holding the decision. Gray and Lamport’s answer is Paxos Commit: it runs a consensus algorithm on the commit or abort decision of each participant, with several coordinators in place of one, and it makes progress as long as a majority of those coordinators are working. The decision is no longer in one memory. It is held by a majority, in the way the earlier lessons held a leader and a lease, so the failure of one node does not decide the transaction. The paper adds that fault-tolerant consensus algorithms do not block whenever any majority of the processes are working.

This costs something: more machines hold the decision, so there is more to run and to pay for. A system that avoids distributed transactions altogether, for example by keeping what must change together in one shard, has no such protocol to run.

  • Two-phase commit has two phases: prepare, where each participant makes the transaction durable and votes, and decide, where the coordinator announces commit or abort for all.
  • A yes vote is a promise. The participant can no longer commit or abort by itself.
  • If the coordinator fails after all the votes are in, the participants cannot know the outcome, and they wait while their locks stay held. This is why the protocol is said to block.
  • Replacing the single coordinator with a group that decides by majority, as Paxos Commit does, removes the stall, at the cost of more machines to run.
  • The alternative is to avoid distributed transactions by keeping what must change together in one place.

That is the end of the module’s lessons. The next page gathers them: a recap, a checklist for using agreement between nodes, and a quiz that covers all five.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.