Video in progress
Why is a compensating transaction not a rollback, and what is a pivot step?
A compensation "cancels" the effect of an earlier step with a new, separate operation, because the change was already committed and others have seen it. A pivot step is the one after which the saga no longer goes back, it can only complete forward.
📖 Answer
With a local DB rollback, the change looks as if it never happened. In a saga, every step has committed its own transaction: the stock was reserved, another user saw "0 left in the warehouse", an email went out. A compensation closes off that effect with the opposite business action: ReleaseStock, RefundPayment, a second email saying "your order was canceled".
That is why compensations:
- must be idempotent themselves (they may arrive twice);
- must not fail, or must be retried until they succeed;
- don't always undo everything: a refund returns the money, but the bank fee may not come back.
Steps usually fall into three kinds:
| Step | Example | Property |
|---|---|---|
| Compensatable | reserving stock | can be canceled |
| Pivot | charging the payment | this is where the "yes" or "no" is decided |
| Retriable | scheduling delivery | if it fails, we only retry |
A design rule: move steps that are hard or expensive to compensate to after the pivot. For example, send the customer the "order confirmed" SMS after the payment, not before.
Sign up to mark studied questions and save your progress.