Distributed transaction among services in a microservice system, using spring-cloud

Viewed 1669

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:

  1. Http client would make a request to service SA, to save a record RA, into DA.
  2. Then, SA send a request to service SB, to save a record RB, into DB.

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 2PC protocol).
  • 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 2PC protocol) still work?
  • If not, what is the preferred solution?
    Possible solutions I can guess:
    1. Refactor code to move the 2 operations into a single service.
    2. Implementat the mq + compensation architecture to support this.
1 Answers

Maybe this project is helpful for you https://github.com/apache/servicecomb-pack

Apache ServiceComb Pack is an eventually data consistency solution for micro-service applications. ServiceComb Pack currently provides TCC and Saga distributed transaction co-ordination solutions by using Alpha as a transaction coordinator and Omega as an transaction agent

Related