Microservices Architecture Pattern - SAGA

Introduction

In this article, we are going to discuss the Microservices Architecture pattern - SAGA pattern. This article can be used by beginners, Intermediate and Professional.

We are going to cover,

  1. What is SAGA Pattern?
  2. Real World use Case
  3. Types of SAGA pattern
  4. Choreography – Basic
  5. Orchestration - Basic

What Is SAGA Pattern?

Microservices are very popular and vastly used in today's technology world. Implementing microservices is easy but complications start when the distributed transaction comes into the picture.

SAGA patterns introduce to solve this problem.

Consistency plays a very important role in any transaction. I mean transactions should execute all or nothing. 

Now think about multiple microservices with their database. We need to roll back all completed transactions in case of any microservice failed in between to maintain data consistency to make our business reliable, sustainable, and reach to desire outcome.

USE CASE

Let us assume that we have an online restaurant ordering system.

For Online Restaurant system might have below microservices,

  1. Order Services – Place order service
  2. Payment Service – Payment service for payment
  3. Restaurant Service – Select restaurants and select dishes. 
  4. Delivery Service – Deliver order.

How will they interact?

The below image will explain the process,

Microservices Architecture pattern - SAGA

  1. Client/ UI placed Order using Order service.
  2. Payment service confirmed about payment received and notify to order service and Restaurant service.
  3. Restaurant service checks the availability of items and notifies to Order Service and next service Delivery service.
  4. Once delivery is done, the Delivery service notifies to Order Service.

In case of any microservices failed, the SAGA pattern will roll back transaction from all other microservice to make sure consistency. 

Types of SAGA

SAGA pattern can be implemented using the below 2 ways,

  1. Choreography – Event-based
  2. Orchestration – Command-based.

Choreography – Event-based

In choreography, each microservices interact with each other through the event. In this case, we do not have any centralized service or coordinator.

For the event, we can use event sync like RabbitMQ or MSMQ, etc.

Let's try to discuss the same use case for Choreography.

As we know its event-based and all microservice interact using common queue like RabbitMQ, MSMQ, etc

Microservices Architecture pattern - SAGA

As you can see in about images, Each stage has an event and this event registers in a common queue. The next subsequence service will be executed based on the event completed and register in the queue.

Eg.

  1. ORDER_CEATE Event register when order placed. Once Order Created ORDER_CREATED event fire.
  2. The payment service will see this event and start verifying payment status. If it's paid ORDER_PAID event trigger and notify to Order service and Restaurant service.
  3. Restaurant service checks the availability of items and registers vent ORDER_PREPARED.
  4. Once Order delivers ORDER_REGISTERED event fire and notifies to order service.

Choreography - Proc/Cons

  1. It is very simple to start as it doesn’t have any coordinator.
  2. No single point of failure.
  3. Complex in the large number of microservice and huge documents required to understand interaction flow of microservices.
  4. Integrate testing is difficult.

Orchestration – Command-based.

It is a centralized service to manage transactions of all services and flow.

The orchestration tells each microservices which operations to perform and in case of failure, it also sends messages to different microservices for the rollback of the transaction.

It also manages the state of each task through the state machine.

Let’s discuss the same Restaurant service example here.

Microservices Architecture pattern - SAGA

It is the command-based approach. All command is handled by centralized Service called “Orchestration Service” This service maintain the state of each microservices and give command accordingly. 

Orchestration - Proc/Cons

  1. Easily to add new microservices hence highly scalable.
  2. No possibility of cyclic dependency between microservices
  3. Single point of failure.
  4. Complex due to manage state tasks.

In the next article, I will try to explain the same concept through .net core code. I hope you enjoyed this article and find it useful.

Thanks for reading. 😊


Similar Articles