Distributed Atomic Commit: Two-Phase and Three-Phase Commit Protocols
In a distributed transaction spanning multiple independent database nodes, all participating shards must satisfy the ACID Atomicity guarantee: either every participant commits its localized changes permanently, or all participants abort and roll back completely.
Because individual nodes can crash and network links can drop packets, distributed systems utilize atomic commitment protocols. The Two-Phase Commit (2PC) protocol is the standard consensus mechanism for cross-shard ACID transactions, while the Three-Phase Commit (3PC) protocol modifies the flow to mitigate coordinator blocking issues under specific network models.
Two-Phase Commit (2PC) Architecture
2PC divides distributed transaction commitment between a designated Coordinator node and a set of Participant (cohort) nodes across two sequential phases:
Phase 1: Prepare Phase (Voting Phase)
- The Coordinator writes a
START_2PCrecord to its Write-Ahead Log (WAL) and sends aPREPARERPC to all participants. - Each participant executes the transaction locally up to the point of committing (acquiring necessary locks, validating constraints, and writing undo/redo logs to persistent storage).
- Each participant replies with either
VOTE_COMMIT(if ready and resources are locked) orVOTE_ABORT(if constraints failed or resources are unavailable).
Phase 2: Commit / Abort Phase (Decision Phase)
- If ALL participants voted
VOTE_COMMIT: The Coordinator writes aGLOBAL_COMMITrecord to its log and broadcasts aGLOBAL_COMMITmessage to all participants. Participants commit changes permanently, release locks, and reply withACK. - If ANY participant voted
VOTE_ABORT(or a timeout expires): The Coordinator writes aGLOBAL_ABORTlog record and broadcasts aGLOBAL_ABORTcommand. Participants roll back changes using undo logs and release locks. - Once all
ACKmessages are received, the Coordinator writes anENDlog record, terminating the transaction.
The Critical Weakness: The Blocking Problem
2PC is classified as a blocking protocol due to a fundamental failure mode:
- Coordinator Crash during Decision Phase: If the coordinator crashes after participants have voted
VOTE_COMMITbut before broadcasting theGLOBAL_COMMIT/GLOBAL_ABORTdecision, participants enter an indefinite state of uncertainty. - Resource Deadlocks: Participants that voted
VOTE_COMMITcannot unilaterally decide to abort (because the coordinator might have already committed) or unilaterally decide to commit (because another participant might have voted abort). - Lock Contention: As a result, participants must keep table and row locks held indefinitely until the coordinator recovers, causing cascading timeouts and system-wide stalling.
Three-Phase Commit (3PC): Eliminating the Blocking State
Introduced by Dale Skeen in 1981, Three-Phase Commit (3PC) eliminates indefinite blocking under crash-stop failure models (with bounded network latency) by splitting the decision phase into two separate stages:
- Phase 1: Can-Commit? (Voting): Coordinator asks participants if they can commit. Participants vote
YESorNOwithout locking resources irreversibly. - Phase 2: Pre-Commit: If all voted
YES, the coordinator issues aPRE_COMMITmessage. Participants acknowledge and enter a state where they know every other node also agreed to commit. - Phase 3: Do-Commit: Upon receiving acknowledgements from all participants, the coordinator issues the final
DO_COMMITcommand.
Key Non-Blocking Guarantee: If the coordinator crashes while participants are in the PRE_COMMIT state, any newly elected coordinator knows that all participants previously agreed to commit, allowing it to safely proceed with the commit without stalling. However, 3PC assumes a synchronous network and can still fail or cause split-brain data divergence during arbitrary asynchronous network partitions.
2PC vs. 3PC vs. Modern Consensus Protocols
- Failure Resilience: 2PC handles fail-recovery nodes but blocks on coordinator loss; 3PC avoids blocking in fail-stop systems but fails in partitioned networks; Raft/Paxos handles partitions safely via quorums.
- Latency Overhead: 2PC requires 2 round-trip times (RTTs); 3PC requires 3 RTTs; Modern distributed databases combine 2PC for cross-shard coordination with Raft/Paxos for intra-shard replication.
- Saga Pattern Alternative: For long-running asynchronous workflows spanning microservices, systems replace synchronous 2PC blocking locks with the Saga pattern (compensating transactions).
Real-World Systems and Engineering Use Cases
- Google Spanner & CockroachDB: Using Two-Phase Commit layered directly on top of Multi-Raft / Paxos consensus groups to coordinate cross-range atomic SQL transactions with TrueTime ordering.
- XA Transactions (Java JTA / PostgreSQL / MySQL): Standardizing enterprise distributed transactions across heterogeneous relational databases and message brokers (JMS, ActiveMQ).
- Distributed Object Stores & Storage Appliances: Synchronizing file metadata updates and multi-region volume commits across storage clusters.
- Financial Settlement & Payment Gateways: Ensuring funds deduction and credit issuance across distinct banking ledgers maintain all-or-nothing transactional guarantees.