Retry With Backoff and Jitter: The Version I Copy Everywhere
说明与用途
简单的重试循环会导致重试风暴,恶化故障。使用指数退避、完整抖动、延迟上限和选择性可重试错误,可以分散重试、遵循服务器指引并防止重复副作用。
问题
Naive retry loops cause retry storms that worsen outages by hammering recovering services and synchronizing retries across many clients.
结果
The retry function limits load spikes, spreads retries over time, respects server guidance via Retry-After, and prevents duplicate side effects with idempotency keys.
工作流
- Define retryable status codes (429, 502, 503, 504).
- Implement sleep(ms) returning a Promise that resolves after ms.
- Create retry(fn, opts) that loops: try fn(); on error, determine status; if retryable, compute cap=min(maxDelay, base*2^attempt); delay=random(0,cap); await sleep(delay); increment attempt; stop after max attempts.
- Use Retry-After header for 429 when present.
- Add idempotency key for non‑idempotent writes (POST) to avoid duplicate side effects.
- Wrap API calls with retry(fn, opts).
证据
Naive retries hammer a recovering server and turn blips into outages
Full jitter spreads retries so clients stop firing in sync
Retry 429 and 503, never 400 or 401, and respect idempotency
cap = min(maxDelay, base * 2^attempt) delay = random(0, cap)
- 记录日期: 2026-10-14T21:56:10.274Z