codewithgirish / Lab
Fixed window or token bucket?
Send the same traffic through two rate limiters and compare every decision on a shared timeline. The simulation runs in your browser and gives the same result every time.
Experiment 01 / Rate limiting
Browser simulationLimit 10 maps to: fixed window 10 requests per 1,000 ms · token bucket capacity 10, refill 10 tokens/s
- accepted
- rejected
Input 10 requests at 950 ms and 10 at 1,050 ms, either side of the window boundary at 1,000 ms.
Fixed window 20/20 acceptedToken bucket 11/20 accepted
The fixed window accepted 20/20 because its quota reset to 10 at the 1,000 ms boundary, so each burst landed in a fresh window. The token bucket accepted 11/20: it spent its 10 tokens on the first burst and refilled 1 token in the 100 ms before the second. Neither is broken; they enforce different policies about short-term bursts.
Preview Run the simulation to see each decision. Changing a setting stops playback and clears the result.
Request log (20 requests, results after the run)
| Time (ms) | Requests | Fixed window accepted | Token bucket accepted |
|---|---|---|---|
| 950 | 10 | — | — |
| 1,050 | 10 | — | — |
How each one decides
Fixed window
Time is cut into one-second windows aligned at 0, 1,000, 2,000 ms. Each window has its own counter. A request is accepted while the counter is below the limit. A request exactly on a boundary belongs to the new window, whose counter starts at zero.
Token bucket
The bucket starts full, holding as many tokens as the limit. Before each request it refills continuously for the time that has passed, capped at its capacity. A request is accepted if at least one whole token is available, and it spends that token. Requests with the same timestamp share the bucket with no refill between them.
One limit, two meanings
The shared limit becomes the fixed window's requests per second, and the bucket's capacity and refill per second. Both allow the same long-run average rate. They differ in how much of it can arrive at once.
The boundary effect
In the boundary burst, the first burst lands at the end of one window and the second at the start of the next. The fixed window sees two separate windows, each within its limit, so it accepts both: twice the limit inside 100 ms. The token bucket remembers the first burst. Its tokens are spent, and 100 ms of refill buys back only a tenth of the limit.
This is a consequence of the policy, not a bug in fixed windows. A fixed window promises at most the limit per aligned window, and it keeps that promise. If short spikes across a boundary matter, a sliding window or a token bucket expresses that policy more directly.
For production context on why services rate-limit and how token buckets are used, see Stripe's Scaling your API with rate limiters.
About this simulation
- Both limiters receive exactly the same ordered list of request timestamps.
- Decisions use simulation time in whole milliseconds. Playback only animates a playhead over results that are already computed.
- Token refills are tracked in thousandths of a token, so they are exact for these inputs.
- Nothing is sent to a server. This is a teaching model, not a benchmark of any real system.