Saga and compensation
Coordinate a business process across local transactions.
The problem and the pattern
A booking workflow may reserve capacity, request payment, then confirm the reservation. If payment fails, release capacity through an explicit compensating action. A saga records progress and recovery decisions. Compensation is a new business action; it is not a database rollback across services.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
Reserve capacity → request payment → confirm booking
Payment fails → release capacity
Payment outcome unknown → reconcile before retrying chargeTrade-offs
- Long-running workflows become observable and recoverable.
- Compensation can fail and needs its own retries and escalation.
- Business stakeholders must define acceptable intermediate states.
When to avoid it
Avoid sagas for work that fits safely into one local database transaction.
Your turn
Payment times out after the provider may have charged the card. Why is immediately charging again dangerous?
Reveal a worked answer
A timeout does not prove failure. Reconcile using the provider operation ID or idempotency key, then decide whether to retry or compensate. Persist workflow state and make each step repeatable.