Search code examples
spring-bootmicroservicesdistributed-transactions2phase-commit

How to do 2 phase commit between two micro-services(Spring-boot)?


I Have two mico-serives A and B where they connect to seperate database, From Mico-serives A i need to persist(save) objects of both A and B in same transtation how to achive this.

I am using Spring micro-servies with netflix-oss.Please give suggestions on best way to do achive 2 phase commit.


Solution

  • you can not implement traditional transaction system in micro-services in a distributed environment.

    You should you Event Sourcing + CQRS technique and because they are atomic you will gain something like implementing transactions or 2PC in a monolithic system.

    Other possible way is transaction-log-mining that I think linked-in is using this way but it has its own cons and pros. for e.g. binary log of different databases are different and event in same kind of database there are differences between different versions.

    I suggest that you use Event Sourcing + CQRS and string events in an event-store then try reaching eventual consistency base on CAP theorem after transferring multiple events between micro-service A and B and updating domain states in each step.

    It is suggested that you use a message broker like ActiveMQ, RabbitMQ or Kafka for sending event-sourced events between different microservices and string them in an event store like mysql or other systems.

    Another benefit of this way beside mimicking transactions is that you will have a complete audit log.