# 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.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/compensating-transaction

## 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.

- **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. **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. **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. **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. **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. **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:

```text
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. **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. **Book the hotel:** The hotel service reserves room H1. The workflow records the action that would cancel this reservation.
3. **Book the car:** The car rental call fails, even after retries. The workflow now starts compensation.
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. **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?

A. Refund the card first, then release the stock. If the refund times out, mark the order cancelled and stop.
B. Release the stock, then refund the card with a stable key. If the refund times out, retry it and check whether a refund already exists.
C. Leave the charge in place and ask the customer to request a refund by phone.
D. Ship the order anyway and refund it after delivery, so no step has to be undone.

**Answer:** B. Undo runs in reverse, so the newest finished step, the stock reservation, is released first, and then the charge is refunded. A timed-out refund may still have succeeded, so the retry must be idempotent and must check first.

**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?

A. Cancel the flight at once and refund it, without asking the customer.
B. Offer another hotel and let the customer decide whether to cancel the flight.
C. Show the trip as complete and retry the hotel later without telling anyone.
D. Delete the flight record from the airline's system.

**Answer:** B. A failed step does not always mean a full undo. A different hotel may be the better result, and a high-impact choice like cancelling flights is best made by the customer.

**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?

A. Recall the email from the customer's inbox automatically.
B. Send a cancellation message as a new step, because a sent email cannot be unsent.
C. Roll back the email server's database so the email disappears.
D. Do nothing, because an email is not part of the stored data.

**Answer:** B. A sent email is an outside effect. It cannot be unsent, so the workflow adds a new message that corrects the situation.

## 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

- [Compensating Transaction pattern - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/patterns/compensating-transaction)
- [Saga Design Pattern - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/patterns/saga)
- [Minimize coordination - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/guide/design-principles/minimize-coordination)
- [Design for Self-Healing - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/guide/design-principles/self-healing)
- [Architecture design patterns that support reliability - Microsoft Azure Well-Architected Framework](https://learn.microsoft.com/en-us/azure/well-architected/reliability/design-patterns)
- [Scheduler Agent Supervisor pattern - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/patterns/scheduler-agent-supervisor)
- [Distributed Transactions: 2PC vs Saga Pattern Explained (systemdesignsim.com)](https://systemdesignsim.com/system-design/distributed-transactions)
- [Distributed Transactions and Rollbacks: A Survival Guide to the Saga Pattern (AverageDevs)](https://averagedevs.com/blog/distributed-transactions-saga-pattern)
- [Saga Compensation Pattern Low-Level Design (techinterview.org)](https://www.techinterview.org/post/3233468856/lld-saga-compensation/)
- [Saga Pattern for Microservices Explained (Conduktor glossary)](https://www.conduktor.io/glossary/saga-pattern-for-distributed-transactions)
