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

Performance Tuning in Mule4 Applications

To achieve optimal performance from your Mule applications, you must evaluate both the applications themselves and the environment in which they run. Although Mule 4 is designed to tune itself, your applications might exhibit performance issues due to their initial construction or dependencies. Similarly, for on-premises installations, you might need to tune the environment itself so that your Mule applications can take full advantage of it. Because many variables influence it, tuning the performance of your application requires some trial and error. You can simplify performance tuning by using documented best practices and testing your applications in ideal test environments. The following recommendations come from the Development and Services Engineering teams and benchmarking efforts by MuleSoft Performance Engineering. Optimizing the performance of your Mule apps requires the following actions: Applying tuning recommendations at the application level        ...

MQ-Based Integration vs. REST API-Based Integration: Choosing the Right Path for Your Architecture

In today's interconnected world, integration is at the heart of seamless operations.  Two of the most popular methods for connecting systems are  1. Message Queue (MQ)-based integration  2. REST API-based integration. But how do you choose the right one for your needs? 🔄 MQ-Based Integration : - Asynchronous Communication : Ensures reliability and resilience, allowing systems to communicate without waiting for an immediate response. Perfect for handling high volumes of data and complex workflows. - Decoupled Systems : MQ allows systems to operate independently, reducing dependencies and enhancing scalability. - Guaranteed Delivery : Messages are queued and delivered even if the destination system is temporarily unavailable, ensuring that no data is lost. 🌐 REST API-Based Integration : - Synchronous Communication : Ideal for real-time, request-response interactions where immediate feedback is needed. - Ease of Use : REST APIs are widely adopted, easy to implement, and pe...

Microservices design patterns

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