Free self-study · No account needed · Progress stays in this browser. Back it up before clearing site data or changing devices.

← Week 14: Orchestration and Recovery

Week 14 / Lesson 2

Recovering from partial workflow failure

Bound retries and compensate effects that cannot be committed atomically.

Section 2.1 1 of 7

Overview

Focus

Bound retries and compensate effects that cannot be committed atomically.

Your route through the lesson

  • 2.2 Retry classification and idempotency
  • 2.3 Compensating actions and their limitations
  • 2.4 Cancellation and unresolved effects
  • 2.5 Put it into practice
  • 2.6 Guided comparison
  • 2.7 A failed compensation after cancellation

All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.

Section 2.2 2 of 7

Retry classification and idempotency

Focus

Classify failures before retrying and preserve logical operation identity.

Retries are additional attempts at the same logical operation. They are appropriate when failure may be transient and the operation can be safely repeated or reconciled. A temporary service-unavailable response differs from invalid input or denied permission: repeating an unchanged invalid request consumes resources without changing its cause.

Define a maximum attempt count and a delay policy. Increasing waits between attempts reduces pressure on a struggling dependency; a total deadline prevents a workflow from retrying forever. The retry budget should include attempts made by libraries and workers so nested retries do not multiply invisibly. Three outer attempts each containing three inner attempts can create nine requests.

Idempotency means repeating the same logical operation has the same intended effect as doing it once. A stable key lets a receiver recognize repeats, but the receiver must enforce the contract. Store the key with the result or effect. Reusing a key with different parameters should be rejected. A local dictionary can illustrate this behavior, yet it loses its records on restart and cannot prove remote durability. Treat an ambiguous timeout as a reconciliation problem before choosing to retry a consequential action.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. An unchanged request fails because its amount is invalid. Appropriate retry decision?
2. Three outer attempts each make three inner attempts. Maximum requests?
3. An idempotency key is reused with a different destination. What should the receiver do?

Section 2.3 3 of 7

Compensating actions and their limitations

Focus

Use compensation as a new business action with its own failure evidence.

When several services cannot share one transaction, a workflow may need compensating actions. A reservation can be released; a payment can be refunded. Compensation attempts to restore an acceptable business condition, but it is not time travel. A refund does not erase a payment's audit history, fees, or the fact that a customer temporarily lost funds.

Consider reserve, charge, then ship. If shipment fails before dispatch, release the reservation and refund the charge. Each compensation has a stable operation identifier and its own status. Repeating refund after a lost response must not refund twice. The record should link refund to charge and explain why it ran.

Some effects cannot be fully compensated. An email cannot be unread; a shipped parcel may require a return. That limitation belongs in workflow design before the effect occurs. Prefer ordering reversible preparations before irreversible actions. Define unresolved status if a compensation fails, rather than declaring the whole workflow rolled back. A successful local state update does not establish that a remote refund or release actually completed; preserve the receiver's confirmation or a reconciliation path.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A payment is refunded after shipment fails. What remains true?
2. Refund response is lost. Why retry under the same refund identity?
3. A sent email contains an error. Is deleting the local sent record full compensation?

Section 2.4 4 of 7

Cancellation and unresolved effects

Focus

Treat cancellation as a boundary decision and preserve unresolved effects.

Cancellation means no new work should begin beyond a defined boundary. It does not guarantee that already-running remote work stopped. A worker may receive cancellation after a charge request has committed but before the response arrives. The workflow must reconcile that charge and, if appropriate, request compensation.

Use explicit states such as cancel_requested, compensating, cancelled, and compensation_failed. A task should enter cancelled only when required effects are either absent, safely completed according to policy, or compensated as defined. If a release operation fails, compensation_failed records an outstanding obligation even when shipment was successfully prevented.

In a reservation-charge-shipment example, cancellation after charging prevents the ship step, triggers one refund, and attempts a reservation release. If release remains unavailable after its bounded retries, record the reservation identifier, attempted compensation, failure reason, and responsible escalation route. Do not keep retrying indefinitely or hide the failure inside a generic cancelled label. Cancellation and recovery interact: a restart must reload both the cancellation request and unresolved effect records so it cannot resume shipment simply because a queue message still exists.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Cancellation arrives after a remote charge committed. What is required?
2. Refund succeeds but reservation release fails. Honest final status?
3. After restart, a cancelled workflow still has a shipment message. What should govern execution?

