Saga design pattern:
Implement each business transaction that spans multiple services as a saga. A saga is a sequence of local transactions. Each local transaction updates the database and publishes a message or event 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.

There are two ways of coordination sagas:
Choreography - each local transaction publishes domain events that trigger local transactions in other services
Orchestration - an orchestrator (object) tells the participants what local transactions to execute
It then emits an Order Created event
The Customer Serviceās event handler attempts to reserve credit
It then emits an event indicating the outcome
The OrderServiceās event handler either approves or rejects the Order
Orchestration - an orchestrator (object) tells the participants what local transactions to execute
Choreography based SAGA:
An e-commerce application that uses this approach would create an order using a choreography-based saga that consists of the following steps:The Order Service receives the POST /orders request and creates an Order in a PENDING stateIt then emits an Order Created event
The Customer Serviceās event handler attempts to reserve credit
It then emits an event indicating the outcome
The OrderServiceās event handler either approves or rejects the Order
Orchestration based SAGA:
An e-commerce application that uses this approach would create an order using an orchestration-based saga that consists of the following steps:The Order Service receives the POST /orders request and creates the Create Order saga orchestratorThe saga orchestrator creates an Order in the PENDING state
It then sends a Reserve Credit command to the Customer Service
The Customer Service attempts to reserve credit
It then sends back a reply message indicating the outcome
The saga orchestrator either approves or rejects the Order
The saga design pattern is used to manage transactions across multiple microservices and maintain data consistency. It's a good choice when you need to:
Maintain data consistency
The saga pattern helps maintain data consistency across multiple services.Avoid a single point of failure
The saga pattern distributes responsibilities across participants, so it doesn't introduce a single point of failure.Handle long-lived transactions
The saga pattern allows you to roll back if an operation fails, without blocking other microservices.Work in a loosely coupled environment
The saga pattern makes transaction management possible in a loosely coupled, message-driven environment.Here are some other things to know about the saga design pattern:
Comments
Post a Comment