The burst at the window edge
A public API: 12,000 req/s, most of it scrapers. Your gateway lets 7,000 a second through, which 4 servers can carry. Yet every minute, right on the minute, the servers fall over.
Play this levelFree with an account. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
A rate limiter has to count requests somehow. The simplest is a fixed window: count per clock minute, and reset to zero on the minute.
Scrapers learn the edge. They send a full minute's worth at 0:59, the counter resets, and they send another full minute's worth at 1:00. For a moment your servers get twice the limit.
A sliding window counts the last 60 seconds, whenever now is. There's no reset to aim at, so no moment ever lets more than the limit through.
You could halve the limit so even the double burst fits, but then you turn away half the people you could serve all day long.
A cheap fix many teams reach for: keep the window and add a per-second cap on top. The burst at the edge can't get past the cap either. The catch shows up in the next level: a cap that tight also refuses your real users' bursts.
What you have to achieve
12,000 requests a second at the busiest hour, 10% of them writes.
- No broken or illegal connections
- No component is overloaded
- At least 6k req/s get an answer
- p99 response time under 350 ms
- Machines cost at most $1,150 / month
What you learn
Same limit, same servers, and no more crash on the minute. A limit is only as good as how it counts: anything that resets on a schedule can be timed.
Check yourself
A fixed window allows 100 a minute. What's the most a client can send in 2 seconds?
- 100
- 200: 100 just before the reset, 100 just after
- 50
Show the answer
200: 100 just before the reset, 100 just after. The counter forgets everything at the reset, so the end of one window and the start of the next can both be full.
Why not just halve the limit?
- It costs more
- Then you turn away half the people you could serve, all day
- Gateways can't go that low
Show the answer
Then you turn away half the people you could serve, all day. The burst only lasts a moment, but a lower limit refuses people every second. Fix how you count, not how much.
You keep the fixed window and add a per-second cap on top. What does that change?
- Nothing: the burst at the edge still gets through
- It stops the edge burst, but also refuses real users' short bursts
- It lets real users burst as much as they like
Show the answer
It stops the edge burst, but also refuses real users' short bursts. The cap stops anyone sending a full minute in one second, scripts and people alike. A dashboard firing 30 calls at once is refused too. Only a token bucket tells the two apart.