Section 2.5 5 of 7

Put it into practice

Focus

Run the reservation, charge, cancellation, and failed-compensation simulator.

Download week14.py, save it in an exercise folder, and run python week14.py --lesson 2. This single practice runs a harmless local ledger; it makes no payment or shipment request. It requires only Python's standard library, a small processor, and a few seconds. Its temporary directory is removed when the script ends.

The helper once receives a key and an action function. If the key is absent from the dictionary, it calls the action and saves the result; otherwise it returns the saved result. A lambda is a short function expression: lambda: 'refunded' creates a no-argument function returning that string. The fixture uses only trusted fixed actions, not generated code.

Keys o9/reserve and o9/charge establish preparations. Cancellation then invokes o9/refund twice under one identity and records a failed o9/release. Assertions verify exactly one refunded result, no o9/ship effect, and an explicit compensation_failed entry. The printed warning identifies the persistence limit: this in-memory ledger does not survive a process restart and does not prove any remote exactly-once contract. Use that limitation when interpreting the PASS rather than treating a simulator as production payment infrastructure.

Download this week’s Python lab ↓

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. The simulator calls once with o9/refund twice. How many logical refund results are stored?
2. What does lambda: "refunded" represent in this fixture?
3. The dictionary ledger passes all checks. Which claim remains unsupported?

Section 2.6 6 of 7

Guided comparison

Focus

Explain why the worked cancellation result includes an unresolved obligation.

The completed trace first records reserved and charged. Cancellation then prevents the next forward action, shipment. It does not remove the already recorded charge. The compensating refund is stored under o9/refund. Calling once again with that key returns the original refunded value, so the alternative duplicate-refund function is never used.

Reservation release is represented as a failed compensation record. The final result therefore contains both successful safety behavior and an unresolved problem: no shipment, one refund, and a reservation still requiring resolution. These facts should be reported separately. A single green success flag would conceal the remaining obligation; a single red flag would conceal which protections worked.

The fixture intentionally lacks crash durability for its dictionary. Extending it would require saving effect identities and compensation status transactionally, then reconciling external outcomes after restart. Even with durable bookkeeping, a receiver must honor stable keys for duplicate prevention. The design lesson is to give every consequential forward and reverse action a traceable identity, a bounded retry policy, and an honest outcome. That pattern makes partial recovery understandable without claiming that every workflow can be restored to its original physical state.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Why is the duplicate-refund action function never used on the second call?
2. Cancellation prevents shipment, a refund succeeds once, and reservation release fails. Which report matches these observations?
3. What additional mechanism is needed before duplicate prevention can survive restart?

Section 2.7 7 of 7

A failed compensation after cancellation

Focus

Handle a failed compensation after cancellation without losing responsibility.

A booking workflow reserves a seat and charges a fictitious payment. The customer cancels before ticket issuance. The charge is refunded, but the seat-release service is unavailable. Calling the workflow cancelled without qualification would imply that the seat is free when it is still reserved.

The worked recovery records cancel_requested, refund confirmed, and seat_release unresolved. It retains the original reservation and release operation identifiers. A bounded retry schedule may try release again while preserving the same logical identity. If the deadline is exceeded, the workflow routes an explicit unresolved obligation to the responsible operator. Ticket issuance remains blocked throughout.

If the release later succeeds, a new audit event closes the obligation; it does not rewrite the earlier failure out of history. If the service reports reservation already expired, reconciliation can establish that the desired seat availability has been reached without another release. This is evidence-based completion. The key transfer is that cancellation and compensation are long-lived state transitions, not exceptions that can be caught and forgotten. Recovery ownership must persist beyond the lifetime of the worker that first saw the failure.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Refund is confirmed but seat release is unavailable. What must remain blocked?
2. Release succeeds after an earlier failed attempt. How should history change?
3. Reservation lookup shows the seat already expired and available. What can that support?

Your personal notes

Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.

References and further reading
Ask your own AI assistant

Copy this prompt into an assistant you already use. Nothing is sent automatically, and your personal notes are not included.

Next lesson: Coordinating concurrent workers →