Saga Pattern is a direct result of Database-per-service pattern. In Database-per-service pattern, each service has its own database. In other words, each service is responsible only for its own data.

This leads to a tricky situation.

Some business transactions require data from multiple services. Such transactions may also need to update or process data across services. Therefore, a mechanism to handle data consistency across multiple services is required.

This situation or use-case forms the basis of the Saga Pattern.

In this series of posts, we will implement Saga Pattern using Axon Framework and Spring Boot.

Axon Framework is a microservices framework that makes it easy to build distributed systems. It provides great support for Spring Boot and we will be using the same to build a sample application.

In Part 1 (this post), we will understand what Saga Pattern really is. We will also look at different types of Saga Implementation. At the end we will also look at the plan for the sample application we are going to build.

In Part 2, we will start building the sample application using Saga Pattern.

Continuing in Part 3, we will continue with our implementation.

Lastly, in Part 4, we will test our application and see the Saga in action.

Defining the Saga Pattern

Saga Pattern proposes implementing distributed transactions in the form of Sagas.

A Saga is nothing but a sequence of local transactions. These local transactions are occurring at the service level. Whenever a local transaction occurs, it publishes a message or an event. Such an event is responsible for triggering the next transaction in the Saga.

But then, you might ask what happens when a single transaction in the Saga sequence fails?

In that case, the Saga executes a series of compensating transactions. These transactions basically undo the changes made by the preceding transactions.

Types of Saga

There are basically two types of Saga. In other words, there are two ways in which you can implement the Saga Pattern.

Choreography-Based Saga

In this type of Saga Implementation, each service publishes one or more domain events. These domain events trigger local transactions in other microservices.

Let’s understand what is happening in the above example.

  • Order Service is responsible for creating an Order. It also publishes an event for the same.
  • The Payment Service listens to that event and creates an Invoice.
  • When the Invoice is created, the Shipping Service creates the shipment.
  • When the Order is shipped, the Order Service updates the status of the Order.

Now, this is a very simple example. In real life situations, there would be many business rules and steps involved to carry out such a distributed transaction. However, this example is only meant to demonstrate how Saga Pattern works.

The important thing to note here is that each service plays its part in the choreography. Each service is basically dependent on the events coming out from other services.

Orchestration-Based Saga

In Orchestration-Based Saga, there is an orchestrator. An orchestrator can also be thought of as a manager that directs the participant services to execute local transactions.

Below is how it can look like for the previous example.

orchestration based saga pattern implementation

Let’s understand what’s happening in this approach:

  • Order Service creates an Order. Then, it also creates the Order Management Saga.
  • The Order Management Saga sends a Create Invoice Command to the Payment Service.
  • The Payment Service creates the Invoice and responds back to the Order Management Saga. Note that these responses can be totally asynchronous and message-driven as well.
  • In the next step, the Order Management Saga issues the Create Shipping Command to the Shipping Service.
  • The Shipping Service does the needful and creates the Shipping. It also replies back to the Order Management Saga.
  • The Order Management Saga changes the status of the Order and ends the Saga’s life-cycle.

Saga Pattern – Benefits and Drawbacks

The biggest benefit of Saga Pattern is that it allows an application to maintain data consistency across multiple services. This is very important from a Microservices Architecture point-of-view because it allows us to write our individual services without tight coupling. In case we run into a case for distributed transactions, we can utilize the Saga Pattern.

However, there are also drawbacks in this approach. The main point going against Saga Pattern is the fact that this pattern makes the programming model more complex. A simple example is developing compensating transactions in case anything goes wrong. Also, developers are usually not completely comfortable with this programming model. At least, not yet.

Other challenges include the topic of atomically updating the Aggregate state as well as publishing domain events.

Usually, Saga Pattern is applied in conjunction with some other patterns such as Event Sourcing and CQRS. These patterns specifically try to handle the scenarios around atomically updating the Aggregate’s state and publish events.

Implementing Saga Pattern

Now that we have understood what is Saga Pattern, we will look at implementing it.

We will be look at Orchestration-Based Implementation. And we will be using the same case as the Order Service described above.

