Two Phase Commit
Also known as:
2PC
XA Transactions
Definition
Two phase commit, or 2PC, is a protocol for committing a transaction across many resources at once. A coordinator first asks every participant if they can commit. If they all say yes, it tells them to commit. If any one says no, it tells them all to abort. 2PC keeps things atomic but can block if the coordinator fails in the middle.
Key Takeaways
- 2PC keeps a transaction atomic across many systems. Either everyone commits or no one does.
- If the coordinator crashes after participants vote yes but before they hear the decision, the participants are stuck waiting.
- 2PC handles crashes but not network splits. That is why modern systems prefer consensus algorithms or sagas for distributed transactions.
- It is still in use inside XA databases, message brokers, and the JTA stack on the JVM.
How It Works
- Phase 1, prepare. The coordinator asks every participant to prepare and write a record saying it can commit.
- Each participant replies yes or no. Yes means it has flushed its redo records and is ready.
- Phase 2, commit or abort. If all votes were yes, the coordinator writes a commit decision and tells everyone to commit. Otherwise it tells them to abort.
- Participants apply the decision, log the outcome, and acknowledge the coordinator.
Where It Is Used
- PostgreSQL supports 2PC through PREPARE TRANSACTION and COMMIT PREPARED for distributed transactions.
- The XA standard and JTA use 2PC to coordinate transactions across databases and message brokers in Java EE.
- Many microservice systems replace 2PC with the saga pattern to avoid the blocking case.