Master 2 Phase Commit | Microservices Transaction Management Interview Questions | Code Decode

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

Udemy Course discounted link

Mock Interview Invitation form :

**Two-Phase Commit (2 PC)**

2 Phase Commit is a standard protocol to handle distributed transactions using 2 stages namely **Prepare stage and the Commit stage.** There is a transaction coordinator component that coordinates the entire transaction by talking to all the services

consider three microservices: Order Service, Payment Service, and Inventory Service.

1. **Transaction Initiation (Coordinator):**
- The Order Service receives a request to place an order and initiates a transaction.
- The Order Service acts as the coordinator for the transaction.
2. **Voting Phase (Prepare Phase):**
- The Order Service sends a prepare message to all participating microservices, including the Payment Service and Inventory Service.
- Each participating microservice (Payment Service, Inventory Service) checks whether it can fulfill its part of the transaction. If everything is in order, they reply with a vote "Yes." Otherwise, they reply with a vote "No."
3. **Decision Phase (Commit or Abort):**
- If all microservices voted "Yes," the Order Service sends a commit message to all participating microservices.
- If any microservice voted "No," the Order Service sends an abort message to all participating microservices.
4. **Commit Phase:**
- If the decision is to commit, all participating microservices (Order Service, Payment Service, Inventory Service) execute the actual changes needed to fulfill the transaction.
- For example, the Order Service records the order, the Payment Service processes the payment, and the Inventory Service updates the available inventory.
5. **Acknowledgment Phase:**
- After successful execution, each microservice sends an acknowledgment back to the Order Service.
6. **Completion:**
- If all acknowledgments are received, the Order Service considers the transaction complete and sends a response to the client indicating success.
- If any acknowledgment is missing or if any microservice encountered an issue during execution, the Order Service can initiate compensation actions or notify the client of a failure.

**Drawbacks of 2PC**

**Coordination Overhead:**

- 2PC involves a significant amount of coordination between the coordinator and participants. The need for multiple rounds of communication and synchronization can introduce overhead, impacting the overall system performance and scalability.
**Single Point of Failure:**

- The coordinator acts as a single point of failure. If the coordinator fails after participants have committed changes but before it sends the final commit message, the system can be left in an inconsistent state
**Blocking:**
- One of the major drawbacks of 2PC is its blocking nature. During the protocol execution, all participants are blocked from processing other transactions until a final decision (commit or abort) is reached.
- If someone has a problem during this process, like a technical issue or a disagreement, everyone has to wait until the issue is resolved or the decision is made to cancel the entire transaction.
**Blocking during Coordinator Failure:**

- If the coordinator fails after sending the commit message but before participants receive it, participants might be left uncertain about the final decision. In this case, participants can independently decide to commit or abort based on a timeout. If they don't receive a coordinator's message within a certain time frame, they may choose to abort and roll back changes. Hence came 3 PC to rectify some disadvantages.

Java 8 Interview Questions and Answers:

Hibernate Interview Questions and Answers:

Spring Boot Interview Questions and Answers:

Angular Playlist:

Subscriber and Follow Code Decode

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

Not even google has a very good source for this design patterns in Microservices, you are great 😊 is it possible to do one implementation video for CQRS. I think we have one for saga already

karthikeyanrm
Автор

This is simply awesome, complex concepts explained in lucid manner via examples and also failure handling covered in detail

romeharshiiitd
Автор

Was looking for this video from your channel while going through the saga design pattern video. Good job team ❤

sayanbiswas
Автор

Excellent video on 2-phase commit in distributed applications. Could you also make a simple java application connecting to 2 different databases using 2-phase commit which would be more helpful?

srikarsharma
Автор

@Codedecode please implement these distributed transactions concept in this course which is in udemy. As that course seems to be very basic please implement these concepts in that e-commerce project in that course.

hiteshupreti
Автор

if this a protocol how is it actually implemented in organizations with each service having its own db?

srinivasrenukunta
Автор

Where is the complete project description video link for this Udemy course ?

abhishekverma
Автор

Please mam please make video on @Transactional

gyaanbhandaar
Автор

@18:11 with so many issues why will anyone wants to learn or use 2PC ? looks like not fit for purpose? Did I miss any explanation where advantages overthrow these disadvantages ?

croydonH
Автор

consistent explanation is wrong, Consistency: This means that integrity constraints must be maintained so that the database is consistent before and after the transaction. It refers to the correctness of a database

anandbabu
join shbcf.ru