Two-Phase Commit (2PC) & Its Limits
Explore atomic distributed transactions: Prepare phase, Commit phase, coordinator failure vulnerabilities, and blocking pitfalls.
01.1. How Two-Phase Commit (2PC) Enforces Distributed Atomicity
When a business transaction spans multiple independent, heterogeneous databases (e.g., deducting inventory in PostgreSQL, debiting a card in MySQL, and issuing loyalty points in Oracle), standard single-database ACID commits are impossible.
Two-Phase Commit (2PC) (standardized in the XA specification) is a distributed algorithm that guarantees Atomicity across multiple nodes: either all participating databases commit the transaction, or all databases abort and roll back.
The 2PC Protocol Execution:
- Phase 1: Prepare (Voting Phase):
- The Transaction Coordinator assigns a global transaction ID and sends a
PREPAREmessage to all participating databases (cohorts). - Each cohort executes the SQL query up to the commit point, writes the changes to its local Write-Ahead Log (WAL), locks the affected rows with exclusive locks, and responds with either
VOTE_COMMIT(if prepared safely) orVOTE_ABORT(if constraints failed or deadlock occurred).
- The Transaction Coordinator assigns a global transaction ID and sends a
- Phase 2: Commit / Abort (Decision Phase):
- Unanimous Consent: If every cohort voted
VOTE_COMMIT, the coordinator writesGLOBAL_COMMITto its local persistent log and broadcastsGLOBAL_COMMITto all cohorts. Cohorts commit the changes, release their row locks, and send anACK. - Single Veto / Timeout: If any cohort votes
VOTE_ABORTor fails to respond within a timeout, the coordinator logsGLOBAL_ABORTand instructs all cohorts to roll back their prepared transactions and release locks.
- Unanimous Consent: If every cohort voted
Two-Phase Commit (2PC) Protocol Flow 🤝
Two-Phase Commit (2PC) Protocol Flow 🤝
Phase 1 (Prepare / Vote) and Phase 2 (Commit / Abort).
Unlock Topic #87: Two-Phase Commit (2PC) & Its Limits
You are viewing a preview. The full in-depth engineering deep dive, interactive simulators, architecture flowcharts, and self-assessment quizzes for this topic are available with Pro or Lifetime Access.
Failure modes, high-throughput bottlenecks, and real FAANG implementation decisions.
Interactive system topology diagrams, live parameter simulators, and downloadable SVG charts.
Staff-level multiple-choice quiz questions with instant feedback and answer explanations.
Firebase Google authentication automatically syncs your completed topics and quiz scores.
How clear and staff-actionable was this system breakdown?