What is Two-Phase Commit?
Also known as: 2PC
Two-Phase Commit is a distributed consensus protocol that ensures all participants in a transaction either all commit or all abort, maintaining atomicity across multiple databases or services.
Two-Phase Commit (2PC) is a distributed algorithm used to coordinate a commit or abort decision across multiple participants (resource managers) that take part in a single atomic transaction. It is a blocking protocol with two distinct phases: the prepare phase and the commit phase. In the prepare phase, a designated coordinator sends a prepare request to every participant and waits for their votes. Each participant writes a prepare log record, forces its state to disk, and replies with either a Yes vote (promising it can commit) or a No vote (forcing an abort). If the coordinator receives Yes votes from all participants, it decides to commit; otherwise it decides to abort.
In the second phase, the coordinator broadcasts the decision (commit or abort) to all participants and waits for acknowledgments. Each participant that receives a commit must write a commit log record, apply the changes permanently, and acknowledge. Once all acknowledgments are received, the coordinator records the transaction outcome in a log and considers the transaction complete. The protocol guarantees that all participants converge on the same outcome because a participant that voted Yes is blocked until it hears the final decision: it cannot unilaterally commit or abort after promising to commit. This blocking property is the main weakness of 2PC. If the coordinator crashes after sending prepare requests but before sending the final decision, participants may remain locked indefinitely until the coordinator recovers, which reduces availability.
Two-Phase Commit is a foundational building block for distributed transactions, appearing in X/Open XA (eXtended Architecture) standard, where it coordinates between a transaction manager and resource managers such as relational databases, message queues, and other transactional resources. It is also used in many distributed databases and transaction monitors, though modern systems often prefer alternative protocols (Paxos, Raft, or relaxed consistency models) to avoid the blocking problem. The protocol does not require Byzantine fault tolerance; it assumes honest (fail-stop) failures and synchronous communication for the critical voting phase.
Key facts
- Operates in two phases: prepare (voting) and commit/abort (decision execution).
- Requires a single coordinator that may become a single point of failure.
- Participants that vote Yes are blocked until the coordinator sends the final decision, potentially causing indefinite waiting.
- Standardized in X/Open XA (Distributed Transaction Processing: The XA Specification).
- Guarantees atomicity but not availability in the presence of a coordinator crash.
How it works in practice
Related terms
References
More in Databases
ACID
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties guaranteeing reliable database transaction processing, ensuring data integrity despite concurrent access or failures.
B-Tree Index
A B-Tree index is a self-balancing tree data structure that maintains sorted data for efficient insertion, deletion, and range queries in database systems.
BASE
BASE is a design philosophy for distributed databases that prioritizes availability and partition tolerance over immediate consistency, making it a looser alternative to ACID in NoSQL systems.
CAP Theorem
CAP theorem states that a distributed data system cannot simultaneously provide consistency, availability, and partition tolerance; it can only guarantee two of the three.
Connection Pool
A managed cache of database connections that applications reuse rather than opening and closing a connection for each query, reducing latency and server load.
Failover
Failover is the process of automatically or manually promoting a replica database to primary when the active node fails, ensuring continued availability.
Foreign Key
A column or set of columns in a database table whose values must match a primary key in another table, ensuring referential integrity between the two tables.
Hash Index
A data structure that maps keys to storage locations using a hash function, providing constant-time equality lookups but no ordered or range scans.
Materialized View
A database object that stores the precomputed result of a query as a table, refreshed periodically or on demand to improve read performance and reduce computational overhead.
NoSQL
NoSQL is a family of non-relational database systems designed for flexible schemas, horizontal scaling, and high-throughput data access that traditional SQL databases cannot easily provide.