This is the solution to maintain transaction across distributed databases in microservices. There are 2 types of it:
- Choreography
- Orchestration
1. Choreography:
It is a de-centralized form of system where different services interact with messages and pre-defined rules.
Below would be an happy flow example of Choreography Saga design pattern:
Begin Transaction
1. Create order.
2. Reserve product.
3. Process payment.
4. Create shipment.
End Transaction
And below would be an Error Flow example of the above:
Suppose, there is a failure at Payment service due to credit card decline.
Every transaction that was successful before this failure has to do a compensatory transaction.
Begin Transaction
1. Create order.
2. Reserve product.
3. Payment failure.
1. Cancel product.
2. Cancel order.
End Transaction
2. Orchestration:
In Orchestration, a centralized service called orchestrator co-ordinates the entire process.
Below would be an happy flow example of Orchestrator Saga design pattern:
In the below diagram a Saga can be an java class which acts like an orchestrator.
And below would be an Error Flow example of the above: