Two phase commit vs Saga Pattern
2PC <준비>는 prepare이고 <결과 반영>은 커밋(commit) 이다. 그리고 독립적인 일을 시작하는 것은 begin(<시작>)이고 준비 이전까지 일을 끝내는 것은 end(<끝>) 라고 한다. 이를 요약하면begin -> end -> prepare -> commit
와 같은 수행 단계를 거쳐서 하나의 독립적인 일을 처리하게 되고, 독립적인 일들을 묶어서 하나의 단위로 처리하는 것이 바로 트랜잭션이다. 트랜잭션이 성공적으로 끝나기 위해서는 트랜잭션에 참여(participate)한 모든 독립적인 일들이 <준비(prepare)>가 되어야 한다. 그렇게 모든 일이 <준비>가 된 상태에서만 각각 커밋을 해서 트랜잭션이 성공적으로 끝나는 것이다.
Distributed transactions in micro-services & AWS Step Functions as a SAGA
ORCHESTRATOR
TransactionIn a monolith, everything gets deployed together. With microservices, we split the work between multiple systems. Microservices can give us increased agility and scalability But distributed systems can be harder to coordinate
분산시스템 Challenges:
- 관리포인트 증가
- Monitoring
- Data Architecture Challenges – Transaction.
- Technology lock-in
- polyglot persistence
- Transactional integrity
- Consistency Availability Partition tolerance
C A P 를 동시에 만족하는것은 불가능.
Transaction
Many enterprise applications use transactions to ensure consistency and isolation.
* Atomicity* Consistency* Isolation* Durability
two-phase commit
2PC is not an option!
Guarantees consistency. BUT>>>>>SAGA Pattern
SAGAs
Local transaction.
ACIDSaga & Saga 사이는 Isolation이 보장 안됨
Compensated transactions
Local transaction 개별 트랜젝션 (보상 동작)
Compensation (실패에 의해서 trigger). Triggered by failure
all of nothing
SAGA: Every Transition has a Compensation.
Choreography SAGAOrchestrated SAGAMixing Style – Choreography, Orchestrated, 2PC
ACID transactions can simply rollback.
Buit Saga – Lack of isolation (ACD)개발자가 rollback을 디자인 해야함Careful design required.데이터
Rollback
분산 되어있는 서비스의 Transiction 무결성 보장
Two-Phase Typically, 2PC is for immediate transactions. Typically, Sagas are for long-running transactions.
보상이나 재실행을 할수 없는 트랜젝션Pivot 트랜젝션
Orchestrated SAGAAWS Step FunctionsConductor
AWS Step Functions
Managed Service.
AWS Step Functions – The Basics
Practice
- Function 1: sub-numbers-lam-developarn:aws:lambda:us-east-1:294043514821:function:sub-numbers-lam-develop
- Function 2: greater-than-la-developarn:aws:lambda:us-east-1:294043514821:function:greater-than-la-develop
- Function 3: less-than-lambd-developarn:aws:lambda:us-east-1:294043514821:function:less-than-lambd-develop
- Function 4: final-state-lam-developarn:aws:lambda:us-east-1:294043514821:function:final-state-lam-develop
Step Function
{
"Comment": "Example of ChoiceState using AWS StepFunctions",
"StartAt": "SubNumbers",
"States": {
"SubNumbers": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:294043514821:function:sub-numbers-lam-develop",
"Next": "ChoiceState"
},
"ChoiceState": {
"Type" : "Choice",
"Choices": [
{
"Variable": "$.number",
"NumericGreaterThan": 100,
"Next": "GreateThan"
},
{
"Variable": "$.number",
"NumericLessThan": 100,
"Next": "LessThan"
}
],
"Default": "EqualTo"
},
"GreateThan": {
"Type" : "Task",
"Resource": "arn:aws:lambda:us-east-1:294043514821:function:greater-than-la-develop",
"Next": "FinalState"
},
"LessThan": {
"Type" : "Task",
"Resource": "arn:aws:lambda:us-east-1:294043514821:function:less-than-lambd-develop",
"Next": "FinalState"
},
"EqualTo": {
"Type": "Fail",
"Cause": "No Matches!"
},
"FinalState": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:294043514821:function:final-state-lam-develop",
"End": true
}
}
}
Hands-on lab #2: Managing Long-Lived Transactions with AWS Step Functions
https://hackmd.io/wDuygnSvS_Wanrc7XEoD-g#Hands-on-lab-2–Managing-Long-Lived-Transactions-with-AWS-Step-Functions