Design patterns and optimization · Intermediate

Design saga compensation and unresolved states

Trace reservation, payment and cancellation to specify compensation retries, unknown outcomes, local invariants and recovery ownership.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Model business progress with visible intermediate states
  2. Pair steps with business-defined compensation
  3. Choose the next action from durable history
  4. Choose control ownership and local invariants
  5. Keep failed compensation visible and owned
  6. Treat reservation expiry as another competing transition
  7. Solve the lost-compensation-response exercise
  8. Ask Logeia about this articleCheck your understanding

Model business progress with visible intermediate states

A flow can commit reservation, payment and notification in separate local services. A saga coordinates that sequence and corrective operations after relevant failures. Read distributed transactions first. It does not turn every step into one atomic commit; intermediate reservation can be visible. Describing only fully exists or does not exist hides states that recovery actually encounters.

Use new, reserved, paid, complete, cancellation-pending, cancelled and needs-review states. Final payment rejection can trigger reservation cancellation, while an unknown payment response requires reconciliation under stable identity. Do not collapse both into one exception path. User-visible acceptance and completion also need different meanings. The state model should explain what the system already committed and what it still owes, including failures that cannot yet choose a safe next business action.

Pair steps with business-defined compensation

Reservation cancellation and a conditionally permitted refund are new business operations, not literal time reversal. They cannot erase a notification already observed, and refunds can have time, fee or external constraints. Define compensation through current business rules rather than mechanically reversing SQL. Some effects have no perfect inverse.

Compensation itself needs identity and state so duplicate cancellation does not restore stock twice. Associate it with the original operation and its own action scope. A delayed cancellation may arrive after the reservation advanced, requiring an explicit permitted transition rather than overwriting newer state. Trace send, acceptance, commitment and acknowledgment separately. The same uncertainty that affects forward work can affect corrective work, so calling it rollback does not justify assuming it always finishes immediately or exactly once.

Choose the next action from durable history

Reservation succeeded, payment was finally rejected, cancellation was sent and its response disappeared. Neither cancelled nor definitely not cancelled follows yet. Cancellation may have committed. Retrieve its result through stable compensation identity or a status query. Preserve the unknown outcome when retrying; creating a fresh stock-restoration identity can duplicate compensation.

Persist saga progress so a restarted owner knows where to continue: step identity, attempts, outcome, next intention and relevant deadlines. An outbox can atomically retain a local result and its next send intention. Inbox or natural uniqueness can protect a receiving local effect. These mechanisms do not make the whole flow atomic. Waiting and observable intermediate state remain part of the business contract, requiring a recovery path even when every local mechanism works as designed.

Observed stateCandidate next action
Reservation, final payment rejectionCancel reservation
Reservation, unknown payment resultReconcile payment result
Lost cancellation responseQuery or retry stable compensation identity
Final uncompensatable failureNeeds-review resolution

Choose control ownership and local invariants

An orchestrator centralizes progress decisions but needs durable state, restart recovery and protection against conflicting owners. Event choreography can distribute control while scattering the evidence needed to reconstruct global progress. Define completion conditions and responsibility so cycles or orphaned unresolved work do not result. Neither label automatically simplifies failure handling.

Concurrent sagas reserving the same inventory still need local business constraints. A saga does not permit negative stock merely because correction may occur later. Define reservation expiry, release and conflicting updates using the resource's transaction contract. Avoiding long database locks does not remove semantic reservation cost or isolation questions. Other callers may still wait for held business capacity. Describe both the local invariant and the larger flow instead of treating eventual compensation as permission for arbitrary intermediate corruption.

Keep failed compensation visible and owned

If cancellation is unavailable, retry under deadlines and budgets while retaining cancellation-pending state. Endless background retry alone cannot distinguish active recovery from abandoned work. Observe age, attempts and last outcome, with a defined needs-review resolution when necessary. A stored state is not the same as an implemented human-notification feature. Report operational capabilities only when they exist and retain an auditable resolved outcome.

When all steps fit one database, a local transaction may be simpler. Strict jointly committed resources need comparison with protocols their participants actually support. Consider sagas for long flows permitting intermediate states, separating invariants that always hold from obligations resolved later. These histories are explanatory, not transactions sent to a production payment or inventory service. Start failure tests with disposable effects and identities, then compare the chosen service contracts.

Treat reservation expiry as another competing transition

An expiring reservation can race with explicit saga cancellation. If both restore inventory, one-time reservation-state transition rules must prevent double restoration. A delayed payment success after expiry also cannot blindly mark the original order complete. Define the reconciliation path for that history. Expiry is not a magical compensation guaranteed to align with remote progress. Test expiry-before-payment, payment-before-expiry and lost cancellation response as distinct schedules, retaining the same operation identities. This extends the state model so recovery chooses a valid business action from durable facts instead of assuming every timeout leaves the same state. Its correctness still depends on the resource enforcing the local transition atomically.

Pending compensation also has a user-visible contract that must remain consistent with the durable workflow state.

Solve the lost-compensation-response exercise

A cancellation response is lost and a fresh cancellation ID is proposed. If the first cancellation committed, the second can restore stock twice. Reconcile the original compensation identity, or rely on a verified one-time reservation-state transition where that business invariant supplies protection. In either case inspect the transaction containing identity and state change. Cancellation is not inherently safe merely because it is corrective.

Treating unknown payment as final rejection can leave committed payment alongside cancelled reservation. Keep unknown, final failure, compensation-pending and compensation-complete states distinct and map them to user-visible behavior. The learning outcome traces identity, commit, response and restarted ownership for both forward and corrective steps.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 17

Choose a learning path