Saga Pattern – Manage distributed transactions between microservices
A transaction is a series of operations that succeed or fails together. In a Monolithic system since a transaction is local, it can be rolled back in case of any failure. ACID is either guaranteed by the database or can be maintained at the application end.
In a distributed system, a single transaction can be divided into multiple services. The problem here is how to maintain data consistency.
Let’s consider an eCommerce application that consists of three stages:
- Placement of an order
- Payment validation/authorization
- Order fulfillment/shipping
Above is a sequence diagram depicting a monolithic application. In case of failure at any stage, the application will roll back the transaction.
Let’s consider a similar flow in a distributed environment. This flow is divided into three services
- Order Service- Receives an order
- Payment Service – Responsible for payment validation/authrization
- Fulfillment Service – Fulfill the order
Each service commits its transaction in its own database. Transaction failure at any service will result in data inconsistency as other services are unaware of failure.
Saga pattern provides a way to achieve data consistency in such a scenario. Each service after updating the database will publish a message to trigger the next local transaction. If any transaction failure happens services will coordinate with each other in a similar fashion to trigger a rollback.
We can implement the saga pattern in one of the two ways
- Choreography – Each service publishes a domain event that triggers the local transaction in the next service. There is a risk of cyclic dependency because each service is consuming each other’s message.
- Orchestration – A centralized orchestrator is responsible for the execution of transactions by each service or executes rollback in case of any failure. Orchestrator contains all business logic required to maintain transaction, in case of large transactions it can become very complex.
Due to the distributed nature of transactions saga pattern is difficult to debug but with many tools available for distributed transaction tracing like Jaeger it is not a road blocker.
Saga pattern increases the complexity by adding functionalities in service to maintain transaction. But at the same time provides data consistency in a distributed environment and increases the availability of a system. It helps in overcoming many challenges that occur due to the distributed nature of the system.
Please let us know about your feedback by writing to us.
Reference – https://docs.microsoft.com/en-us/azure/architecture/reference-architectures/saga/saga