Async Retry with Jittered Exponential Backoff
Retrying failed network requests at fixed intervals produces a thundering herd that repeatedly crashes recovering servers. Full jitter backoff computes delay as a random value between 0 and min(max_delay, base * 2^attempt), desynchronizing retry waves.
import asyncio
import random
async def retry(fn, max_tries=3, base=0.1, cap=2.0):
for attempt in range(max_tries):
try:
return await fn()
except Exception:
sleep_time = random.uniform(0, min(cap, base * (2 ** attempt)))
await asyncio.sleep(sleep_time)
# Async Retry with Jittered Exponential Backoff When an upstream microservice hiccups and fails, 1,000 clients retrying at exactly 1.0s, 2.0s, and 4.0s will hit the target server in synchronized assault waves, knocking it down repeatedly. 🌊🛑 **Jittered exponential backoff** solves this by adding entropy. Instead of waiting a deterministic $2^n$ seconds, the coroutine sleeps for a uniform random duration between $0$ and $\min(\text{cap}, \text{base} \times 2^{\text{attempt}})$. This disperses clients into a flat, manageable trickle of requests. ```python import asyncio import random async def retry_with_jitter(fn, max_attempts=3, base_delay=0.05, max_delay=1.0): for attempt in range(max_attempts): try: return await fn() except Exception: if attempt == max_attempts - 1: raise delay = random.uniform(0, min(max_delay, base_delay * (2 ** attempt))) await asyncio.sleep(delay) ``` ## Pop Quiz Why is "Full Jitter" preferred over pure exponential backoff without jitter? - [ ] It spreads concurrent retry attempts evenly across time, avoiding synchronized request bursts. - [ ] It ensures the network socket buffer never overflows into system swap memory. - [ ] It converts failed TCP handshakes into asynchronous UDP datagrams. - [ ] It bypasses HTTP 429 status codes by modifying SSL client fingerprints. ## Real-World Challenge Implement `async_retry_with_jitter(coro_fn, max_retries: int = 3, base_delay: float = 0.05, max_delay: float = 0.5)`. It must retry `coro_fn()` on exceptions, applying full randomized jitter up to `max_delay`, and re-raise the final exception if retries are exhausted. <details> <summary>Solution</summary> ```python import asyncio import random async def async_retry_with_jitter(coro_fn, max_retries: int = 3, base_delay: float = 0.05, max_delay: float = 0.5): attempt = 0 while True: try: return await coro_fn() except Exception: attempt += 1 if attempt > max_retries: raise backoff_cap = min(max_delay, base_delay * (2 ** (attempt - 1))) sleep_duration = random.uniform(0.0, backoff_cap) await asyncio.sleep(sleep_duration) # Verification async def run_test(): attempts = 0 async def flaky_service(): nonlocal attempts attempts += 1 if attempts < 3: raise ConnectionResetError("Transient connection reset") return "SUCCESS" result = await async_retry_with_jitter(flaky_service, max_retries=4, base_delay=0.01, max_delay=0.1) assert result == "SUCCESS" assert attempts == 3 asyncio.run(run_test()) ``` </details>