Avoid synchronised stampedes
Thousands of clients or keys retry, reconnect or expire at the same instant.
Basic solution
Add jitter to retries, TTLs and reconnects; warm caches gradually; serve stale while refreshing.
Reconnect with and without jitter
A restarted connection server drops 250K sockets. Without jitter they all sync within a second and overload the chat service; with 0 to 30 s of jitter the same work arrives at 8K/s.
How it works
- Find the events that make many clients or keys act at the same instant: a connection server restarting and dropping all its sockets, a batch of cache keys written together and therefore expiring together, a rate-limit window resetting on the minute, a cron job that fires on every host at :00, a push notification that makes a million phones open the app at once.
- For events driven by clients, add a random delay before the client acts: reconnect after a random 0 to 30 s, retry with exponential backoff plus a random jitter. The work is the same; it is spread across the window instead of landing in one second.
- For events driven by expiry, add randomness to the TTL (the base value plus up to 10 to 20%) so keys written together expire spread out, and refresh very hot keys shortly before they expire, each with some probability, so they rarely miss at all.
- When a miss on a hot key does happen, let one request fetch the value and make the others wait for its result (single-flight), or serve the stale value to everyone while that one refresh runs.
- For scheduled jobs, compute an offset within the window from a hash of the host name or job id, so each host runs at a different second instead of all at the top of the minute.
- For planned mass events such as a launch, warm the caches and scale the fleet before the event, and rate-limit the burst at the edge so only what fits gets through.
The principle
- Spread in time what would otherwise be a spike. The total work is unchanged; the peak is what breaks systems.
Worked example
Chat connection servers
- 50M online users across 200 connection servers, 250K WebSockets each. A server is restarted for a deploy.
- Without jitter: 250K clients detect the drop and reconnect within about 1 s. The balancer spreads them across 199 servers, 1,250 each, fine. But each reconnect does a registry write and a sync request to the chat service: 250K sync calls in one second, against a chat service sized for 20K/s. The chat service falls over, which drops more connections, which reconnect, and so on.
- With reconnect jitter of 0 to 30 s: 250K reconnects become about 8,300/s, and sync calls 8,300/s, well within capacity. The deploy of all 200 servers, one every 30 s, is invisible.
- Cache TTLs in the same system: presence entries with a flat 60 s TTL written at login all expire together after a login surge. With TTL = 60 s + random(0 to 12 s) the expiry spreads across 12 seconds.
- The 9 a.m. push: a notification to 10M users makes 2M of them open the app in a minute. That herd is intentional, so it is handled by pre-scaling the API fleet 10 minutes before the send and sending in waves of 500K per minute.
Variants and when each wins
Jitter on client timers
- Reconnect and retry delays are randomised so clients that failed together do not act together. It wins as the cheapest fix with the biggest effect for client-driven herds.
TTL jitter and early refresh
- Expiry times are randomised and hot keys are refreshed slightly before they expire. It wins for expiry-driven herds in caches, because keys stop expiring in lockstep.
Single-flight and stale-while-revalidate
- When a miss happens, one request does the fetch and the others reuse its result or receive the stale value. It wins for the misses that still occur, because N identical fetches become one.
Rate limiting the burst
- The edge admits the herd at a controlled rate and queues or rejects the rest. It wins when the herd is intentional or unavoidable, such as a product launch.
Recommendation: jitter everywhere by default, single-flight on every cache load, and pre-scaling plus waves for planned events.
Failure modes
Retries without jitter
- After a brief outage, load returns in perfect waves at 1 s, 2 s and 4 s, each re-breaking the service. It happens because every client runs the same exponential backoff schedule from the same moment. Use full jitter: a random delay between zero and the backoff cap.
Keys written together expire together
- Every hour on the hour, database load spikes 20×. It happens because a batch job wrote a million keys with the same TTL at the same time. Add jitter to the TTL, or stagger the batch itself.
Cron on every host at :00
- CPU spikes across the whole cluster at the top of each hour. It happens because every host runs the same cron job at the same second. Offset each host's schedule by a hash of its name, or run the job once from a single scheduler that holds a lock.
Reconnect storm after a load balancer change
- A balancer config push drops every connection at once, and the reconnects overwhelm the backend. It happens because the balancer closed all connections together and the clients reconnected immediately. Drain connections gradually on the balancer, and have clients reconnect with jitter and a cap on attempts per second.
Herd hidden by a cache that then expires
- A viral post is served fine for five minutes, then the site stalls. It happens because the one hot key expired and thousands of requests missed together with no single-flight. Add single-flight, and refresh early any key whose request rate is above a threshold.
Interviewer follow-ups
Answer out loud before you open each one.
What does "full jitter" mean and why is it better than a fixed backoff?
Instead of waiting exactly base × 2^attempt, wait a random time between 0 and that value. Fixed backoff keeps clients that failed together in lockstep, so they retry together; full jitter spreads them uniformly across the window, which smooths load the most for a given cap.
How much TTL jitter is enough?
Enough that the expiry spread is wider than the time it takes to repopulate. Ten to twenty percent of the base TTL is a common choice; a 5 minute TTL with 30 to 60 s of spread turns a 1 s spike into a gentle ramp. More than that and your effective TTL becomes hard to reason about.
How do you handle a herd you cannot spread, like a product launch?
Pre-warm the caches with the launch content, scale the fleet before the event rather than waiting for autoscaling, put a queue or waiting room in front of the write path, and shed low-value traffic at the edge. You cannot remove the spike, so you make the system ready for it and bound what gets through.
What is the relationship with single-flight and stale-while-revalidate?
Jitter reduces how often many requests miss together; single-flight limits the damage when they do by making one request do the fetch; stale-while-revalidate removes the wait entirely by serving the old value during the refresh. Together they make a cache miss on a hot key cost one backend request and no user-visible latency.
Connections
- Pairs with
- Retry transient failures carefully · Protect against a cache stampede · Collapse duplicate in-flight requests · Limit requests per client · Push to the browser in real time
- Used in
- Distributed Key-Value Cache (Redis / Memcached-like) · Distributed Rate Limiter · Chat App (WhatsApp / Messenger)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 | Connection server 17, Other servers | Compute Engine | Azure Virtual Machines | DigitalOcean Droplets, any VM or bare metal |
| Amazon EC2 Auto Scaling | Chat service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |