Algorithms · Safety nets

Exponential backoff with jitter

Why AWS adds randomness to “try again”. When everyone retries on the same beat, they crash the server again together. A little randomness breaks the rhythm.

The problem

Picture a service run by Amazon Web Services that many clients call at once. Then it stumbles for a moment and gets overloaded. Every client that was turned away does the sensible thing: it retries.

Now the service is trying to recover while all those clients are knocking on the door again. Marc Brooker's 2015 AWS Architecture Blog post, Exponential Backoff And Jitter, is about this moment: many clients competing, all retrying.

The obvious way

Wait a little, then try again. To be polite, double the wait after each failure: this is exponential backoff. It sounds like it should spread the load out. It doesn't, because clients that failed together also wait the same time. They come back together, collide, fail together, and double together. The post shows that even with backoff, retries still arrive in synchronized clusters.

The clever idea

Imagine 100 people leaving a stadium through a few gates. If everyone says "let's all try again in exactly 10 minutes," the crowd just re-forms at the gates. If each person picks their own random moment, the crowd thins into a trickle.

That's jitter: randomness added to the wait. With the post's "Full Jitter," a client sleeps a random time between 0 and min(cap, base * 2^attempt). The backoff ceiling still grows, but each client lands somewhere different below it. The post also compares "Equal Jitter" and "Decorrelated Jitter", and finds the jittered approaches do much less total work.

How the code does it

Every client calls at slot 0, but each slot serves only 10, so the capacity check turns the rest away. Each loser gets a longer wait from the doubling line, capped so it never explodes.

Then this one line is the whole experiment. Without jitter, every client that failed together gets the same wait, so they return together. With jitter, they scatter. The three charts show it: total calls first, then the plain run's tall spikes next to the jittered run's flatter spread. Fewer wasted calls means less work for the service. (One tweak: my random wait starts at 1 slot, not 0, because slot 0 has already passed in this simulation.)

Trade-offs

Jitter makes any single client's wait unpredictable: one might get lucky and return quickly, another might sleep near the cap. It also makes behaviour harder to debug, since no two runs look alike (that's why the code fixes a seed). And if only one client is ever retrying, there's no crowd to spread out, so jitter buys you little.

Where else you’ll see it

A 2023 update to the post notes this is still how AWS SDKs build resilient clients. The same idea fits any code that retries a shared service: API clients, queue workers, reconnecting apps.

Two clients fail together at slot 0 using plain exponential backoff (no jitter). When do they retry?

  • At the same slot as each other
  • At random different slots
  • One right away, one later
Show the answer

At the same slot as each other. Same failure time plus the same formula gives the same wait, so they stay in lockstep. Jitter is what breaks the tie.

Change it and see
  • Set CAPACITY to 100 and rerun
  • Change CAP from 64 to 512
  • Set BASE to 2, compare spikes

The code

Python, with what each line means beside it. It compares the clever way with retrying on a fixed schedule (or plain exponential backoff).

import randomBorrow Python's random number tools
CLIENTS, CAPACITY, BASE, CAP = 100, 10, 8, 64Set clients, capacity, base wait, and cap
def simulate(jitter):Define one simulation, jitter on or off
random.seed(1)Fix the random seed for repeatable runs
due = {c: 0 for c in range(CLIENTS)}Every client's first call is at slot 0
tries = {c: 0 for c in range(CLIENTS)}Everyone starts with zero failed tries
load, calls = {}, 0Start an empty load log and call count
while due:Keep going while any client still waits
slot = min(due.values())Jump to the next slot someone calls
batch = [c for c in due if due[c] == slot]Collect the clients calling in that slot
load[slot] = len(batch)Record how many calls hit this slot
calls += len(batch)Add them to the total call count
for i, c in enumerate(batch):Walk through the callers in order
if i < CAPACITY:The first few fit within capacity
del due[c]They succeed and leave the waiting list
else:The rest are turned away
tries[c] += 1Count one more failed try
wait = min(CAP, BASE * 2 ** tries[c])Double the wait each time, up to cap
due[c] = slot + (random.randint(1, wait) if jitter else wait)Schedule retry: fixed wait, or random one
return calls, [load.get(s, 0) for s in range(60)]Return total calls and first 60 slots
plain_calls, plain_load = simulate(jitter=False)Run the obvious way, no randomness
jitter_calls, jitter_load = simulate(jitter=True)Run the clever way, full jitter
show([plain_calls, jitter_calls], "total calls: plain vs jitter")Chart total calls side by side
show(plain_load, "plain: requests per slot")Chart the plain load per slot
show(jitter_load, "full jitter: requests per slot")Chart the jittered load per slot

Read more

Run it. Then change it.

The code runs right in the page and draws as it goes. Free, nothing to install.

More on safety nets