3 Phase commit in Distributed Transaction in Microservice | Interview Question | Code Decode

Опубликовано: 26 Январь 2024
на канале: Code Decode
7,001
107

In this microservice Interview questions and answers for 3 phase commit for distributed transaction management in microservices.

Udemy Course discounted link

https://www.udemy.com/course/master-s...

Mock Interview Invitation form :
https://forms.gle/wHnipR1FWrEDaexGA

*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.


Core Java Interview Questions and Answers:    • Core Java frequently asked Interview ...  

Advance Java Interview Questions and Answers:    • Advance Java Interview Questions  

Java 8 Interview Questions and Answers:
   • Java 8 Interview Questions(New Features)  

Hibernate Interview Questions and Answers:
   • Hibernate Interview Questions Java  

Spring Boot Interview Questions and Answers:
   • Advance Java Interview Questions  

Angular Playlist:
   • Angular Course Introduction || Angular 8  
SQL Playlist:    • SQL Interview Questions and Answers  

GIT:    • GIT  

Subscriber and Follow Code Decode
Subscriber Code Decode: https://www.youtube.com/c/CodeDecode?...
LinkedIn :   / codedecodeyoutube  
Instagram:   / codedecode25  

#3phasecommit #codedecode #microservices


Смотрите видео 3 Phase commit in Distributed Transaction in Microservice | Interview Question | Code Decode онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Code Decode 26 Январь 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 7,00 раз и оно понравилось 10 людям.