Retry Logic
ELI5 — The Vibe Check
Retry Logic automatically retries failed requests instead of giving up on the first failure. But done wrong, retries cause thundering herds. The secret: exponential backoff with jitter. Wait 1s, then 2s, then 4s — plus some randomness so all clients don't retry at the same time.
Real Talk
Retry logic handles transient failures by re-attempting operations with strategies like fixed delay, exponential backoff, and jitter. Key considerations: idempotency (safe to retry), max attempts, retry-able error codes, and circuit breaking to prevent cascading failures. Libraries like axios-retry and tenacity simplify implementation.
Show Me The Code
async function fetchWithRetry(url, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fetch(url);
} catch (err) {
if (i === maxRetries - 1) throw err;
const delay = Math.pow(2, i) * 1000 + Math.random() * 1000;
await new Promise(r => setTimeout(r, delay));
}
}
}
When You'll Hear This
"Exponential backoff with jitter prevents all clients from retrying at the same moment." / "Only retry idempotent operations — retrying a payment could charge the customer twice."
Related Terms
Circuit Breaker
Circuit Breaker is like the electrical circuit breaker in your house.
Exponential Backoff
Exponential backoff doubles the wait time between each retry. Wait 1s, then 2s, then 4s, then 8s.
Idempotency
Idempotency means 'doing the same thing twice gives you the same result.' It's like pressing an elevator button multiple times — you still go to the same f