DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Micro Frontends on Monorepo With Remote State Management
  • Five Java Books Beginners and Professionals Should Read
  • Designing a New Framework for Ephemeral Resources
  • What to Pay Attention to as Automation Upends the Developer Experience

Trending

  • Micro Frontends on Monorepo With Remote State Management
  • Five Java Books Beginners and Professionals Should Read
  • Designing a New Framework for Ephemeral Resources
  • What to Pay Attention to as Automation Upends the Developer Experience
  1. DZone
  2. Data Engineering
  3. Databases
  4. Saga Pattern: Compensating Distributed Transactions

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.

Kuldeep Pandey user avatar by
Kuldeep Pandey
·
Sep. 06, 20 · Tutorial
Like (7)
Save
Tweet
Share
6.62K Views

Join the DZone community and get the full member experience.

Join For Free

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
}



microservice AWS

Opinions expressed by DZone contributors are their own.

Trending

  • Micro Frontends on Monorepo With Remote State Management
  • Five Java Books Beginners and Professionals Should Read
  • Designing a New Framework for Ephemeral Resources
  • What to Pay Attention to as Automation Upends the Developer Experience

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: