Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distributed transactions in microservices

I have 2 microservices S1 and S2. S1 invokes S2 to update a data and then S1 inserts another data,But let's consider S1 fails,Then we need to rollback the data updated by S2 or else we'll be in inconsistent state.

I also gone through Saga patterns.will it satisfy this inconsistency

Can anyone suggest any better solutions for this?

like image 220
Ragavan Avatar asked Jul 25 '18 13:07

Ragavan


People also ask

What is distributed transaction system?

A distributed transaction is a set of operations on data that is performed across two or more data repositories (especially databases). It is typically coordinated across separate nodes connected by a network, but may also span multiple databases on a single server.

How transactions are done in microservices?

You then make both microservices share the same database instance. Because the services are written and deployed as libraries in a common runtime, they can participate in the same transactions. Because the modules share a database instance, you can use a local transaction to commit or rollback all changes at once.

What is distributed transaction in spring boot?

Spring Boot supports distributed JTA transactions across multiple XA resources by using either an Atomikos or Bitronix embedded transaction manager. JTA transactions are also supported when deploying to a suitable Java EE Application Server.


1 Answers

Distributed transactions are problematic for most circumstances and they are bad for services

  • Service Boundary – service boundary is a trust boundary. Atomic transactions require holding locks and holding them on behalf of foreign service is opening a security hole (makes it much easier to do a denial of service attack) You cannot assume atomicity between two different entities or resources. Esp. when these resources belong to different businesses.
  • Transactions introduce tight coupling both temporal and operational
  • Transactions hinder scalability – It isn’t that you can’t scale but it is much harder

Sagas (which, by the way, do not necessitate orchestration) emerged as a solution for coordination because they allow services to be more flexible - and are in fact closer to how real life work. Another pattern you can combine with Sagas to help with delaying effects can be reservation.

Another option you have might be reconsidering how you partitioned your services. It might be that the service boundaries you have now are not correct and a redesign will contain the needed transaction into one service

like image 138
Arnon Rotem-Gal-Oz Avatar answered Sep 23 '22 05:09

Arnon Rotem-Gal-Oz