Tuesday, December 21, 2021

Distributed Transactions

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



Eventual Consistency / SAGA, failure scenario







Tha 𝗔𝗣𝗜 Design 𝗛𝗮𝗻𝗱𝗯𝗼𝗼𝗸

 Here is the 𝟮𝟬𝟮𝟲 𝗔𝗣𝗜 𝗛𝗮𝗻𝗱𝗯𝗼𝗼𝗸 broken down by architecture: 𝟭. 𝗧𝗵𝗲 "𝗥𝗲𝗾𝘂𝗲𝘀𝘁-𝗥𝗲𝘀𝗽𝗼𝗻𝘀𝗲" 𝗧𝗿𝗶𝗼: ...