esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

Compensating Transaction: Undo Finished Steps When a Later Step Fails

When a multi-step job cannot roll back as one unit, each finished step needs its own defined undo action.

In short

A compensating transaction is a defined undo action for a finished step, used to reverse earlier steps when a later step of a multi-step job fails.

10 min readFree

What you will learn

Many jobs have several steps, and each step changes data in a different place. A checkout may charge a card, hold stock, and book a delivery. What happens when the last step fails after the first two have already finished? This lesson explains the compensating transaction, the common answer to that question. You will learn how to plan an undo for each step, how to run those undos in the right order, and what the design can never fully fix.

Key words

Transaction
A group of changes that should happen together as one unit of work.
ACID
Four promises a database makes about a transaction: all-or-nothing, valid results, no mixing with other work in progress, and changes that survive a crash once saved. The letters stand for atomicity, consistency, isolation, and durability.
Rollback
Throwing away the changes of an unfinished transaction, so it is as if they never happened.
Local transaction
A transaction that one service runs inside its own database.
Compensating transaction
A new step that reverses the business effect of an earlier step that already finished. It does not erase the old step.
Saga
A workflow made of local transactions, where each finished step has a compensating step ready in case a later step fails.
Idempotent
Safe to repeat. Running the action two or three times has the same effect as running it once.
Eventual consistency
A state where parts of a system may disagree for a while, but settle into agreement later.
Point of no return
The step after which the earlier work can no longer be undone, so the job must finish.

Why is one rollback not enough?

On a single database, a failed job is easy to clean up. The database holds the changes as pending, and if the job fails it throws them away. That is a rollback, and it works because one system controls every change. Many modern systems split work across several services, and each service keeps its own data. No single rollback can reach all of them. Architecture guidance says that distributed transactions need coordination, and that ACID guarantees do not apply directly across independent data stores, so the pattern aims for consistency by combining all-or-nothing steps with compensation.

  • Separate databases

    Each service owns its data. The order service cannot delete a row inside the payment service.

  • Committed local changes

    Once a service saves its local change, that change is final for its database. The workflow cannot reach in and reverse it.

  • Outside effects

    A card charge or a shipping label lives outside your database. Only a business action can reverse it.

The fix is to build the job from smaller local transactions. Self-healing guidance recommends avoiding distributed transactions and composing the operation from smaller pieces, then using compensation when the operation fails midway. Each piece commits on its own. If a later piece fails, the system runs new actions that cancel the effect of the earlier pieces. This design also avoids locking every service until the very end.

Why give every step its own undo?

Identify which steps are compensable and which are irreversible, and decide the undo pairs before the job runs. The pairs are often simple. The undo does not have to be the exact opposite of the step. A refund may need a fee rule, and a cancelled seat may need a rebooking rule. Business rules decide what the undo does.

Tip

Write each undo next to its forward step before you write any code. If you cannot name an undo for a step, mark that step as irreversible and plan where it sits in the flow.

How does compensation run?

Compensation has three jobs. It must remember which steps finished, walk back through that record, and make every undo safe to repeat. The steps below show a normal run.

  1. 1

    Record each finished step

    Save each completed step and its undo action in durable storage, so a crash does not erase the list. A status table for each step is one way to tell which steps still owe an undo.

  2. 2

    Notice the failure

    A failure may be temporary, so retry the step first. Undo begins only after retries give up, when the failure keeps coming back or cannot be fixed.

  3. 3

    Run undos in reverse

    Start with the newest finished step and walk backward. One saga description says the completed steps are rolled back in reverse order. The order can change when one data store is more sensitive to inconsistency, so that store is undone first.

  4. 4

    Make each undo idempotent

    Define each undo as an idempotent command, so repeating it is safe. A refund step should first check whether the refund already happened.

  5. 5

    Retry, then alert a person

    An undo can fail too. Keep its state saved, keep retrying, and when retries run out, raise an alert with the details. A failed undo can leave the task neither finished nor cleanly reversed.

The next two sub-topics go deeper into the repeat-safe part and show a tiny runnable model.

How do you make undo safe to repeat?

Repeats happen because networks drop replies. A caller may send a refund, hear nothing, and send it again. Without protection, the customer gets two refunds. A common fix is an idempotency key, which is a unique label for one action, such as the order ID plus the step name. The undo stores the key when it runs. When it sees the key again, it skips the work and reports success. A timed-out call is also tricky. Do not assume a timeout means no money moved, because the earlier call may still land.

