Skip to main content

Fail Fast on Permanent Errors

View as Markdown

Use this pattern when a provider call can fail in two ways: a transient error that a retry may fix, and a permanent error that no retry can fix.

An example is an expense claim paid through a payment provider. A timeout is transient: the next attempt may succeed. An invalid bank account is permanent: the provider has rejected the request, and every retry gets the same answer.

Step retry retries every error that Step code raises, under the Step's retry policy. In the example the retry scheduling budget is one hour, so a permanent error that Execute raises keeps being retried until that budget runs out. Classify the error in Execute instead, and route a permanent error at once.

How it works

  • Make provider writes in Execute. Execute sees the provider's error and decides what it means. A read-only call in WaitFor hands a permanent error to Execute instead; see WaitFor and Execute.
  • Let a transient error fail the attempt. Throw it or return it. Step retry runs the call again under the Step's own retry policy.
  • Catch a permanent error and return a Step decision. Go to a failure-handling Step that records the business failure. Return ForceFail instead when the whole Flow must be marked failed at once.
  • Treat an error as permanent only when the provider confirms it, such as rejected input or an invalid request. Leave an unknown error transient, so retry still covers it.
  • Size the retry policy for transient errors only. A routed permanent error never consumes it.
  • Prefer to put calls that can fail in Execute. Execute returns the decision that routes the failure, in the same call that saw it.
  • Once the rejection decision commits, that error never retries SubmitPayout. The failure-handling Step is a Step of its own, so Dex can still retry it independently.
  • Keep the failure record free of sensitive input. The example records a fixed reason and never the bank account number.

In the example:

  1. ExpenseReimbursementFlow starts SubmitPayout with the claim. The Flow ID is the claim ID and the provider's idempotency key.
  2. SubmitPayout calls the payment provider. On success, it records the payout in the PayoutOutcome Attribute and completes the Flow.
  3. When the provider times out, SubmitPayout lets the error fail the attempt. Step retry calls the provider again with backoff, until the one-hour retry scheduling budget runs out.
  4. When the provider rejects the bank account, SubmitPayout goes to RecordRejectedClaim with a fixed reason. SubmitPayout is not retried for this error.
  5. RecordRejectedClaim records the rejection in PayoutOutcome and completes the Flow.

Example

The fake provider accepts only a bank account of eight ASCII digits and rejects any other. It times out on the first submission of every valid claim, then pays it. Run the HTTP sample from the examples playground.

expense_reimbursement_flow.pyPython
class RecordRejectedClaim(Step[ClaimRejection]):
def execute(self, context: Context, input: ClaimRejection) -> StepDecision:
PAYOUT_OUTCOME.set(
context, PayoutOutcome("REJECTED", input.reason, input.submit_attempt)
)
return graceful_complete(f"rejected: {input.reason}")


class SubmitPayout(Step[ExpenseClaim]):
def __init__(self, provider: FakePaymentProvider) -> None:
self.provider = provider

def get_step_options(self) -> StepOptions:
return StepOptions(
execute_retry=RetryPolicy(
initial_interval=timedelta(seconds=1),
backoff_coefficient=2.0,
maximum_interval=timedelta(minutes=5),
total_duration=timedelta(hours=1),
)
)

def execute(self, context: Context, input: ExpenseClaim) -> StepDecision:
try:
payout_id = self.provider.submit_payout(context.flow_id, input)
except PayoutRejectedError as rejection:
return go_to(
RecordRejectedClaim, ClaimRejection(str(rejection), context.attempt)
)
PAYOUT_OUTCOME.set(context, PayoutOutcome("PAID", payout_id, context.attempt))
return graceful_complete(payout_id)

Example: examples/python/dex_examples/patterns/fail-fast/expense_reimbursement_flow.py

Test both routes

Test with the Step's real retry policy. A test that shortens the policy no longer tests the route.

  • Make a fake provider return a permanent error. Assert the terminal state and that the outcome came from the first attempt of SubmitPayout. When the test can observe the fake provider, also assert that it was called exactly once.
  • Make the fake provider return a transient error once. Assert that the Step retries and then succeeds on the second attempt.

The examples read the attempt from the PayoutOutcome Attribute, which records the attempt that produced the outcome.

Compared with Execute Failure Recovery

Execute Failure Recovery handles a failure that is still there after the retry policy is exhausted. Its ExecuteFailure policy moves the Flow to a recovery Step, which receives the failed Step's input.

Fail Fast on Permanent Errors decides before any retry. Execute itself returns the decision, so the failure-handling Step receives whatever input Execute builds, such as the rejection reason.

Use both together when a Step needs both: fail fast on a confirmed permanent error, and add an ExecuteFailure route for transient errors that outlast the retry policy.

Next