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.
Popular reads
View All
Cursor Skills: How to Create and Use Agent Skills
Jun 23, 2026
Payment System Design: Ledger, Idempotency, and Settlement
Jul 18, 2026
X Algorithm Explained: How the Open Source Recommendation System Works
Jan 22, 2026
Complete Guide to Graph Data Structure: BFS, DFS, Adjacency List vs Matrix
Jan 20, 2026
The Complete HTMX Guide: From Zero to Production
Dec 22, 2025
Transactional Outbox Pattern: Never Lose an Event Again
Apr 07, 2026
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.