Today I learned: There are three kinds of jitter

July 17, 2024

Key takeaway: Add jitter to every retry loop, Full Jitter by default, and cap both the delay and the number of retries.

Exponential backoff alone doesn’t stop a thundering herd. You need jitter, and which kind you pick matters a lot less than picking one.

A dependency blips for two seconds and every client sees an error at the same instant. With backoff, they all wait exactly 100ms, retry together, fail together, then all wait exactly 200ms. Backoff changes how often each client retries, not when, so every retry wave still has every client in it.

Jitter adds randomness so clients spread out. Marc Brooker’s Exponential Backoff And Jitter on the AWS Architecture Blog compares three kinds:

  • Full Jitter waits a random time from zero up to the backoff value. It spreads clients the widest, though an unlucky one retries almost immediately.
  • Equal Jitter keeps half the backoff and randomizes the other half, so everyone waits at least some minimum.
  • Decorrelated Jitter picks a random wait between the base delay and three times the previous wait, so clients that started in lockstep drift apart quickly.

In Brooker’s simulations, all three did far less total work than backoff with no jitter. If I had to pick a default, it’s Full Jitter. It’s one line:

def full_jitter(attempt)
  rand(0.0..backoff(attempt))
end

A few things around the formula matter just as much. Cap the delay, or a client failing for an hour waits most of a day between attempts. Cap the retries, because each one adds load to a system that’s already struggling. And only retry what’s safe. A GET is fine. A POST that charges a card needs an idempotency key first.