Forward Recovery vs Backward Recovery
What you'll learn: How to decide whether to undo failed saga steps or push forward to completion when things go wrong.
When a saga step fails midway through a multi-step distributed transaction, you face a crucial choice: should you undo the work already done, or retry until you succeed?
Backward Recovery (Rollback)
Backward recovery means compensating for completed steps by executing compensating transactions in reverse order. When step 3 of 5 fails, you undo step 2, then undo step 1, returning the system to its original state.
Think of it like: Retracing your steps when you take a wrong turn on a hike—you walk back to where you started.
When to use:
- The failed operation cannot reasonably succeed (e.g., insufficient inventory, invalid data)
- Business rules require returning to a consistent starting state
- The failure is permanent or would require significant intervention
Example: In an order saga, if payment authorization fails due to insufficient funds, you compensate by releasing the reserved inventory and canceling the order.
Forward Recovery (Retry)
Forward recovery means retrying the failed step (possibly with modifications) until the saga completes successfully. You don't roll back—you persist until you finish.
Think of it like: If a door is locked, you keep trying different keys or wait for someone to unlock it, rather than giving up and going home.
When to use:
- The failure is transient (network timeout, temporary unavailability)
- The operation is idempotent and safe to retry
- Business requirements mandate eventual completion
Example: If inventory reservation times out, retry the reservation operation several times before giving up.
Choosing Your Strategy
The decision often depends on failure type: transient failures favor forward recovery, permanent failures require backward recovery. Many sagas implement both: retry forward a few times, then compensate backward if retries are exhausted.
Key Takeaway: Forward recovery retries failed steps to completion; backward recovery undoes completed work through compensation—choose based on whether the failure is temporary and whether completion is mandatory.