Skip to content
Streak:0Loading your progress…Sign in

Module 7 · Lesson 2 of 2

Timeouts, retries & backoff

Every network call fails eventually. Design for it. ~25 min

Lesson notes

Rules from production: (1) every outbound call has a timeout; (2) retry only idempotent operations and transient errors (timeouts, 429, 5xx) — never 400s; (3) back off exponentially with jitter so clients don't retry in lockstep; (4) cap attempts and total time; (5) honour Retry-After.

Exponential backoff with full jitter
import random
def backoff_delay(attempt: int, base: float = 0.5, cap: float = 20.0) -> float:
    return random.uniform(0, min(cap, base * 2 ** attempt))

Practice

  1. 1. Async retry with backoff

    hard50 XP

    Write async def call_with_retry(fn, *, attempts, base_delay, sleep, is_retryable). Call await fn(); on exception, if is_retryable(exc) and attempts remain, await sleep(delay) where delay = base_delay * 2 ** (attempt_index) (0-based, no jitter — tests need determinism) and retry. Otherwise re-raise. sleep is injected so tests don't actually wait.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor

  2. 2. What's safe to retry?

    Quizmedium10 XP
    A POST /payments call times out. What's the safest way to retry?