Bird
Raised Fist0
HLDsystem_design~12 mins

Saga pattern for distributed transactions in HLD - Architecture Diagram

Choose your learning style9 modes available
System Overview - Saga pattern for distributed transactions

The Saga pattern manages distributed transactions by breaking them into smaller steps, each handled by a separate service. It ensures data consistency across services by executing compensating actions if any step fails, avoiding the need for a global lock.

This system supports long-running transactions that span multiple microservices, providing reliability and fault tolerance.

Architecture Diagram
User
  |
  v
API Gateway
  |
  v
+-------------------+
|   Order Service    |
+-------------------+
  |          |
  v          v
+---------+  +----------------+
| Payment |  | Inventory      |
| Service |  | Service        |
+---------+  +----------------+
  |             |
  v             v
+-------------------+
|  Compensation     |
|  Coordinator      |
+-------------------+
Components
User
actor
Initiates the distributed transaction request
API Gateway
api_gateway
Receives user requests and routes them to the Order Service
Order Service
service
Starts the saga by creating an order and coordinating subsequent steps
Payment Service
service
Processes payment as part of the transaction
Inventory Service
service
Reserves inventory for the order
Compensation Coordinator
service
Manages compensating actions to undo previous steps if a failure occurs
Request Flow - 10 Hops
UserAPI Gateway
API GatewayOrder Service
Order ServicePayment Service
Payment ServiceOrder Service
Order ServiceInventory Service
Inventory ServiceOrder Service
Order ServiceUser
Order ServiceCompensation Coordinator
Compensation CoordinatorPayment Service
Compensation CoordinatorInventory Service
Failure Scenario
Component Fails:Inventory Service
Impact:Inventory reservation fails causing the overall transaction to fail after payment is processed
Mitigation:Compensation Coordinator triggers rollback on Payment Service to refund payment and informs Order Service to notify the user of failure
Architecture Quiz - 3 Questions
Test your understanding
Which component initiates the rollback process when a transaction step fails?
AAPI Gateway
BCompensation Coordinator
CUser
DOrder Service
Design Principle
This architecture demonstrates the Saga pattern, which manages distributed transactions by coordinating local transactions and compensations across microservices. It avoids global locks and ensures eventual consistency through compensating actions, improving system scalability and fault tolerance.