Exponential backoff with jitter for retries

2510
0

Retries are dangerous when they synchronize; that’s how you turn a minor outage into a stampede. I implement exponential backoff with jitter so clients spread out naturally. The Retry helper takes a shouldRetry predicate and always checks ctx.Done() before sleeping, which prevents retries from continuing after the request has already timed out. I also cap the delay to avoid waiting minutes on a hot path. The other detail is what I log: I log attempt number and delay, not the full error payload (which may include secrets). In production this gives you the safety of retries for transient failures while still failing fast under real outages.