Skip to main content

SAGA Design Pattern - Microservices.




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


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 state
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 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 orchestrator
The 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:

Compensation

The saga pattern uses compensation to handle failures. Compensation is a behavior that executes when something goes wrong, and reverts the workflow to the last known good state.

Coordination

There are two ways to coordinate sagas: choreography and orchestration. In choreography, each microservice coordinates its own local transaction. In orchestration, a central service coordinates the sagas.

Complexity

The saga pattern can be complex to manage, especially if there are many steps in a transaction or the environment is asynchronous



Comments

Popular posts from this blog

Microservices design patterns

Microservices design pattern Next :  saga-design-pattern-microservices

Runtime Fabric (RTF)

MuleSoft's Anypoint Runtime Fabric (RTF) has many features that help with deployment and management of Mule applications: Deployment: RTF can deploy applications to any environment, including on-premises, in the cloud, or in a hybrid setup. It can also automatically deploy Mule runtimes into containers. Isolation: RTF can isolate applications by running a separate Mule runtime server for each application. Scaling: RTF can scale applications across multiple replicas. Fail-over: RTF can automatically fail over applications. Monitoring and logging: RTF has built-in monitoring and logging capabilities to help teams troubleshoot issues and gain insights into application performance. Containerization: RTF supports containerization, which allows applications to be packaged with their dependencies and run consistently across different environments. Integration: RTF can integrate with services like SaveMyLeads to automate data flow between applications. Management: RTF can be managed with A...

Integration Design Patterns

Understanding Integration Design Patterns: Integration design patterns serve as reusable templates for solving common integration problems encountered in software development. They encapsulate best practices and proven solutions, empowering developers to architect complex systems with confidence. These patterns abstract away the complexities of integration, promoting modularity, flexibility, and interoperability across components. Most Common Integration Design Patterns: Point-to-Point Integration: Point-to-Point Integration involves establishing direct connections between individual components. While simple to implement, this pattern can lead to tight coupling and scalability issues as the number of connections grows. Visualizing this pattern, imagine a network of interconnected nodes, each communicating directly with specific endpoints. Publish-Subscribe (Pub/Sub) Integration: Pub/Sub Integration decouples producers of data (publishers) from consumers (subscribers) through a central ...