Let real users burst
A team dashboard: 3,000 req/s, well under your gateway's limit. But opening the dashboard fires 30 calls at once, and users keep seeing "Too many requests" on half the widgets.
Play this levelFree with an account. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
Real limits are per user: say 10 requests a second each, so one script can't take everyone's share. Your users average 2 a second, far under it.
But averages hide bursts. Opening the dashboard sends 30 calls in the same second. A fixed window, a sliding window or a per-second cap counts all 30 in one window, sees 30 > 10, and refuses 20 of them.
A token bucket gives each user a bucket of tokens that refills at 10 a second and holds, say, 30. Each request spends one. A user who was quiet has a full bucket, so the burst goes through, and the average still can't go over 10.
That's why most API gateways and cloud providers use a token bucket: bursts are normal for people, steady floods aren't.
What you have to achieve
3,000 requests a second at the busiest hour, 10% of them writes.
- No broken or illegal connections
- No component is overloaded
- The rate limit turns away under 0.1% of real users' requests
- Fewer than 0.1% of requests fail
What you learn
Every widget loads, and the limit still holds a script to its average. Pick a limiter for the traffic you want to allow, not just the traffic you want to stop.
Check yourself
Users average 2 req/s against a limit of 10. Why are some refused?
- The limit is too low for the average
- Their requests come in bursts, and one burst can go over 10 in a single window
- The servers are full
Show the answer
Their requests come in bursts, and one burst can go over 10 in a single window. A window counts what lands inside it. 30 calls in one second is over 10, whatever the user did the rest of the minute.
What does a token bucket allow that a window doesn't?
- A higher average
- A short burst, paid for with tokens saved while the user was quiet
- Unlimited requests
Show the answer
A short burst, paid for with tokens saved while the user was quiet. The bucket refills at the limit's rate, so the average is still capped. Only the timing gets more forgiving.