DevPath
Foundation lesson · 12 min

Retry transient failures with a budget

Use exponential backoff, jitter and a clear stopping rule.

Classify before retrying

A timeout or temporary 503 may improve on a later attempt; invalid credentials or malformed input usually will not. Retry only known transient failures and only when the operation itself is safe to repeat. An external write may need an idempotency key before a retry is acceptable.

Bound time and load

Exponential backoff spaces attempts, while jitter prevents many workers from retrying together. Cap the number of attempts, the delay, and the total elapsed time. Honor a caller deadline if one exists. Sleep outside database transactions and do not hold locks while waiting.

delay = min(max_delay, base_delay * 2 ** attempt)
sleep(random.uniform(0, delay))  # full jitter

Test without real sleeping

Inject a clock or sleep function so tests can inspect delays instantly. Test one transient failure then success, permanent failure with no retry, and exhaustion of the budget. Verify that failures remain visible to the caller or dead-letter queue rather than disappearing after the final attempt.

Check your understanding

Why add jitter to many workers' retries?

Show explanation

It spreads retry traffic over time instead of creating synchronized spikes against a recovering dependency.

You can read for free. Create an account to save your progress.

← Previous lesson: Bound concurrent service work

Try it at foundation level

Calculate a retry schedule for a transient call with a two-second total budget; include jitter and a permanent-error case.

Check your work

Retries stop within the budget, permanent errors return immediately, and repeated calls do not multiply a non-idempotent effect.

Self-guided practice. This does not record assessed mastery.

Put this lesson into practice

A related activity in this module at mid level.

Try: Add exponential backoff with jitter

Further reading

Primary references for this module. Check the current version when behavior or framework guidance may have changed.