Why a saga
Placing an order touches several services: payment, inventory, fulfillment, and notification. A single distributed transaction across them is fragile. A saga splits the work into local steps, each with a compensating action that undoes it.
Orchestration versus choreography
- Orchestration: a central coordinator calls each step and triggers compensation on failure. Easier to trace.
- Choreography: each service reacts to events from the previous one. Looser coupling but harder to follow.
Compensation
If payment succeeds but shipping cannot fulfill, the saga runs the compensation for payment, which is a refund, and releases the reserved inventory. Compensations must be idempotent because they may be retried.
Key idea
Model order placement as a saga of local steps each paired with an idempotent compensating action, so partial failures unwind cleanly without one global transaction.