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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Enhance Your Communication Strategy: Deliver Multimedia Messages With AWS Pinpoint
  • How To Set up a Push Notification Service in 30 Minutes or Less
  • A Beginner’s Guide To Building Microservices With AWS Lambda
  • Microservices With Apache Camel and Quarkus (Part 4)

Trending

  • Scaling Microservices With Docker and Kubernetes on Production
  • Modern Test Automation With AI (LLM) and Playwright MCP
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  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.

By 
Kuldeep Pandey user avatar
Kuldeep Pandey
·
Sep. 06, 20 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
7.5K 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.

Related

  • Enhance Your Communication Strategy: Deliver Multimedia Messages With AWS Pinpoint
  • How To Set up a Push Notification Service in 30 Minutes or Less
  • A Beginner’s Guide To Building Microservices With AWS Lambda
  • Microservices With Apache Camel and Quarkus (Part 4)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!