Intermediate Topic 25 of 51 Distributed Systems & Cloud
Two-Phase Commit
Protocol making distributed transactions atomic.
How it works
Two-phase commit is an atomic commit protocol that ensures all participants in a distributed transaction either commit or abort. In the prepare phase, the coordinator asks each participant to vote; participants that can commit write their state durably and vote yes, otherwise they vote no. In the commit phase, if all vote yes the coordinator logs and sends commit; if any votes no or times out, it sends abort. Participants that voted yes must wait for the decision.
Where it is used
Distributed databases, XA transactions across databases and message brokers, and some microservice transaction managers use 2PC to maintain atomicity.
Common pitfall
It is a blocking protocol: if the coordinator crashes after participants have voted yes, they hold locks until it recovers, harming availability. It adds latency with extra round trips and logging. Cross-service use in microservices couples them tightly. Alternatives are three-phase commit, consensus-based commit and the saga pattern, which accepts eventual consistency.
Where it fits in the course
Two-Phase Commit is topic 25 of 51 in Distributed Systems & Cloud, rated Intermediate. It follows Leader Election. The next topic is Saga Pattern.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Distributed Systems & Cloud quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Distributed Systems & Cloud quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Two-Phase Commit?
Where do people use Two-Phase Commit?
What is the most common mistake with Two-Phase Commit?
What should I learn before this topic?
How can I practice it?
Is Two-Phase Commit hard to learn?
How long does it take to learn Two-Phase Commit?
Which topics are related to Two-Phase Commit?
What should I study after Two-Phase Commit?
Related topics
- Leader Election (Distributed Systems & Cloud)
- Saga Pattern (Distributed Systems & Cloud)
- Raft (Distributed Systems & Cloud)
- Consistent Hashing (Distributed Systems & Cloud)
- Locking and Two-Phase Locking (Databases)