Skip to main content
Python & FastAPI Interviews Coding P0 Medium

Implement async retry with exponential backoff and jitter

Write an async helper that retries transient HTTP/LLM errors up to max_attempts with capped backoff + jitter.

Official Reference

Model Answer & Explanation

import asyncio, random

async def retry_async(fn, attempts=3, base=0.5, cap=8.0):
    for i in range(attempts):
        try:
            return await fn()
        except (TimeoutError, ConnectionError) as exc:
            if i == attempts - 1:
                raise
            delay = min(cap, base * (2 ** i))
            await asyncio.sleep(random.uniform(0, delay))

Only retry transient failures (timeouts, 429, 502/503/504)—never 400/401/404 validation errors. Respect Retry-After headers when present, multiply delay exponentially, cap at max_delay, and add random jitter (e.g. full jitter random.uniform(0, delay)) to prevent synchronized retry storms.

Follow-up Interview Question · Why should non-idempotent POST endpoints NOT be retried blindly without an idempotency key?

Module Context & References

Python & FastAPI · Practice questions & drills High-frequency coding, FastAPI, concurrency and testing exercises
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.