Saga Pattern for Microservices Distributed Transactions

In this article, we are going to talk about Design Patterns of Microservices architecture which is The Saga Pattern. As you know that we learned practices and patterns and add them into our design toolbox. And we will use these pattern and practices when designing e-commerce microservice architecture.

By the end of the article, you will learn where and when to apply Saga Pattern into Microservices Distributed Architecture with designing e-commerce application system.

Step by Step Design Architectures w/ Course

I have just published a new course — Design Microservices Architecture with Patterns & Principles.

In this course, we’re going to learn how to Design Microservices Architecture with using Design Patterns, Principles and the Best Practices. We will start with designing Monolithic to Event-Driven Microservices step by step and together using the right architecture design patterns and techniques.

Saga Pattern for Distributed Transactions

The saga design pattern is provide to manage data consistency across microservices in distributed transaction cases. Basically, saga patterns offers to create a set of transactions that update microservices sequentially,
and publish events to trigger the next transaction for the next microservices.

If one of the step is failed, than saga patterns trigger to rollback transactions which is basically do reverse operations with publishing rollback events to previous microservices.

By this way it is manage Distributed Transactions across microservices.
As you know that its used some principles inside of the Saga pattern like publish/subscribe pattern with brokers or API Composition patterns.

The SAGA pattern is especially useful in distributed systems, where multiple microservices need to coordinate their actions. In the context of distributed transactions, the SAGA pattern can help ensure that the overall transaction is either completed successfully, or is rolled back to its initial state if any individual microservice encounters an error.

The saga pattern provides transaction management with using a sequence of local transactions of microservices. Every microservices has its own database and it can able to manage local transaction in atomic way with strict consistency.

So saga pattern grouping these local transactions and sequentially invoking one by one. Each local transaction updates the database and publishes an event to trigger the next local transaction.

If one of the step is failed, than saga patterns trigger to rollback transactions that are a set of compensating transactions that rollback the changes on previous microservices and restore data consistency.

There are two type of saga implementation ways, These are “choreography” and “orchestration”.

Let me explain Choreography way of Saga pattern.

Choreography Saga Pattern

Choreography provides to coordinate sagas with applying publish-subscribe principles. With choreography, each microservices run its own local transaction and publishes events to message broker system and that trigger local transactions in other microservices.

This way is good for simple workflows if they don’t require too much microservices transaction steps.

But if Saga Workflow steps increase, then it can become confusing and hard to manage transaction between saga microservices. Also Choreography way decouple direct dependency of microservices when managing transactions.

Orchestration Saga Pattern

Another Saga way is Orchestration. Orchestration provides to coordinate sagas with a centralized controller microservice. This centralized controller microservice, orchestrate the saga workflow and invoke to execute local microservices transactions in sequentially.
The orchestrator microservices execute saga transaction and manage them in centralized way and if one of the step is failed, then executes rollback steps with compensating transactions.

Orchestration way is good for complex workflows which includes lots of steps. But this makes single point-of-failure with centralized controller microservices and need implementation of complex steps.

Rollback of Saga Pattern

To use the SAGA pattern in a distributed transaction, we can use a compensating transaction to undo the changes made by each microservice in the event of an error. This way, we can ensure that the overall transaction is either completed successfully, or is rolled back to its initial state. Here is an example of how the SAGA pattern could be used in an e-commerce order fulfillment process to handle a rollback scenario:

  1. The customer places an order on the e-commerce website and provides their payment information.
  2. The website’s order fulfillment microservice begins a local transaction and sends a request to the inventory microservice, asking it to reserve the items in the customer’s order.
  3. The inventory microservice begins a local transaction and reserves the items in the customer’s order.
  4. The inventory microservice sends a response back to the order fulfillment microservice, indicating that the items were reserved successfully.
  5. The order fulfillment microservice receives the response from the inventory microservice and, assuming everything went as planned, it charges the customer’s payment method and commits its own local transaction.

However, if any errors are encountered along the way, the SAGA pattern provides a way to roll back the changes made by each microservice. For example, if the inventory microservice encountered an error while reserving the items in the customer’s order, it could send a failure response back to the order fulfillment microservice. In this case, the order fulfillment microservice would then execute a compensating transaction to undo the charges to the customer’s payment method and cancel the order, effectively rolling back the entire fulfillment process.

The below image shows a failed transaction with the Saga pattern.

The Update Inventory operation has failed in the Inventory microservice. So when it failed to one step, The Saga invokes a set of compensating transactions to rollback the inventory operations, cancel the payment and the order, and return the data for each microservice back to a consistent state.

We should careful about when using saga pattern in distributed microservices architecture. If our use case required data consistency across several microservices, and required rollback when one of the step is failed, than we should use Saga pattern.

As a result, SAGA pattern is a powerful tool for managing distributed transactions in a microservice architecture. By using compensating transactions, the SAGA pattern allows us to ensure that the overall transaction is either completed successfully, or is rolled back to its initial state if any errors are encountered. This can help us build more resilient and reliable distributed systems.

Step by Step Design Architectures w/ Course

I have just published a new course — Design Microservices Architecture with Patterns & Principles.

In this course, we’re going to learn how to Design Microservices Architecture with using Design Patterns, Principles and the Best Practices. We will start with designing Monolithic to Event-Driven Microservices step by step and together using the right architecture design patterns and techniques.

--

--

Mehmet Ozkaya
Design Microservices Architecture with Patterns & Principles

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices https://github.com/mehmetozkaya