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,
- What is SAGA Pattern?
- Real World use Case
- Types of SAGA pattern
- Choreography – Basic
- 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,
- Order Services – Place order service
- Payment Service – Payment service for payment
- Restaurant Service – Select restaurants and select dishes.
- Delivery Service – Deliver order.
How will they interact?
The below image will explain the process,
- Client/ UI placed Order using Order service.
- Payment service confirmed about payment received and notify to order service and Restaurant service.
- Restaurant service checks the availability of items and notifies to Order Service and next service Delivery service.
- 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,
- Choreography – Event-based
- 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
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.
- ORDER_CEATE Event register when order placed. Once Order Created ORDER_CREATED event fire.
- 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.
- Restaurant service checks the availability of items and registers vent ORDER_PREPARED.
- Once Order delivers ORDER_REGISTERED event fire and notifies to order service.
Choreography - Proc/Cons
- It is very simple to start as it doesn’t have any coordinator.
- No single point of failure.
- Complex in the large number of microservice and huge documents required to understand interaction flow of microservices.
- 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.
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
- Easily to add new microservices hence highly scalable.
- No possibility of cyclic dependency between microservices
- Single point of failure.
- 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. ๐