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

  • 4 Key Observability Metrics for Distributed Applications
  • The Principles of Planning and Implementing Microservices
  • Monolithic Decomposition and Implementing Microservices Architecture
  • Building AMQP-Based Messaging Framework on MongoDB

Trending

  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Start Coding With Google Cloud Workstations
  • Introduction to Retrieval Augmented Generation (RAG)
  • System Coexistence: Bridging Legacy and Modern Architecture
  1. DZone
  2. Data Engineering
  3. Databases
  4. Distributed Transactions With the 2PC Protocol

Distributed Transactions With the 2PC Protocol

While distributed transactions are important to microservices, they're often misunderstood. In this post, we look to iron out these misunderstandings.

By 
Pantelis Papapoulias user avatar
Pantelis Papapoulias
·
Feb. 07, 19 · Analysis
Likes (11)
Comment
Save
Tweet
Share
23.2K Views

Join the DZone community and get the full member experience.

Join For Free

Imagine someone asking you to build an atomic business transaction that requires state modification of your application's database and another service via a REST API. It seems to be pretty straight forward and easy to design and implement, but the devil is always in the details.

Let’s try and design what seems to be obvious. The system first updates the database and then executes the HTTP request right before committing the database transaction. If the HTTP request fails, normally with an exception, the database transaction will rollback and so you can guarantee data consistency. This design decision is something that you are going to regret when this feature reaches production. The reason is called distributed transactions that take place whenever a state modification is performed by two or more remote systems within the boundaries or the same business transaction. Let’s do further analysis over some negative scenarios that can potentially lead to data inconsistency:

  1. The HTTP request fails with a timeout before the application receives the response. The operation has been executed successfully by the remote service, although the application failed to get a response and handled the exception by rolling back the database transaction. 
  2. The HTTP request is successful but the database transaction failed to commit, due to a timeout error or even an application crash.

Additionally, another aspect to consider is the bad utilization of the application resources such as database connections while waiting for the HTTP response from the remote service.

But How Can We Guarantee That the System Is Consistent? 

The two-phase commit protocol consists of two phases:

  1. The commit request or voting phase.
  2.  The commit phase, where based on the voting, the system decides to commit or abort the transaction.

2PC protocol

For our use-case, the protocol implementation will require the following:

  1. A database transaction to persist the request and the HTTP request to the remote service as part of the voting phase.
  2. A second database transaction that will modify the database state and accept or fail the business operation.

With this design, the HTTP request is not within any database transaction boundaries and our voting phase is based on the HTTP response to determine the state of the overall business transaction. So in a happy scenario where the HTTP response status is 2xx, the system will persist the operation as successful in the database. If the HTTP request is rejected, for example, it failed with a 400 HTTP response, then the operation is considered to have failed and is persisted in the database. 

But now let’s see if we made any progress with the edge case scenarios of our previous design. If we receive a timeout of the HTTP request, then we got two options based on the system’s requirements:

  1. Retry the HTTP request as a more graceful recovery option so that the request will eventually succeed. This option is valid only if the remote service operation is idempotent so that reprocessing the same request is safe.
  2. Fail fast and reconcile by performing an HTTP request to the remote service to undo the original operation and additionally fail the operation in the database. The undo operation of the remote service must also be idempotent since, during the reconciliation process, it may be executed multiple times due to unexpected errors such as timeout of other system failures.

Both retries and reconciliation require client-side ID generation of the resource or the request. Based on the ID generated by our application the remote service will be able to implement the idempotent behavior or the undo operation.

What if our application or database crashes at any phase? By the time the system recovers it will be able to retry or reconcile the business operation, similar to the request timeout scenario. For this reason, the request is accepted and persisted as part of the first database transaction before the system sends the HTTP request to the remote service.

There are several patterns of handling distributed transactions depending on their complexity, especially in the microservices world. In this article, we cover distributed transactions in the simplest form, which is frequently ignored by engineers. The first and most important step is to identify, design them properly, and never underestimate them.

Requests Database Database transaction Protocol (object-oriented programming) application microservice remote

Opinions expressed by DZone contributors are their own.

Related

  • 4 Key Observability Metrics for Distributed Applications
  • The Principles of Planning and Implementing Microservices
  • Monolithic Decomposition and Implementing Microservices Architecture
  • Building AMQP-Based Messaging Framework on MongoDB

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!