Two-phase commit definition
Two-phase commit (2PC) is a protocol used in distributed systems to ensure transactional consistency between several nodes in a network. Two-phase commit is used in databases, computer networking, and transaction processing. In a two-phase commit transaction, 2PC coordinates the decision-making process for committing or rolling back (undoing) a transaction across the involved nodes. A two-phase commit helps ensure the system remains consistent and maintains data integrity.
See also: node
How a two-phase commit works
- A two-phase commit has two distinct phases: prepare and commit.
- In the “prepare phase,” the coordinator sends a “prepare” message to all participating nodes. Examples of a coordinator include a software module, a transaction manager, or a message broker.
- If the participating nodes are ready to commit to the transaction, they reply with a “yes.”
- Once a positive response is received, the coordinator sends a message to the nodes instructing them to “commit” the transaction (known as the “commit phase”).
- Each participating node commits the transaction and sends a message to confirm the transaction has been completed.
- If any nodes are not ready to commit to the transaction, the coordinator sends an “abort” message to all nodes.
- The transaction is rolled back, i.e., each node will undo the changes made during the transaction, restoring the system to its previous state.