← All concepts
Messaging & flow
Saga pattern
Run a multi-service transaction as a series of local steps, undoing completed steps if a later one fails.
Checkout touches inventory, payment and shipping, each with its own database, so a single ACID transaction isn't available. A saga executes each step locally and defines a compensating action (release stock, refund) for when a later step fails.
See it in action
Step through the scenarios on the diagram.
Laying out diagram…
1/4An order is placed as PENDING and the saga starts.
- Order service
- — Starts the saga
- Saga orchestrator
- — Runs steps and compensations
- Inventory
- — Reserve / release stock
- Payment
- — Charge / refund
- Shipping
- — Create shipment
How it works
- Orchestration: a workflow engine calls each step and its compensation.
- Choreography: services react to each other's events.
- Every step and compensation must be idempotent and retryable.
Trade-offs
- Intermediate states are visible (an order is PENDING for a while).
- Compensations must be designed carefully — some actions can't be undone, only offset.