Description
The SAGA pattern is a failure management pattern for distributed transactions. It manages data consistency across microservices in distributed transaction scenarios. A SAGA is a sequence of local transactions. Each local transaction updates the database and publishes an event or message to trigger the next local transaction in the SAGA. If a local transaction fails because it violates a business rule then the SAGA executes a series of compensating transactions that undo the changes that were made by the preceding local transactions.
Where to use
- Managing data consistency across microservices without distributed transactions. \n
- Complex workflows like order processing. \n
- Systems where eventual consistency is acceptable.
Real World Example
E-commerce Order Processing: Order Service -> Payment Service -> Shipping Service. If Shipping fails, the system executes compensating transactions to refund payment and cancel the order.
Code Example
# Code Concept for SAGA Pattern
# This pattern is architectural.
class Service:
def execute(self):
# Implementation logic
pass
