You cannot roll back something that already committed, so you undo it with a new action.
A compensating transaction is a new action that undoes the business effect of an earlier one that already committed. You cannot roll back something that is final, so you do the opposite: a refund for a charge, a cancel for a booking.
In one database, a failed transaction is simply rolled back, as if it never happened. Across several services, each service has already committed its own local transaction. By the time a later step fails, the earlier results are real and visible. There is no database to roll them back.
So we write the undo ourselves, as a normal piece of business logic. This is a compensating transaction. It is a semantic undo: it brings the business back to an acceptable state, but not always to the exact same state. A refund returns the money, but the card statement still shows both a charge and a refund. A cancelled booking may still cost a fee.
The SAGA pattern uses compensations as its main tool. This page is about how to design them well: what they must do, what they cannot do, and how to order the steps so the hard ones come last.
| Original step | Compensation | Note |
|---|---|---|
| Reserve stock | Release the stock | Clean undo |
| Charge the card | Refund the card | Both show on the statement |
| Book a flight | Cancel the flight | May cost a fee, or be refused after a deadline |
| Create a user account | Mark it deleted or deactivated | Often not a true delete, because others may refer to it |
| Send a message to a user | Send a correction | The first message cannot be unsent |
| Ship a parcel | Arrange a return | Slow, partial and costly |
Not all steps are equal. Sorting them tells you where to put each one.
A simplified model. Real sagas also keep retrying a compensation until it succeeds, and record progress so a crash in the middle can be resumed.
| Term | Meaning in easy words |
|---|---|
| Compensating transaction | A new action that undoes the business effect of an earlier committed action |
| Semantic undo | Making the business right again, not erasing the past |
| Rollback | A database undoing an uncommitted change, leaving no trace |
| Idempotent | Safe to run again with the same result |
| Retryable | Can be retried until it succeeds |
| Pivot step | The step after which the process can no longer be undone and must go forward |
| Compensable step | A step that has a working undo |
| Retryable step | A step after the pivot that is retried until it succeeds |
| Non-compensable step | A step with no undo, such as sending an email |
| Semantic lock | A flag like 'pending' that warns others the record may still change |
| Reverse order | Compensations run from the latest finished step back to the first |
| Choice | We gain | We lose |
|---|---|---|
| Compensate instead of lock | Speed, no blocking | Isolation, and users may see in-between states |
| Pivot step last | Few compensations, simple recovery | Slower feedback if the risky step is slow |
| Delete the record on undo | Clean data | No audit trail |
| Mark as cancelled instead | A full history | More data to filter out |
| Retry compensation forever | Eventually consistent | Needs alerting for stuck cases |