Two-Phase Commit (2PC) is a distributed consensus protocol that guarantees atomicity for a transaction across multiple, independent participants, ensuring all participants either commit the transaction or all abort it. It works through two distinct phases orchestrated by a central coordinator agent.
Phase 1: Voting (Prepare Phase)
- The coordinator sends a
PREPARE message to all participant agents.
- Each participant performs local validation and writes transaction logs to stable storage but does not commit.
- Each participant replies with a
VOTE_COMMIT if ready, or a VOTE_ABORT if unable to proceed.
Phase 2: Decision (Commit/Abort Phase)
- If the coordinator receives
VOTE_COMMIT from all participants, it decides to commit. It logs this decision durably and sends a GLOBAL_COMMIT message.
- If any participant votes
VOTE_ABORT or times out, the coordinator decides to abort, logs it, and sends a GLOBAL_ABORT message.
- Participants receive the decision, implement it locally (commit or rollback), and send an
ACK to the coordinator.
- The coordinator completes the transaction after receiving all ACKs.