The 17th’s race gate: a token bucket, a fixed window and a sliding window decide real requests at their own times. How many get through when a group arrives…
Why it matters
Every serious API has a rate limiter, and its algorithm decides whether a fair burst gets through, whether there is a burst gap at a window’s change, and how much memory it eats when busy. The right answer for a request turned away is 429 Too Many Requests with Retry-After.
The key idea
A token bucket allows bursts, a sliding window counts strictly
Common mistakes
The limit is passed twice over at the minute’s change, though it was never reached That is a fixed window: its count goes back to 0 at each cut of the clock. Requests in the last second of minute A and the first second of minute B can add up to twice the limit in about two seconds. Use a sliding window or a token bucket.
Many requests get through at once though the limit is only 5 That is a token bucket’s burst: a full jar may be emptied up to its capacity, then its refill rate holds. For a strict limit per window with no burst, use a sliding window, or a smaller jar.
The rate limiter eats memory when it is busy A sliding log keeps one time for each request let in, per user. At scale, use a token bucket (two numbers) or a sliding window estimated from two weighted fixed windows.
Try it in this playground
Send A group of 8 to a jar of 5 tickets Five get through at once, three are turned away with a 429.
Wait, then send one more Once the jar is empty, a ticket comes back every second.
Two groups at the window’s edge, with the fixed window Ten get through within one second though the limit is 5 per 5 seconds.
The same pattern with the sliding window Only five: its notebook does not empty at the clock’s cut.