System design lab · Episode 9

Retries, backoff and jitter

A server restarts and 30 users try to reconnect. If they all try again at the same moment, they knock it over again, and again. The fix is two small ideas: wait longer after each failure, and add a random part to the wait. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
A lit lamp: a user who is connectedThe dial on the server: attempts in the last secondThe tray: attempts the server turned away

Controls

Wait before trying again
Time

Want the details?

Why retries can hurt

Networks and servers fail for a moment all the time, so clients retry: they try the same request again. That is good. But when a server fails for everyone at once, every client starts retrying at the same moment. If they all use the same waiting time, they stay in step, and their retries arrive together, like a crowd pushing through one door. This is called a thundering herd or a retry storm. The server that just came back falls over again under the crowd.

Two ideas fix it. Backoff: wait longer after each failure, so the total load goes down. Jitter: make each wait partly random, so the clients get out of step. You need both.

How to read the bench
Part on the benchWhat it means in a real system
30 lamps at the left30 users of an app, each with a long-lived connection (a chat app, a game, a phone app with live updates). Lit: connected.
Ball on the laneOne attempt to connect.
Dial on the serverAttempts in the last second. The marked part from 18 up is where the server falls over.
TrayAttempts the server turned away, because it was down or already full for this second.
Cross on the serverThe server is down: restarting, or fallen over.
Bars under the pictureAttempts in each of the last 20 seconds. A striped bar reached the crash line.
The numbers
  • 30 users. The server accepts up to 10 new connections a second.
  • 18 or more attempts within one second knock it over for 2 seconds, and it drops everyone who was connected.
  • The planned restart takes 3 seconds.
  • Doubling: 1, 2, 4, 8, then 16 seconds at most.
  • Doubling + random ("full jitter"): a random wait between 0 and the doubling wait.
  • Over one minute: 1 second fixed, about 1,140 attempts and 28 crashes; random up to 1 s, about 1,700 attempts; doubling, about 410 attempts and 13 crashes. None of them gets everyone back. Doubling + random: 80 to 160 attempts, at most 2 crashes, and everyone back after 7 to 25 seconds (we checked 40 different random seeds).

The page uses a seeded random generator, so the same lesson gives the same run each time, and the test checks every number above.

In real systems
  • Cloud SDKs (AWS, Google Cloud, Azure) retry with exponential backoff and jitter by default.
  • Retry-After. A server can answer 429 or 503 with a Retry-After header that tells the client how long to wait. This is backpressure (the previous lab) sent over the network.
  • Limit retries. Give up after a few attempts, or use a retry budget: retries may be at most, say, 10% of all requests. Otherwise every layer retries and the load multiplies.
  • Retry only what is safe. Reading is safe to repeat. A payment is not, unless it carries an idempotency key (see the lesson on delivery guarantees).
  • Add a circuit breaker (lab 7) so a client stops trying a service that is clearly down.
Words to know
Retry
Sending the same request again after a failure.
Exponential backoff
Doubling the wait after each failure, up to a limit.
Jitter
A random part added to the wait, so clients do not stay in step.
Thundering herd
Many clients waking up and hitting the same thing at the same moment.
Retry budget
A limit on how many retries a client may send, compared to its normal requests.