In this post, I will discuss two phase commit (aka 2PC) distributed transaction commit protocol and some of the problems associated with it.
What is a distributed commit protocol?
A commit protocol is an algorithm used for atomically committing a transaction. Atomicity implies that either all the changes (writes / updates) in the transaction will be committed taking the database to a new state or everything will be rolled-back without changing any state in the system.
Distributed commit protocols are widely used in distributed databases but are generally applicable to all kinds of distributed systems. For example, a distributed OLTP database that supports ACID transactions will have to use such a protocol to atomically commit or rollback a transaction on multiple participating nodes, leaving the database in a consistent state at the end with all committed changes being durable.
Two Phase Commit
As the name suggests, 2PC executes in two separate phases. The general topology of the system has a coordinator node where the transaction originates and two or more participating nodes. Coordinator node is also a participating node. For the sake of this discussion, let’s refer to transaction as a database transaction that comprises of one or more SQL statements (e.g INSERT, UPDATE, DELETE) that attempt to change the state of table / database.
Take this user flow as an example
SQL> INSERT INTO ...
SQL> INSERT INTO ...
SQL> DELETE FROM ...
SQL> UPDATE TABLE T ...
SQL> COMMIT
For a distributed transaction that uses 2PC, COMMIT statement triggers the execution of the protocol
Phase 1 – Request Vote
- Coordinator requests vote from all the participants. The votes will be used to decide if the transaction can be safely committed or needs to be aborted.
- Each participant sends one of the following response types to coordinator
- YES – Participant ready to commit the transaction locally
- NO – Participant wants to abort the transaction. Usually the case when something has gone wrong during the local execution of transaction at the