By WalkingTree   June 24, 2020

What is SAGA pattern and how important is it -

With the adoption of ‘Database Per Service’ in a microservices architecture every service gets its separate database. However, there’s a problem with how to ensure data consistency among different services.  

Let’s take a look at an example here. You are implementing the order function on an e-commerce website. When the user places an order, the application will call for the stock service to update the number of product purchases. This will later go to the shipping service to deliver the product to the user. 

But there’s a problem here. What if the quantity of the product is less than the quantity user ordered? Or what if there’s a problem with shipping? Orders and shipping are quite different databases so there’s no chance of using a local ACID transaction. 

What is SAGA?

SAGA pattern has been used widely for the past two decades. There are many ways of implementing SAGA but we are going to focus on mainly two of them – Choreography and Orchestrator. 

SAGA Choreography 

If you are about dance, then choreography is a sequence of steps or movements. It is also quite similar in software development. Let’s apply the choreography method in an e-commerce application. 

  • Order service has to create a record in the database with the status “Verifying’
  • Order services publish an event ‘Order is successfully placed’
  • The stock services update the number of products
  • Publishes an event ‘Product is updated successfully’ 
  • The status is changed to ‘Shipping’
  • Shipping service creates a record and publishes an event ‘Shipping is registered successfully’ 
  • The status is changed to ‘Finish’

SAGA Orchestration 

In this method, a process manager is added as an orchestrator. This process manager is responsible for identifying the events and triggering the endpoints. Instead of having an order service, stock service, and shipping service in the choreography method, here you can just implement a process manager and all these 2 services will listen to this. 

Read on to know more about SAGA and how to actually implement these 2 methods. 

Recent Blogs

Translate »