/pattern/saga-compensation/

04 · ProductionRuntime ArchitectureSaga PatternCompensating ActionTransactional WorkflowException Handling & Recovery

Saga / Compensation.
Every forward step ships with its own undo.

A long-running transaction is split into steps, each paired with a compensating action; when a later step fails, the already-committed steps are undone in reverse order via their compensations, because a distributed transaction cannot hold a lock across all the resources at once.

In practiceA travel-booking agent books a flight, then a hotel; when the car-rental step fails, it automatically cancels the hotel and then the flight via their respective compensating actions.

When to reach for it

  • Agents trigger external side effects.
  • Distributed transactions are unavailable.
  • Error reversal is technically feasible.

When it backfires

  • Actions cannot be compensated.
  • Strict atomic consistency is required.
  • Errors are better prevented by upfront approval.

The tradeoff

More robust long-running processes are gained against complex compensation logic.

The effect

What it actually does.

A failed step triggers compensating rollbacks.

commitrollbackstepstepstep
Pitfalls

Two ways this pattern will hurt you.

Compensation is also a side effect that can fail

The refund step fails. The system is now half-rolled-back: order cancelled, money still gone. Compensation is treated as if it cannot itself error.

Fix · Treat each compensation as a workflow with its own retries and dead-letter. Persist intent before attempting compensation so a recovery process can drive it to completion.

A non-compensatable step sits in the middle

One step has no possible undo — an email sent, a payment settled, a message posted. A later failure cannot roll it back, so the saga cannot restore a consistent state.

Fix · Order irreversible steps last (the pivot), so everything before them is compensatable and everything after is retriable; gate a genuinely irreversible mid-flow action behind a human approval.

Framework support

Where Saga / Compensation is native.

AWS StrandsWorkflowNative
Microsoft Agent Frameworkcompensation stepsNative
LangGraphcompensating edgesNative

Search

Search patterns, frameworks, and pages.