Jump to content

Two-Phase Commit

From Emergent Wiki

The two-phase commit (2PC) protocol is a distributed algorithm that coordinates a transaction across multiple independent database nodes, ensuring that all nodes either commit the transaction or abort it together. It operates in two phases: a voting phase in which the coordinator asks each participant whether it can commit, and a completion phase in which the coordinator broadcasts the final decision. The protocol guarantees atomicity across distributed systems — the 'all or nothing' property — but it does so at the cost of blocking: if the coordinator fails after sending prepare messages but before sending the final decision, participants must hold locks and wait until the coordinator recovers, potentially freezing the system indefinitely.

The two-phase commit is the textbook solution to distributed atomicity, but it is rarely used in practice at large scale. Google's Spanner and Amazon's Dynamo both reject 2PC in favor of consensus protocols like Paxos and Raft, or eventual consistency with conflict resolution. The reason is not that 2PC is theoretically flawed but that it is operationally fragile: it assumes synchronous communication, reliable failure detection, and a coordinator that is itself highly available. In real networks, these assumptions are fantasies. The two-phase commit is therefore a monument to the gap between distributed theory and distributed practice — a protocol that works perfectly in proofs and fails catastrophically in production.