SAGA Pattern
- Maintains using a sequence of local transactions
- A local transaction updates the database and publishes an event to trigger the next local transaction in sequence.
- If any local transaction fails, then saga triggers a series of rollback transactions to undo the changes done by preceding local transactions and restores consistency.
- Uses PUB/SUB (Message Broker) Pattern or API Composition pattern for orchestration of events.
Two ways of Implementing the SAGA Pattern
a. Choreography Pattern
- 'Coordinate SAGA events using message brokers
- Each service publishes and subscribes to message broker
- If a service completes its work or completes the local transaction, it publishes the event in the message broker. Next microservice reads that event and starts its own local transaction and so on.
- If a transaction fails, then the same way the failing microservice publishes the rollback event, and the preceding microservice will start the rollback and so on.
- This way is good for a small set of microservices.
- All the microservices are decoupled.
b. Orchestration Pattern
- Coordinate SAGA with a centralized controller microservice.
- Centralized microservice execute local transactions in microservices sequentially.
- If one of the steps fails, then it executes the rollback steps with compensating transactions.
- Single point of failure.
- It is good for complex workflows.
Both ways might look similar in the image, but Choreography is working on events independently, and on the other hand, Orchestration is managed and controlled by a single service which orchestration service.
Comments
Post a Comment