Some of the tools and frameworks we would be using are as follows:

  • Axon Framework – This is a Java-based Microservices Framework that helps build applications using Domain Driven Design techniques. Building Scalable Microservices using Axon Framework describes the framework in more detail.
  • Axon Server – From version 4 onward, Axon has becomes more of a platform. The Axon Platform comprises of the core Axon Framework and also the Axon Server. The Axon Server facilitates communication between microservices.
  • Spring Boot – Spring Boot is probably the most popular Java-based framework for building enterprise application. It’s ease of use and wide integration with other frameworks makes it an ideal choice for building microservices. Building Fastest Production-Ready Microservices using Spring Boot talks in detail about Spring Boot Microservices.

Conclusion

To conclude for this post, we will be using the above frameworks plus a lot of other smaller components to implement Saga Pattern for Order Management.

Till this point, we have explored what Saga Pattern really is. And how it can solve one of the most difficult challenges in moving to a Microservices Architecture.

We also looked at two ways of implementing Saga Pattern – the Choreography Way and the Orchestration Way. We also understood the benefits and drawbacks of Saga implementation.

In the next post, we will start with implementation of the Order Management Saga in the Orchestration-based approach. So stay tuned and let me know your overall thoughts about Saga Pattern in the comments section below.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

16 Comments

Jin Park · January 6, 2020 at 2:16 am

I discovered your post while googling. Cloud-native Java only describes this saga pattern quite briefly; This post helped me understanding concepts and how it works, Thanks for your documentation.

    Saurabh Dashora · January 6, 2020 at 11:56 am

    Hi Jin, Thanks for the wonderful feedback! Happy to help you!

Sandeep Sundaram · February 18, 2020 at 8:05 am

Awesome piece of article , Helped me understand SAGA pattern better.

    Saurabh Dashora · February 18, 2020 at 12:10 pm

    Hi Sandeep,

    Thanks for the great feedback!

Matt Madhavan · March 5, 2020 at 2:14 am

Hey dude!
You never went through the compensating transactions in your articles! what good are the articles then?

Do you want me to complete then for you?

Matt

Marcelo · May 17, 2020 at 4:55 pm

Please fix the links. It’s not working using the current link for the other parts of the article

    Saurabh Dashora · May 19, 2020 at 8:19 am

    Hi Marcelo…fixed the links. Recent migration of the site from staging messed up the links.

varun · May 18, 2020 at 4:49 pm

part 2 link is dead

    Saurabh Dashora · May 19, 2020 at 8:18 am

    Fixed…a recent migration from staging seems to have messed up the links. Thanks!

sindhuja · June 28, 2020 at 12:44 pm

thanks for explaining in detail and i have one question to you ..
In orchestration saga patter does the order management saga will update the data base every time when the other services like payment ,or shipping send their response back to order management upon their completed actions.

    Saurabh Dashora · June 30, 2020 at 2:10 am

    Hi Sindhuja…Glad it was helpful!

    With regards to your query, my answer is that it depends on your business functionality. The Saga itself will keep track of the current status of the steps in the process and for that yes, it will have to update the Saga instance. Since the Saga is orchestrated by the Order Service, this service might choose to do something with the responses received from payment or shipping.

Vinit · July 30, 2020 at 1:23 am

How important the Axon server (middleware) in the Saga pattern? How about messaging systems like RabbitMQ or Azure Service Bus?

    Saurabh Dashora · July 30, 2020 at 8:10 am

    Hi Vinit,

    Axon is basically a framework that makes it easy to write an orchestration-based Saga implementation. It helps perform a lot of heavy-lifting around managing the various commands and events. However, it is not mandatory that you use Axon. You could basically write your own logic to manage the Saga or use any other framework as well.

Meysam · November 5, 2020 at 6:37 am

Thank you for this useful article

    Saurabh Dashora · November 5, 2020 at 9:10 am

    Glad it was helpful!

Sandip · January 23, 2021 at 1:46 pm

Awesome post
Waiting for the next post with its implementation

Leave a Reply

Your email address will not be published. Required fields are marked *