Saga pattern
Сага
A way to run one business operation as a series of transactions that commit independently. If the operation cannot finish, separate actions called compensations counteract the effects of completed steps.
Also known as: распределённые транзакции
What problem does it solve?
Consider booking a trip across separate flight and hotel systems. The flight reservation may commit before the hotel reports that no rooms are available. There is no shared database transaction to roll back. A saga defines both the steps that complete the booking and the actions that handle a partial failure, such as canceling the flight reservation.
Local commits and compensations
Each step commits in the system that owns the data. When a step succeeds, the workflow can move forward. A temporary fault may call for a retry; a confirmed business failure may trigger compensations for earlier commits. Compensation is a new business action, not a database rollback. For example, a refund leaves a record of both the charge and the refund. Think about steps that cannot be undone before choosing an order: it may be safer to hold a seat until a hotel is secured than to issue a nonrefundable ticket first.
Two ways to coordinate a saga
- Orchestration: A workflow component records progress, tells participants which actions to take, and decides how to handle each outcome. Branches and timeouts are easier to see in one place. The orchestrator needs durable state so it can resume after a crash.
- Choreography: Participants react to events from one another. For example, a
FlightHeldevent triggers the hotel reservation, whileHotelUnavailabletriggers release of the flight hold. There is no central workflow controller, but tracing the complete process gets harder as more participants and branches are added.
Worked example: a hotel request times out
Do not treat the timeout as proof that the hotel rejected the request. The hotel system might have committed the reservation and lost the reply. Check the booking status using a stable request ID, or retry with the same idempotency key so the provider does not create a second booking. If the hotel confirms that it cannot reserve a room, release the flight hold. If that release fails, keep the saga in a cancellation-pending state and retry or escalate rather than reporting that it is canceled.
Guarantees and trade-offs
A saga does not give the workflow global ACID isolation. Other operations can see intermediate states: a held seat can be unavailable to another traveler before the trip is confirmed. Protect rules such as “never reserve the same seat twice” in the service that owns the resource. If one database transaction can cover the operation, it is usually simpler.
With messaging, a service could crash after committing its data but before publishing the outcome. A transactional outbox addresses that gap by writing the data change and an outgoing event record in the same local transaction, then publishing the event separately. The event can still be delivered more than once, so forward steps and compensations should be idempotent.
In system design interviews
Walk through the happy path, then pick a failure after a local commit. Explain how the workflow learns what happened, what the user sees while the outcome is uncertain, and how processing resumes if a service restarts.