LearnViz CompSci
Home / Learn / Distributed Systems & Cloud / Two-Phase Commit

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

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. 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?
Protocol making distributed transactions atomic. It belongs to Distributed Systems & Cloud and is an intermediate-level topic.
Where do people use Two-Phase Commit?
Distributed databases, XA transactions across databases and message brokers, and some microservice transaction managers use 2PC to maintain atomicity.
What is the most common mistake with Two-Phase Commit?
It is a blocking protocol: if the coordinator crashes after participants have voted yes, they hold locks until it recovers, harming availability.
What should I learn before this topic?
Start with Leader Election, the topic just before it in Distributed Systems & Cloud.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Distributed Systems & Cloud quiz.
Is Two-Phase Commit hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Two-Phase Commit?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Two-Phase Commit?
Leader Election, Saga Pattern, Raft are closely connected. The full list is in the related topics section of this page.
What should I study after Two-Phase Commit?
Continue with Saga Pattern, the next topic in Distributed Systems & Cloud.

Related topics