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

Pantelis Papapoulias user avatar by
Pantelis Papapoulias
·
Feb. 07, 19 · Analysis
Like (11)
Save
Tweet
Share
22.27K 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.

Popular on DZone

  • Microservices Discovery With Eureka
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • What Is a Kubernetes CI/CD Pipeline?
  • Deploying Java Serverless Functions as AWS Lambda

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
  • +1 (919) 678-0300

Let's be friends: