Retry backoff planner
Plan retries with backoff and jitter, and see what a thousand clients do to your server.
Up to 20.
Leave it empty for none.
Up to 10,000.
Full jitter waits a random time between zero and the planned wait. It spreads clients out the most.
Worst case: 18.2 s before I give up.
6 attempts with a 2 s timeout each, and waits that double from 200 ms to 3.2 s.
Attempt by attempt
| Attempt | Wait before it | With full jitter | Worst case so far |
|---|---|---|---|
| 1 | starts at once | – | 2 s |
| 2 | 200 ms | 0 to 200 ms | 4.2 s |
| 3 | 400 ms | 0 to 400 ms | 6.6 s |
| 4 | 800 ms | 0 to 800 ms | 9.4 s |
| 5 | 1.6 s | 0 to 1.6 s | 13 s |
| 6 | 3.2 s | 0 to 3.2 s | 18.2 s |
Longest wait before attempts 2 to 6
The longest wait grows from 200 ms before attempt 2 to 3.2 s before attempt 6.
1,000 clients fail at the same moment
Retries per 100 ms over the next 7 s. Every try fails at once, so each client makes all 5 retries.
No jitter
Five waves of 1,000 retries, all at once. This is the thundering herd.
Full jitter
The same retries, spread out. Peak: 721 in one slice instead of 1,000, about 7,210 a second instead of 10,000.
The policy as code
// Retry with backoff: up to 6 attempts, the first try included.
const delays = [200, 400, 800, 1600, 3200]; // ms before attempts 2 to 6, capped at 10 s
export async function withRetry<T>(call: (signal: AbortSignal) => Promise<T>): Promise<T> {
for (let attempt = 1; ; attempt++) {
try {
return await call(AbortSignal.timeout(2000)); // 2 s for each attempt
} catch (error) {
if (attempt > delays.length) throw error;
const planned = delays[attempt - 1] ?? 0;
const wait = Math.random() * planned; // full jitter
await new Promise((resolve) => setTimeout(resolve, wait));
}
}
}
package retry
import (
"context"
"math/rand/v2"
"time"
)
// The waits before attempts 2 to 6, capped at 10 s.
var delays = []time.Duration{
200 * time.Millisecond,
400 * time.Millisecond,
800 * time.Millisecond,
1600 * time.Millisecond,
3200 * time.Millisecond,
}
// WithRetry makes up to 6 attempts, the first try included.
func WithRetry[T any](ctx context.Context, call func(context.Context) (T, error)) (T, error) {
for attempt := 1; ; attempt++ {
attemptCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
result, err := call(attemptCtx)
cancel()
if err == nil || attempt > len(delays) {
return result, err
}
planned := delays[attempt-1]
wait := time.Duration(rand.Int64N(int64(planned) + 1)) // full jitter
select {
case <-time.After(wait):
case <-ctx.Done():
return result, ctx.Err()
}
}
}
import random
import time
# The waits in seconds before attempts 2 to 6, capped at 10 s.
DELAYS = [0.2, 0.4, 0.8, 1.6, 3.2]
TIMEOUT = 2.0 # seconds for each attempt: pass it to your client
def with_retry(call):
"""Make up to 6 attempts, the first try included."""
for attempt in range(1, len(DELAYS) + 2):
try:
return call(timeout=TIMEOUT)
except Exception:
if attempt > len(DELAYS):
raise
planned = DELAYS[attempt - 1]
time.sleep(random.uniform(0, planned)) # full jitter
What it does
It plans a retry policy and shows you what it really costs. Give it a first wait, a multiplier, a cap, the number of attempts and a timeout, and it lists every attempt with the wait before it and the worst case so far. Pick a jitter (none, full, equal or decorrelated) and the table shows the range each wait can fall in.
Then it lets a crowd loose: a thousand clients fail at the same moment and retry. One chart shows them without jitter, all arriving in the same instant, which is the thundering herd your database dreads. The other shows the same retries with jitter, spread out, with the peak per slice and per second. Last, it writes the policy as plain code in TypeScript, Go and Python, with no retry library, ready to copy.
How to use it
Change any number and everything updates as you type. Leave the timeout empty if your calls have none; the worst case then counts only the waiting. Try each jitter and watch the second chart flatten. Press Try an example for a slower, busier service, or Reset to go back to where you started. When you like the plan, pick a language and press Copy.
How I built it
It is a small client island with no dependencies, and it sends nothing anywhere. The maths is plain: each planned wait is the first wait times the multiplier to the power of the retry, never more than the cap. Full jitter picks from zero to that wait, equal jitter keeps half and randomises the other half, and decorrelated jitter follows the version from the AWS architecture blog, with your multiplier in place of their 3.
The simulation is seeded with mulberry32, so the same plan always draws the same chart, and my tests pin the exact numbers. Every try fails at once, the worst case for a herd. At 10,000 clients and 20 attempts it draws 190,000 random numbers, which takes a few milliseconds, so it runs right on the page with no worker. The charts are SVG drawn from those counts.