Saga Pattern
By Alexandr Prygun
Saga Pattern
Play
hys-enterprise.com
HYS Enterprise is a Dutch software
development company with more than
200 talented engineers from all over
the world hys-enterprise.com
UkraineNetherlands
My experience
5+ years experience .NET Developer / Team Lead in HYS Enterprise
Saga pattern
Monolith App
Saga Pattern
API Application Database
One call One transactionIncoming call
Saga Patterm
API
Gateway
Application 1 Database 1
Application 2 Database 2
Application 3 Database 3
Incoming call
SOA/Microservices App
Saga Pattern
2PC: Prepare Phase
API
Gateway
Application 1 Database 1
Application 2 Database 2
Application 3 Database 3
Incoming call
Propose
Vote
Propose
Vote
Propose
Vote
Saga Pattern
2PC: Commit Phase
API
Gateway
Application 1 Database 1
Application 2 Database 2
Application 3 Database 3
Incoming call
Commit/Abort
Done
Commit/Abort
Done
Commit/Abort
Done
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
Saga Pattern
Sagas History
Long Live Transactions
Hector Garcia-Molina, Kenneth Salem,
Princeton University, 1987
Saga Pattern
What is Saga?
- Saga is long-lived transaction
- Saga is collection of sub-transactions
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
Saga Pattern
Saga choreography
Hotel
Booking
Service
Database 1
Rent Car
Service Database 2
Flight Tickets
Service Database 3
Book Trip Start saga
Hotel is booked
Car is booked
End saga
Tickets are booked
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
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.
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
Thank you
For your attention!
Any Questions?
Event Name

Saga pattern

  • 1.
    Saga Pattern By AlexandrPrygun Saga Pattern Play hys-enterprise.com
  • 2.
    HYS Enterprise isa Dutch software development company with more than 200 talented engineers from all over the world hys-enterprise.com UkraineNetherlands
  • 3.
    My experience 5+ yearsexperience .NET Developer / Team Lead in HYS Enterprise Saga pattern
  • 4.
    Monolith App Saga Pattern APIApplication Database One call One transactionIncoming call
  • 5.
    Saga Patterm API Gateway Application 1Database 1 Application 2 Database 2 Application 3 Database 3 Incoming call SOA/Microservices App
  • 6.
    Saga Pattern 2PC: PreparePhase API Gateway Application 1 Database 1 Application 2 Database 2 Application 3 Database 3 Incoming call Propose Vote Propose Vote Propose Vote
  • 7.
    Saga Pattern 2PC: CommitPhase API Gateway Application 1 Database 1 Application 2 Database 2 Application 3 Database 3 Incoming call Commit/Abort Done Commit/Abort Done Commit/Abort Done
  • 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
  • 9.
    Saga Pattern Sagas History LongLive Transactions Hector Garcia-Molina, Kenneth Salem, Princeton University, 1987
  • 10.
    Saga Pattern What isSaga? - Saga is long-lived transaction - Saga is collection of sub-transactions
  • 11.
    Saga Pattern Saga orchestrator Hotel Booking Service Database1 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
  • 12.
    Saga Pattern Saga choreography Hotel Booking Service Database1 Rent Car Service Database 2 Flight Tickets Service Database 3 Book Trip Start saga Hotel is booked Car is booked End saga Tickets are booked
  • 13.
    Saga Pattern Unsuccessful Saga Startsaga 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 prosand 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
  • 16.
    Thank you For yourattention! Any Questions? Event Name