简体   繁体   中英

Spring boot Distrubuted transaction

We need to find best way to address distributed transaction management in our microservices architecture.

Here is the Problem Statement.

We have one Composite microservice which shall interact with underlying other 2 Atomic microservices (Which are meant for specific purpose obviously) and have separate database eg We can consider these 2 microservices as

  1. STUDENT_SERVICE (STU_DB)
  2. TEACHER_SERVICE (TEACHR_DB)

Here in Composite Service Usecase is like user (Administrator) can assign a Teacher to a student for the specific course etc.

I wonder how can we address this problem in one transaction as each servie (STUDENT_SERVICE and TEACHER_SERVICE ) has separate DB and all should happen in one transaction either commit or rollback.

Since those 2 services are separate and I see JTA would not be of help as it is meant for having these 2 applications (services) deployed on same application server!

I have opted out JTA as mentioned above //Pseudo Code class CompositeService{

AssignStaff(resquest){

//txn Start
updateStudentServiceAPI(request);
UpdateTeacherServiceAPI(request);
//txn End
}


} 

System should be in consistent state after api execution

This is a tricky question even it's not obvious at the first sight.

The functionality you call for is understood to be an anti-pattern for microservice architecture.

Microservice architecture is in general a distributed system. Transactions in distributed systems are hard (see https://martin.kleppmann.com/2015/09/26/transactions-at-strange-loop.html ). Your application consists from two services.

The JTA is a Java API for ACID style transactions. ACID transactions usually requires locks to be established in databases. As the transaction spans over multiple services (in your case there are two) then a failure of one service can block processing of the other service. In such case you are loosing the advantage of the microservice architecture - loose coupling and Independence of the services. You can end up of building a distributed monolith (see nice article https://blog.christianposta.com/microservices/the-hardest-part-about-microservices-data/ ).

Btw. there are several discussion on the topic of transactions in microservices here at Stackoverflow. Just search or check eg

What are your options

(disclaimer: I'm a developer for http://narayana.io and presented options are from perspective of Java EE and Narayana. There could be other projects providing similar functionality. Plus, even Narayana integrates nicely with Spring you will possibly need to handle some integration issues.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM