Understanding the Two-Phase Commit Protocol: A Zomato 10-Minute Delivery Analogy
To understand the Two-Phase Commit Protocol (2PC), let’s use Zomato’s “10-Minute Delivery” as an analogy. In a distributed system like Zomato, ensuring that every step involved in the 10-minute delivery is synchronized is crucial, especially if multiple systems or vendors are involved. Imagine the following scenario for Zomato’s rapid delivery:
Scenario:
A customer places an order on Zomato for a 10-minute delivery. The order needs to involve several entities: Zomato’s database (where orders are tracked), the restaurant (to prepare the food), and a delivery partner (to pick up and deliver the food). We need to ensure that every entity is in agreement before the order proceeds; otherwise, the transaction should be rolled back.
This is where the Two-Phase Commit Protocol comes into play, providing a mechanism to ensure that either all parts of the transaction complete successfully or none at all.
How Two-Phase Commit Works in This Scenario:
1. Preparation Phase (Phase 1):
Step 1: Zomato’s system initiates the order and sends a Prepare request to each participant:
- Restaurant to confirm they can prepare the food within the 10-minute window.
- Delivery Partner to confirm a delivery agent is available and can reach the restaurant within the required time.
- Database to lock the necessary resources (e.g., deduct the customer’s payment, update restaurant stock).
Step 2: Each participant checks if they can fulfill the request:
- Restaurant verifies if they have the ingredients and enough capacity to prepare the order.
- Delivery Partner checks if there’s a delivery agent nearby and available.
- Database ensures the payment can be processed and reserves the customer’s funds.
Step 3: Each participant responds:
- If they can fulfill their part, they send a Yes/Prepared response.
- If any participant cannot fulfill their part (e.g., delivery agent unavailable), they respond with No/Abort.
2. Commit Phase (Phase 2):
Decision Point: Based on the responses, Zomato’s system (acting as the coordinator) decides:
- Commit: If all participants respond with Yes/Prepared, the coordinator sends a Commit request to all participants.
- Abort: If any participant responds with No/Abort, the coordinator sends an Abort request, canceling the order.
Commit Process:
If Commit is chosen, each participant finalizes their actions:
- Restaurant begins food preparation.
- Delivery Partner assigns the agent to pick up and deliver the food.
- Database completes the payment process, deducting the amount from the customer’s balance.
If Abort is chosen, all participants rollback:
- Restaurant does not prepare the order.
- Delivery Partner releases the agent.
- Database releases any holds on customer payment.
Why This Matters
Using the 2PC protocol, Zomato ensures that the customer’s 10-minute delivery experience is only initiated if every party involved confirms they can meet their responsibilities. This avoids situations where the customer is charged, but the restaurant or delivery agent cannot complete the order, ensuring a consistent and reliable order process across all parties involved.