patternModeratepending
Retry with jitter -- prevent thundering herd
Viewed 0 times
jitterthundering herdexponential backoffretry stormdecorrelatedfull jitter
Problem
When a service recovers from an outage, all waiting clients retry simultaneously. This creates a thundering herd that immediately overwhelms the recovered service, causing another outage.
Solution
Add randomized jitter to retry delays. Instead of everyone retrying at exactly 1s, 2s, 4s, spread retries across the interval. Full jitter: random between 0 and backoff. Equal jitter: backoff/2 + random(0, backoff/2). Decorrelated jitter: random between base and previous delay * 3. AWS recommends full jitter as the best default.
Why
Without jitter, exponential backoff creates synchronized retry waves. All clients double their wait at the same time, so they all retry at the same time. Jitter breaks the synchronization.
Code Snippets
Retry with full jitter
// Exponential backoff with full jitter
async function retryWithJitter(fn, maxRetries = 5, baseMs = 100) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await fn();
} catch (err) {
if (attempt === maxRetries - 1) throw err;
const backoff = baseMs * Math.pow(2, attempt);
const jitter = Math.random() * backoff; // full jitter
await new Promise(r => setTimeout(r, jitter));
}
}
}Revisions (0)
No revisions yet.