{{announcement.body}}
{{announcement.title}}

Saga Pattern: Compensating Distributed Transactions

DZone 's Guide to

Saga Pattern: Compensating Distributed Transactions

This tutorial demonstrates how the Saga pattern can be used in an application with AWS Step Fucntions in a series of conditional transactions.

· Microservices Zone ·
Free Resource

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

Java
 




x
27


 
1
"States": {
2
    "Service1": {
3
      "Type": "Task",
4
       "Resource": "<Transcation event for service1>",
5
      "Catch": [        
6
        {
7
          "ErrorEquals": ["States.ALL"],
8
          "ResultPath": "$.Service1Error",
9
          "Next": "rollback Service1"
10
        }
11
      ],
12
      "ResultPath": "$.Service1Result",
13
      "Next":"Service2"
14
    }


The notable part is Type, Resource (It is used to trigger Transaction event for service1 mostly done via calling AWS lambda function), Catch(this is used to rollback the transaction occurred by service1, for achieving the same we create the new state i.e. rollback Service1), ResultPath(It is helpful for passing the output of first state to another state), and Next(It is use to call the next state from existing state)

Similarly, we can develop the states for Service2 and Service3 along with the rollback states for both the services.

Below I am sharing the final state machine JSON along with AWS generated visual workflow with all the scenario which we have discussed at the starting of the discussion.

I hope now you are able to understand why we called it a compensating transaction.


Visual workflow


Java
 




xxxxxxxxxx
1
135
99


1
{
2
  "Comment": "Saga pattern Using Step Functions",
3
  "StartAt": "Service1",
4
  "States": {
5
    "Service1": {
6
      "Type": "Task",
7
       "Resource": "<Transcation event for service1>",
8
      "Catch": [        
9
        {
10
          "ErrorEquals": ["States.ALL"],
11
          "ResultPath": "$.Service1Error",
12
          "Next": "rollback Service1"
13
        }
14
      ],
15
      "ResultPath": "$.Service1Result",
16
      "Next":"Service2"
17
    },
18
    "Service2": {
19
      "Type": "Task",
20
       "Resource": "<Transcation event for service2",
21
      "Catch": [        
22
        {
23
          "ErrorEquals": ["States.ALL"],
24
          "ResultPath": "$.Service2Error",
25
          "Next": "rollback Servicer2"
26
        }
27
      ],
28
      "ResultPath": "$.Service2Result",
29
      "Next":"Servicer3"
30
    },
31
    "Servicer3": {
32
      "Type": "Task",
33
      "Resource": "<Transcation event for service3>",
34
      "Catch": [        
35
        {
36
          "ErrorEquals": ["States.ALL"],
37
          "ResultPath": "$.Servicer3Error",
38
          "Next": "rollback Servicer3"
39
        }
40
      ],
41
      "ResultPath": "$.Servicer3Result",
42
      "End": true
43
    },
44
    "rollback Service1": {
45
      "Type": "Task",
46
      "Resource": "<Transcation event for rollback service1>",
47
      "ResultPath": "$.rollback Service1Result",
48
      "Next":"Fail"
49
    },
50
    "rollback Servicer2": {
51
      "Type": "Task",
52
       "Resource": "<Transcation event for rollback service2>",
53
 
          
54
      "ResultPath": "$.rollback Servicer2Result",
55
      "Next":"rollback Service1"
56
    },
57
    "rollback Servicer3": {
58
      "Type": "Task",
59
       "Resource": "<Transcation event for rollback service3>",
60
      "ResultPath": "$.rollback Servicer3Result",
61
      "Next":"rollback Servicer2"
62
    },
63
    "Fail": {
64
      "Type": "Fail"
65
    }
66
  }
67
}



Topics:
aws, aws step functions, cloud, design patterns, saga pattern

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}