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

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

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Build a Stateless Microservice With GitHub Copilot in VSCode
  • From Prompt to Running Microservice: ServiceBricks Step-By-Step
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • Micro-Frontends in a Microservice Architecture

Trending

  • Understanding Java Signals
  • Develop a Reverse Proxy With Caching in Go
  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  1. DZone
  2. Data Engineering
  3. Data
  4. Transactions in Microservices

Transactions in Microservices

Take a look at how a microservice model can apply to everyday transactions.

By 
Ajay Mahajan user avatar
Ajay Mahajan
·
Jul. 16, 16 · Tutorial
Likes (37)
Comment
Save
Tweet
Share
49.9K Views

Join the DZone community and get the full member experience.

Join For Free

A microservice does a business unit of work. Transaction boundaries in most cases will span a business unit of work; hence, the transactions are automatically contained within a service. For example, a "debit savings bank account" operation can be atomic and transaction integrity can be ensured as that operation is within the boundary of service.

However, there are business scenarios that require you to run business units of work across multiple services, written by different teams in different domains.  Since such orchestration spans across boundaries of multiple services, ensuring transaction integrity is a challenge. There are multiple strategies to address this challenge.  

Taking a concrete example of online "credit card bill payment" that we all familiar with, bill payment requires orchestration of two operations, "debit savings bank account" and "credit to credit card account" which are handled by different domains. In this case, "credit card bill payment" is a composite operation across two services.  The debit and credit operations should happen together or not happen at all. So that leads to the classic architecture challenge, how do we ensure transaction integrity of operations that are performed across services that are distributed.

Our instinct is to think of distributed transactions as the strategy coordinated by a composite service "credit card bill payment." However, distributed transactions are wrought with scalability and stability issues, like deadlock concerns and hence not recommended.

Compensating Transactions

A more viable strategy is to see the "credit card bill payment" execute the two operations independently. If the composite service detects a failure in credit operation after executing debit operation, it will have to reverse the debit operation by posting an equivalent credit entry. This credit entry is a compensating transaction. 

 Compensating Transaction

However, there are scenarios that can affect the transaction integrity with the compensation strategy. One of the scenarios is when composite service fails after executing a debit operation before it can execute credit operation. Multiple patterns can be used to handle failure during the transaction. We will discuss three patterns:

  • State Store.
  • Routing Slip.
  • Process Manager.

State Store

Composite service records all the state changes in a state store. In case of failure, state store can be used to find and recover incomplete transactions.

 State Store

Routing Slip

Another popular resilient approach is to make composite "credit card bill payment" operation asynchronous. Composite service creates a message with two request commands (a debit and a credit instruction) in a routing slip. This message is given to the debitSavings service from the routing slip. The debitSavings service executes the first command and enriches the routing slip before handing message to credit service that completes the credit operation. If there is a failure, the message is sent to an error queue, where the composite service can look at state and error status and compensate if required.

Routing Slip

Process Manager

An alternative to routing slip approach is the Process Manager, which listens to events generated by Debit and Credit Operations and decides on compensating or completing the transaction.

Collapsing Operations Within the Same Bounded Context

Taking another example, inter-account transfer between two savings accounts can be traditionally done by calling "Savings Account" microservices twice, viz. once for the debit of origin account and the second time for crediting destination account. It brings similar transaction considerations discussed above. However, in this case, since both debit and credit operations happen on same domain "Savings Account" the "Account Transfer" logic can reside in the same microservice that runs debit and credit operations.  This simplifies transaction boundaries.

Conclusion

Thus, we discussed various patterns to handle transaction concerns in microservices. Based on your use case, the best approach can be taken forward from the above options.

microservice

Opinions expressed by DZone contributors are their own.

Related

  • Build a Stateless Microservice With GitHub Copilot in VSCode
  • From Prompt to Running Microservice: ServiceBricks Step-By-Step
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • Micro-Frontends in a Microservice Architecture

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!