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.

Level
04 · Production
Subdomain
Runtime Architecture
Also known as
Saga Pattern, Compensating Action, Transactional Workflow, Exception Handling & Recovery
Trade-off
More robust long-running processes are gained against complex compensation logic.
Native in
AWS Strands, Microsoft Agent Framework, LangGraph
The mechanism

How it works.

Every step that changes something outside comes with an action that undoes it. When a later step fails, the completed steps are undone in reverse order.

1 · Forward

  1. Run a stepIt commits for real
  2. Remember its compensationOn a stack
  3. Run the next stepUntil all are done

2 · When a step fails

  1. Stop going forwardThe failed step changed nothing
  2. Take the last compensationThe most recent step first
  3. Run itUntil the stack is empty
  4. Report the outcomeUndone, with the reason
Compensation
An action that reverses the business effect of a step. It is a new action, not a rollback: the cancelled booking stays in the history.
Reverse order
The last committed step is undone first, because later steps may depend on earlier ones.
Point of no return
A step that cannot be compensated, such as a sent email. It belongs at the end, or behind an approval.
Walk it through

A real run, step by step.

TaskA travel agent books a flight, a hotel and a rental car. No car is available:
Illustrative run.
StepActionResult
1Book the flightBooked
2Book the hotelBooked
3Rent the carFails: no car is available
4Compensate step 2: cancel the hotelCancelled
5Compensate step 1: cancel the flightCancelled, with a fee of 50 EUR
What this shows: the customer ends up with no trip and no orphaned bookings. The fee in step 5 shows that a compensation is not a rollback. The flight really was booked for a while, others could see that, and undoing it had a cost. A step that cannot be undone at all has to come last.
In code

The pattern in code.

Plain Pythonpython
def book_trip(trip):
    steps = [(book_flight, cancel_flight),
             (book_hotel, cancel_hotel),
             (rent_car, cancel_car)]
    done = []  # compensations of the steps that committed
    try:
        for action, compensation in steps:
            reference = action(trip)
            done.append((compensation, reference))
    except BookingError as error:
        # undo in reverse order
        for compensation, reference in reversed(done):
            compensation(reference)
        return f"Nothing booked: {error}"
    return "Trip booked"
Fit

Use it, or not.

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.
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 canceled, money still gone. Compensation is treated as if it cannot itself error.

Countermeasure

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.

Countermeasure

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
Sources

Where this pattern comes from.

  1. SagasIn the source list

Search

Search patterns, frameworks, and pages.