Friday, March 5, 2021

Microservices -1 | Eventual Consistency | Transient Exception | Compensating Transaction|Timeout Retry

Hard Consistency - ACID does not apply to microservices
It may have been applicable via 2 Phase Commit in case of Distributed Transaction but the database was still the same 
Now its time for Eventual Consistency


 ACID on Local Transactions
Multiple steps in Transaction Boundary - Because its just 1
one fails, all Fails

Distributed Transactions
More than 1 Database 


Using Multiple Local Transaction
One is committed, Another Fails
Compensatory Transaction(Workaround Steps) - Not Always a Good Practice
https://www.youtube.com/watch?v=jGJT1FRYGcY
Stale Data - Multiple Write -> Dual Write


Begin Txn
Lock
Mutate
Release Lock (Commit)  or Rollback ---- Goto State


https://www.youtube.com/watch?v=pSz0Fpvu2Gk
Older Distributed Transaction or Monolithic Architecture
ACID Principles to Multiple External System
2 Phase Commit - Distributed Transactions - Not a Good Fit for Microservices


Distributed Transaction - how ??
Saga
2 Phase Commit --- -Prepare and Commit --> 2 Phases
Prepare1 -> Prepared1   -> Commit1
Prepare2 -> Prepared2   ->  Commit2
Commit1 + Commit2 = Commit ----Txn Cordinator ---> Application


2 Phase Commit - Discouraged in Microservices -- HTTP Slowness, LOCKED Resource , Slow
Co-ordinator Fails ? What Then ?


3 Phase allows any Participant to be a Co-ordinator


2Phase / 3-Phase - biggest drawback is - they have a wrapper aroun TXN -
So its long living and synchronous


2 Phase is Okay for - Simple Monolithic Application
But Microservices is already slow,uses HTTP and now u put 2phase commit , Its worse


Asynchronous -- SAGA
Txn1 -> Message 
Txn2 -> Message (If Failure, Then Rollback Message)



https://youtu.be/H6F4BorD49g   - Hussein Nasser

What is a Distributed Transaction in Microservices?


What 3 Methods

1)Atomic Clocks - Rollback The steps
2)Event Based - Kafka/Rabbit MQ -> Publish/Subscribe -> Reverse 
3)MiniMonolith - MacroService [Combine homogenous txns in 1 Service]
Distributed Transactions - Txns between Multiple Processes - Each with their own commit/rollback

Multiple Local Transactions - Dual Write - Inconsistencies


MiniMonolith - MacroService
Monorepo
Microservices

No comments:

Post a Comment

Azure - Pipeline - Add Approver for Stage

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass