The current situation
Got a project using: spring-boot, spring-cloud, postgresql, as a microservice system.
There are 2 services, say SA and SB, they operate on 2 RDBMS databases respectively, say DA and DB.
Now, there is an operation contains 2 sub steps:
- Http client would make a request to service SA, to save a record
RA, intoDA. - Then, SA send a request to service SB, to save a record
RB, intoDB.
As a whole, the 2 sub steps should either both commit, or both rollback.
Analysis
- If move both operations into a single service, then could use Spring's distributed transaction to sovled it with JTA (based on
2PCprotocol). - But here, the 2 operations are in 2 services, and they communicated via http REST protocol. Maybe could use mq + compensation to solve this, but I am not sure is there a better approach.
The questions are
- In this case, does JTA (based on
2PCprotocol) still work? - If not, what is the preferred solution?
Possible solutions I can guess:- Refactor code to move the 2 operations into a single service.
- Implementat the mq + compensation architecture to support this.