Tip

Treat a timeout as unknown, not as failure. Check the status first, then retry with the same key.

Running the idea in Python

The sample below is a toy model with no real payment or database. It shows the order of steps, the reverse undo, and a refund that is skipped when repeated. It uses only the standard library, so you can run it with python3 -I.

Python
refunded = set()

def charge(order):
    print("charge", order)

def reserve(order):
    print("reserve", order)

def ship(order):
    raise RuntimeError("shipping down")

def refund(order):
    if order in refunded:
        print("refund skipped", order)
        return
    refunded.add(order)
    print("refund", order)

def release(order):
    print("release", order)

def run_saga(order, steps):
    completed = []
    for name, do, undo in steps:
        try:
            do(order)
        except RuntimeError as error:
            print("step failed:", name, "-", error)
            for _, done_undo in reversed(completed):
                done_undo(order)
            return "compensated"
        completed.append((name, undo))
    return "completed"

steps = [
    ("charge card", charge, refund),
    ("reserve stock", reserve, release),
    ("ship order", ship, None),
]

print(run_saga("order-42", steps))
refund("order-42")

Output

charge order-42
reserve order-42
step failed: ship order - shipping down
release order-42
refund order-42
compensated
refund skipped order-42

The shipping step failed, so it never finished and has no undo to run. The two earlier steps were undone, the newest one first. The final refund call was skipped because the refund was already recorded. That skip is the idempotent behavior from the previous sub-topic.

Worked example: a travel booking

Imagine a travel booking site such as Expedia, where a user books a flight, a hotel, and a car in one checkout. This is a simple model of the flow, not a description of how any real company builds its system. Each step below is a local action with a matching undo.

  1. 1

    Book the flight

    The flight service holds seat F1 and saves the booking. The workflow records the flight payment and the action that would refund it.

  2. 2

    Book the hotel

    The hotel service reserves room H1. The workflow records the action that would cancel this reservation.

  3. 3

    Book the car

    The car rental call fails, even after retries. The workflow now starts compensation.

  4. 4

    Undo in reverse

    The workflow cancels the hotel reservation first, then refunds the flight payment. Each undo uses the booking ID as a key, so a repeated undo does no extra harm.

  5. 5

    Show the failure screen

    The user sees a booking could not be completed screen. The screen should also say what happens to the money and whether the undo is still running.

Azure's travel example adds a useful twist. If one hotel fails, a different hotel may be better than cancelling the flights, and the customer should make that choice. High-impact decisions are often best left to a person.

Tip

A booking screen should never say the booking succeeded while undo steps are still running.

What are the limits of compensation?

Compensation is a useful tool, but it is not a time machine. Four limits matter most.

  • Eventual, not instant

    Undo runs after the earlier steps have already saved. For a while the data is wrong, and only later does it become right.

  • Semantic, not exact

    A refund leaves the original charge on the statement and adds a credit beside it. The customer sees both.

  • Some effects stay

    An email already sent or a package already shipped cannot be pulled back. You can only send a correction or start a return.

  • Users see the middle

    While undo runs, a user may see a held seat or a charged card. Screens and messages must be written for that moment.

Compensation can also fail. The undo itself may break, and then the system can stay inconsistent until someone fixes it. That is why the retry and alert rules from earlier matter so much.

Points of no return

Some designs mark one step as the pivot, which is the point of no return. Saga guidance says that after the pivot succeeds, the earlier steps are no longer undoable. The rest of the job must then finish. Steps that come after the pivot are retryable, so they must be idempotent to help the saga reach its final state even when temporary failures occur.

Tip

Place emails, bank transfers, and printed notices after the last check that could still fail.

Check yourself

Try these three questions. Each one needs the idea, not just a memory of the words.

Quick check

An order workflow has charged the customer's card and reserved inventory. The shipping service is down, so the order must be abandoned. Which plan is right?

Quick check

A travel booking site has booked a flight, and then the hotel step fails. The customer has not chosen yet. What should the system do first?

Quick check

A checkout workflow sent a confirmation email, and then a later step failed. The team wants the email taken back. What is the best design?

Key takeaways

A compensating transaction undoes finished steps when a later step fails. Plan each undo before the job runs, run the undos in reverse, make them safe to repeat, and be honest about what cannot be reversed. Users may see the unfinished middle for a while, so design the screens and messages for that moment.

Sources

How is this lesson?

Last updated on 18 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.