Distributed Transactions: Two-Phase Commit Protocol



3743 views Distributed Systems



Distributed Transactions are essential to have strong consistency in a distributed setup.

An example could be as simple as a 10-min food/grocery delivery- where to guarantee a 10-min delivery, you can only accept orders when there are goods available in the dark store, and a delivery agent is available to deliver the goods.

This is a classic case of Distributed Transaction where you need a guarantee of atomicity and consistency across two different services. In a distributed setup, we can achieve it using an algorithm called Two-phase Commit.

The core idea of 2PC is: Split the transaction into two phases: Reservation and Assignment.

Phase 1: Reservation

The Order service will first talk to store service to reserve food items and delivery service to reserve a delivery partner. When the food or delivery partner is reserved, they are not notified. By reserving them, we are just making them unavailable for everyone else.

If the order service fails to reserve any of these, we roll back the reservation and abort the transaction informing the user that the order is not placed. Reservation comes with a timer, which means if we cannot assign a reserved food item to order in “n” minutes, we will be releasing the reservation, making them available for other transactions.

We move forward to the Commit phase only when the order service reserves both- a food item and a delivery agent.

Phase 2: Commit

In the Commit phase, the order services reach out to the store service and the delivery service to assign the food and agent to the order. Because the food and the agent were reserved, no other transaction could see it, and hence with a simple assignment, we can get the reserved food and agent assigned to an order.

Upon this assignment, the store and the delivery agent are notified about the order and proceed with their respective duties.

We retry a few times if any of the assignments fail (which could happen only if the service goes down). If we still cannot get the assignment done, we inform the user that the order cannot be placed.

The order is placed only after the food item, and the delivery agent is assigned to the order.


Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 14000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.




Other videos that you might like


Implementing Distributed Transactions using Two Phase Commit Protocol

1626 views 83 likes 2022-03-30

Previously, we built a theoretical foundation of Distributed Transaction using the Two-Phase Commit protocol. In this vi...

Distributed Transactions: Two-Phase Commit Protocol

3743 views 163 likes 2022-03-28

Distributed Transactions are tough and intimidating. It is hard to guarantee atomicity across microservices given the ne...


Arpit's Newsletter read by 14000+ engineers

🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.



  • v10.6.4
  • © Arpit Bhayani, 2022