2. HYS Enterprise is a Dutch software
development company with more than
200 talented engineers from all over
the world hys-enterprise.com
UkraineNetherlands
3. My experience
5+ years experience .NET Developer / Team Lead in HYS Enterprise
Saga pattern
8. Saga Pattern
2PC: Disadvantages
- Data stays locked until commit or abort command
- There is no mechanism to rollback other transactions in case one service fails
- Others have to wait until the slowest resource finish its confirmation.
- Bad scaling
10. Saga Pattern
What is Saga?
- Saga is long-lived transaction
- Saga is collection of sub-transactions
11. Saga Pattern
Saga orchestrator
Hotel
Booking
Service
Database 1
Rent Car
Service Database 2
Flight Tickets
Service Database 3
Book Trip Start saga
Start book hotel
End book hotel
Start book car
End book car
Start book tickets
End book tickets
End saga
13. Saga Pattern
Unsuccessful Saga
Start saga
1. Start book hotel
2. End book hotel
3. Start book car
4. End book car
5. Start book flights
6. Flights booking fail
Abort saga
7. Start cancel car
8. End cancel car
9. Start cancel hotel
10. End cancel hotel
End saga
Commit steps
Rollback steps
14. Saga Pattern
Saga pros and cons
Main advantage is that it helps maintain data consistency across
multiple services without tight coupling.
Main disadvantage is the complexity from a programming point
of view. Compensating transaction should be designed.
15. Saga Pattern
Saga tips
- A unique distributed transaction ID should be created for each saga and it
should be present in each related command or event
- Saga commands should be idempotent
- All components should implement compensate commands handlers
- Compensate commands should be idempotent as well
- Avoid synchronous operations