In today’s web world with an increase in the load to monolithic web application a new system design came into existence that is called microservice architecture design. Microservice architecture design helps us to structure an application as a collection of services that are loosely coupled and independently deployable. As there are many benefits of using microservice architecture but it too comes with some challenges. One of the main challenges is to handle the distributed transactions.

In this tutorial, we will discuss about the distributed transactions and how to handle it using the Saga pattern.

Assuming we have three microservices (service, service2, and service3) and based on some transactions that happened in service1, it will trigger an event which calls the next microservice i.e. step 2 and similarly service2 will call service3 microservice.

1. service1

2. service2

3. service3

and we have a distributed transaction use case.

1. If service1 transaction succeeds call service2 and if service2 transaction succeeds call service3 and if service3 transaction succeeds mark the transaction as completed.

2.If service1 transaction succeeds call service2 and if service2 transaction fails mark the transaction as failed and rollback the service1 transaction.

3.If service1 transaction succeeds call service2 and if service2 transaction succeeds call service3 and if service3 transaction fails mark the transaction as fail and rollback the service2 and service1 transaction.

4.If service1 transaction fails mark the transaction as fail and rollback the transaction.

This is where Saga pattern comes to rescue.

Saga pattern is an asynchronous and event-driven design pattern which consists of a sequential/parallel chain of microservices (steps) which are been called based on some events(transactions) that occur from within a single microservice.

We can also say Saga pattern is a fail-over and compensating handling pattern, so whenever there is any failure, it will execute the corresponding compensating action to return the initial state from where the Saga flow started.

So to implement the saga pattern one of the best solution is to use AWS step function.

Lets understand what is Step function and how it can fit to our use case.

Step functions are basically an orchestration service for AWS Lambda and activity-based tasks.It makes easy to coordinate microservices using visual workflow.

  • State machine: In simple word the workflow which we build using step function is called a state machine. It describes how to propagate inputs from one state to the next
  • **AWS States: **It’s a way to tell the state machine to execute some task and there are seven types of AWS states you can have:

1. Task

2. Pass

3. Wait

4. Succeed

5. Fail

6. Choice

7. Input and Output

For more details about the task please refer the AWS - AWS Step Function

Now as we understand state machine consist of different states and states consist of task.

Let’s develop the state machine for our use case.

Firstly we will create Service1 as one of the state as mention in below code

#cloud #aws #design patterns #saga pattern #aws step functions

Saga Pattern: Compensating Distributed Transactions
1.65 GEEK