Two-Phase Commit (2PC)
Summary: A classic algorithm for distributed atomic commit, ensuring that all participating nodes in a transaction either commit or abort.
Sources: chapter9
Last updated: 2026-04-17
Two-phase commit is an algorithm for achieving atomic-commit across multiple nodes (source: chapter9, p. 355). It is used by some databases and in systems like xa transactions.
The Phases
The process is managed by a coordinator (or transaction manager) and involves two phases:
- Phase 1: Prepare: The coordinator sends a prepare request to all participants, asking them to vote on whether they can commit. Each participant checks for constraint violations and ensures its data is written to disk (source: chapter9, p. 356).
- Phase 2: Commit: If all participants vote “yes,” the coordinator makes the definitive decision to commit and sends a commit request to all participants. If any participant votes “no” or times out, the coordinator sends an abort request (source: chapter9, p. 356).
The Coordinator
The coordinator is the central component and its failure can cause problems (source: chapter9, p. 358).
- Once a participant votes “yes,” it is in doubt (or uncertain) until it receives a commit or abort decision from the coordinator (source: chapter9, p. 358).
- If the coordinator crashes while participants are in doubt, they must wait for it to recover. This makes 2PC a blocking protocol (source: chapter9, p. 359).
The “Point of No Return”
There are two critical “points of no return” in 2PC (source: chapter9, p. 357):
- Once a participant votes “yes,” it promises to be able to commit if told to do so.
- Once the coordinator decides to commit, that decision is irrevocable and must be enforced on all participants.
Limitations
- Coordinator Failure: As mentioned, it’s a blocking protocol (source: chapter9, p. 359).
- Performance: 2PC has a heavy performance penalty due to disk writes and network round-trips (source: chapter9, p. 360).
- Locks: While a participant is in doubt, it must hold onto all locks associated with the transaction, which can block other transactions (source: chapter9, p. 362).
Three-Phase Commit (3PC)
3PC is an alternative that is non-blocking but requires a perfect failure detector, which is impossible in an asynchronous network (source: chapter9, p. 359).