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

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • From Monolith to Containers: Real-World Migration Blueprint
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Build a Stateless Microservice With GitHub Copilot in VSCode

Trending

  • Web Crawling for RAG With Crawl4AI
  • Privacy-Preserving AI: How Multimodal Models Are Changing Data Security
  • It Costs That Much Because Observability Takes Hours
  • How to Use AI With WordPress
  1. DZone
  2. Data Engineering
  3. Data
  4. Saga Pattern | How to Implement Business Transactions Using Microservices - Part II

Saga Pattern | How to Implement Business Transactions Using Microservices - Part II

After seeing the challenges of implementing distributed transactions and how to implement the Saga pattern, let's look at its Command/Orchestration sequencing logic.

By 
Denis W S Rosa user avatar
Denis W S Rosa
·
Feb. 07, 18 · Tutorial
Likes (16)
Comment
Save
Tweet
Share
30.9K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous post, we saw some of the challenges of implementing distributed transactions and how to implement Saga's pattern using the Event/Choreography approach. In this article, let's talk about how to address some of its problems like complex transactions or cyclic dependencies of events by using another type of Saga's implementation called Command or Orchestration.

Saga's Command/Orchestration Sequencing Logic

In the orchestration approach, we define a new service with the sole responsibility of telling each participant what to do and when. The saga orchestrator communicates with each service in a command/reply style telling them what operation should be performed.

Let's see how it looks like using our previous e-commerce example:


  1. Order Service saves a pending order and asks Order Saga Orchestrator (OSO) to start a create order transaction.
  2. OSO sends an Execute Payment command to Payment Service, and it replies with a Payment Executed message
  3. OSO sends a Prepare Order command to Stock Service, and it replies with an Order Prepared message
  4. OSO sends a Deliver Order command to Delivery Service, and it replies with an Order Delivered message

In the case above, Order Saga Orchestrator knows what is the flow needed to execute a "create order" transaction. If anything fails, it is also responsible for coordinating the rollback by sending commands to each participant to undo the previous operation.

A standard way to model a saga orchestrator is a State Machine where each transformation corresponds to a command or message. State machines are an excellent pattern to structure a well-defined behavior as they are easy to implement and particularly great for testing.

Rolling Back in Saga's Command/Orchestration

Rollbacks are a lot easier when you have an orchestrator to coordinate everything:


Benefits and Drawbacks of Using Saga's Command/Orchestration Design

Orchestration-based sagas have a variety of benefits:

  • Avoid cyclic dependencies between services, as the saga orchestrator invokes the saga participants but the participants do not invoke the orchestrator
  • Centralize the orchestration of the distributed transaction
  • Reduce participants complexity as they only need to execute/reply commands.
  • Easier to be implemented and tested
  • The transaction complexity remains linear when new steps are added
  • Rollbacks are easier to manage
  • If you have a second transaction willing to change the same target object, you can easily put it on hold on the orchestrator until the first transaction ends.

However, this approach still has some drawbacks, one of them is the risk of concentrating too much logic in the orchestrator and ending up with an architecture where the smart orchestrator tells dumb services what to do.

Another downside of Saga's Orchestration-based is that it slightly increases your infrastructure complexity as you will need to manage an extra service.

Saga Pattern Tips

Create a Unique ID per Transaction

Having a unique identifier for each transaction is a common technique for traceability, but it also helps participants to have a standard way to request data from each other. By using a transaction Id, for instance, Delivery Service could ask Stock Service where to pick up the products and double check with the Payment Service if the order was paid.

Add the Reply Address Within the Command

Instead of designing your participants to reply to a fixed address, consider sending the reply address within the message, this way you enable your participants to reply to multiple orchestrators.

Idempotent Operations

If you are using queues for communication between services (like SQS, Kafka, RabbitMQ, etc.), I personally recommended you make your operations Idempotent. Most of those queues might deliver the same message twice.

It also might increase the fault tolerance of your service. Quite often a bug in a client might trigger/replay unwanted messages and mess up with your database.

Avoiding Synchronous Communications

As the transaction goes, don't forget to add into the message all the data needed for each operation to be executed. The whole goal is to avoid synchronous calls between the services just to request more data. It will enable your services to execute their local transactions even when other services are offline.

The downside is that your orchestrator will be slightly more complex as you will need to manipulate the requests/responses of each step, so be aware of the tradeoffs.

If you have any questions, feel free to ask me at @deniswsrosa.

microservice

Published at DZone with permission of Denis W S Rosa, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • From Monolith to Containers: Real-World Migration Blueprint
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Build a Stateless Microservice With GitHub Copilot in VSCode

Partner Resources

×

Comments

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
  • [email protected]

Let's be friends: