What is a distributed transaction?
Transactions that span over multiple physical systems or computers over the network, are simply termed Distributed Transactions. In the world of microservices a transaction is now distributed to multiple services that are called in a sequence to complete the entire transaction.
Here is a monolithic e-commerce system using transactions:
Here is the e-commerce system decomposed as microservices:
Two approaches
- Two-Phase Commit
- Eventual Consistency and Compensation / SAGA
1. Two-Phase Commit
Handling transactions in two stages, a prepare phase and a commit phase. One important participant is the Transaction Coordinator which maintains the lifecycle of the transaction.
Successful Two Phase commit on Microservices
Failed Two Phase commit on Microservices
2. Eventual Consistency and Compensation / SAGA
Each service publishes an event whenever it updates its data. Other service subscribe to events. When an event is received, a service updates its data.
Eventual Consistency / SAGA, success scenario