3-Phase Commit in Distributed Transactions: Must-Know for Microservices Interview Q | Code Decode

preview_player
Показать описание
In this microservice Interview questions and answers for 3 phase commit for distributed transaction management in microservices.

Udemy Course discounted link

Mock Interview Invitation form :

**3 Phase Commit**

** Why 3 PC was introduced?**

In 2 PC, If a few participants sent Ok / yes during voting phase, means they are ready to commit. But they are blocked as they are waiting if all others are also ready.

Now if the coordinator dies, these participants will never get to know that they should commit or roll back. They will be blocked until a Commit or Rollback message is sent from the coordinator or the request timesout. Thus the **2PC technique is a blocking protocol**.

3PC introduces an extra phase (Pre-commit Phase) compared to the traditional 2-phase commit. This extra phase helps** reduce the likelihood of blocking issues** and enhances the overall reliability of the distributed transaction.

If the coordinator fails before sending a preCommit message, other participants will unanimously agree that the operation was aborted.

**3 PC is an extension of a two-phase commit** wherein the commit phase is split into two phases as follows.

**1) Prepare to commit**, After unanimously receiving yes in the first phase of the 2PC / **Voting Phase (Prepare Phase), ** the coordinator asks all participants to prepare to commit. During this phase, all participants acquire locks, etc, but they don’t actually commit.

**2) Do Commit - **If the coordinator receives yes from all participants during the prepare to commit phase then it asks all participants to commit.

The pre-commit phase introduced above helps us to recover from the case when a participant fails or both the coordinator and participant node fail** during the commit phase**.

Steps in 3PC if the coordinator dies in the commit phase -

1) Now when the coordinator fails, **the secondary coordinator **takes charge of coordinating.

2) Then It **queries all the participants about their status**.

3) If it learns that **some nodes are in the commit phase** then it assumes that the previous coordinator before crashing has made the decision **to commit**. Hence it can ask all the participants to commit

4) But if participants says that it doesn’t receive **prepare to commit,** then the new coordinator can assume that the previous coordinator failed even before it started the prepare to commit phase. Hence it can safely assume no other participant would have committed the changes and hence safely abort the transaction.

**Async - In e-commerce **

- The order processing involves multiple services that can function independently. There is no need for a tightly coordinated, all-or-nothing transaction.
- If the payment service fails, the system can initiate a compensating transaction to cancel the order or release the reserved inventory without affecting other services.
- The Saga pattern supports loose coupling and allows for partial rollbacks, making it suitable for complex, distributed workflows.
- Each service (inventory, payment, shipping) handles its part of the transaction independently. If a failure occurs in one service, compensating transactions are triggered to maintain consistency.
In summary, 2PC is suitable for scenarios requiring strong consistency and atomicity, where blocking during the commit phase is acceptable. On the other hand, the Saga pattern is a good fit for scenarios with loosely coupled, independent services, where partial rollbacks and compensating transactions are necessary for handling failures in a distributed environment.

Java 8 Interview Questions and Answers:

Hibernate Interview Questions and Answers:

Spring Boot Interview Questions and Answers:

Angular Playlist:

Subscriber and Follow Code Decode

#3phasecommit #codedecode #microservices
Рекомендации по теме
Комментарии
Автор

I create a analogy for two phase commit and three phase commit as follows, have a look into it,
Coordinator = Traffic Controller
Participants = Cars at an intersection

Two-Phase Commit (2PC) Analogy:
Phase 1: Ask – "Can you cross?"
The coordinator asks each car: "Can you cross?"
Each car checks if its engine is fine, road ahead is clear, and responds “Yes” or “No”
Phase 2: Decide & Act – "Go!"
If all cars say “Yes”, the coordinator says “Go” (commit).
If any car says “No”, the coordinator says “Stop” (abort).
Risk: If the coordinator crashes after telling cars to get ready, but before saying “Go, ” the cars may wait forever, unsure what to do (blocking problem)

Three-Phase Commit (3PC) Analogy:
Phase 1: Ask – "Can you cross?"
Coordinator asks: "Can you cross?"
Cars check readiness and reply “Yes” or “No”
Phase 2: Prepare – "Start your engine, but don’t move yet."
If all cars said “Yes, ” the coordinator tells each car:
“Start your car and be ready to move, but DON’T move yet.”
Cars start their engines and confirm: “I’m ready”
Phase 3: Commit – "Now go!"
If all cars successfully started and are ready, the coordinator says: “Go now!”
Advantage: Even if the coordinator crashes after Phase 2, each car knows it was in “ready” state and can safely time out or decide based on known rules, avoiding the blocking issue. In phase three Cars are already started so their less possibility of failure of Participants.

chetankhandave
Автор

I am the first visitor to this video 😂

chnrbk
Автор

Excellent video 😊 thank you so much Code decode team. please include saga pattern also in this playlist as its in a different one. Kindly do the CQRS as well

karthikeyanrm
Автор

Please try to upload Spring boot microservices implementation for two and three phase commit.

vinaykulkarni
Автор

Please create videos on these models as well

KumarGaurav-xDsoCH
Автор

Thanks. Please create video on fail noisy and fail stop.

sourabhjinde
Автор

That's why microservices fails even in 3pc

SiiitiiFreelancing-jlty
join shbcf.ru