Fixed windows and the boundary burst
The simplest counter
Section titled “The simplest counter”The easiest way to say “no more than 50 requests every 10 seconds” is to cut time into blocks of 10 seconds and count the requests in the current block. When the count reaches 50, refuse. When the block ends, start again from zero. This is the fixed window limiter. ASP.NET Core’s documentation describes it in the same terms: when the time window expires, a new window starts and the request limit resets.
It is attractive for a reason: the whole state is one number, the count of the current window, plus the number of the window it belongs to. It is cheap to store, cheap to update, and easy to explain to a customer (“100 requests per minute”). The windows are aligned to the clock, not to the first request: in the lab they start at 0 and at every multiple of their length.
The problem is the reset. The count goes back to zero at the boundary whatever happened just before it.
Mental model. A fixed window forgets everything at the stroke of the clock. A client that spends its whole allowance in the last second of a window, and again in the first second of the next, has used twice the allowance in two seconds, and no rule was broken.
Try it: the double burst
Section titled “Try it: the double burst”The traffic is bursty: nothing, then 50 requests in each of two consecutive seconds, every 20 seconds. That is 5 a second on average, exactly the Rate limit. The limit is 50 requests in each Window of 10 seconds. The two seconds of a burst are the ninth and the tenth of the cycle: the last second of one window and the first of the next. Press Pause and then Reset, so that the lab stands at second 40.
A burst of 50 requests in the last second of one window and 50 in the first second of the next: all 100 get through in two seconds, twice the limit of 50.
What is happening
The limit is 5 requests a second on average: 50 in 10 seconds.
Fixed window: at most 50 requests in each block of 10 seconds. The blocks start at 0 and at every multiple of 10, and the count goes back to zero at each boundary.
Traffic is bursty: nothing arrives, except 50 requests in each of two seconds, every 20 seconds. The average is 5 a second.
Fixed window: 100 requests got through within 10 seconds, 2 times the limit of 50. A burst at the end of one window and another at the start of the next are counted in two different windows.
- Arrived
- 2000 in the last second
- Accepted
- 200100% of the requests
- Rejected
- 00% of the requests
- Most in 10 s
- 100the limit is 50
- Most in one second
- 50the rate is 5 a second
Accepted Rejected (hatched) Rate limit, 5 a second Window boundary every 10 s
| Limiter | Accepted | Rejected | Most in one second | Most in 10 s | Kept in memory |
|---|---|---|---|---|---|
| Token bucket | 110 | 90 | 50 | 55 | 1 (the tokens left) |
| Leaky bucket (queue) | 110 | 90 | 5 | 50 | 5 (requests waiting in the queue) |
| Fixed window (shown) | 200 | 0 | 50 | 100 | 1 (the count of this window) |
| Sliding window log | 100 | 100 | 50 | 50 | 0 (a timestamp for each accepted request) |
| Sliding window counter | 100 | 100 | 50 | 50 | 2 (this window's count and the last one's) |
All five see exactly the same requests. The most in 10 s is the most that reached the service behind in any 10 consecutive seconds; the limit is 50.
The chart as a table
| Second | Arrived | Accepted | Rejected | Reached the service behind |
|---|---|---|---|---|
| 20 | 0 | 0 | 0 | 0 |
| 21 | 0 | 0 | 0 | 0 |
| 22 | 0 | 0 | 0 | 0 |
| 23 | 0 | 0 | 0 | 0 |
| 24 | 0 | 0 | 0 | 0 |
| 25 | 0 | 0 | 0 | 0 |
| 26 | 0 | 0 | 0 | 0 |
| 27 | 0 | 0 | 0 | 0 |
| 28 | 0 | 0 | 0 | 0 |
| 29 | 50 | 50 | 0 | 50 |
| 30 | 50 | 50 | 0 | 50 |
| 31 | 0 | 0 | 0 | 0 |
| 32 | 0 | 0 | 0 | 0 |
| 33 | 0 | 0 | 0 | 0 |
| 34 | 0 | 0 | 0 | 0 |
| 35 | 0 | 0 | 0 | 0 |
| 36 | 0 | 0 | 0 | 0 |
| 37 | 0 | 0 | 0 | 0 |
| 38 | 0 | 0 | 0 | 0 |
| 39 | 0 | 0 | 0 | 0 |
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Time moves in whole seconds, read from a clock that only moves when the lab says so. In each second a limiter first sees the new time (a refill, a drain, a new window) and then decides on every request that arrives in that second, all at the same instant.
- The three patterns are built from the average load. Steady sends the load every second. Bursty sends nothing, except 10 times the load in each of two seconds (seconds 9 and 10) of every 20. Spiky sends half the load, or 5 times the load in about one second in eight, chosen by a seeded random draw. All five limiters see the same requests.
Simplifications
- The leaky bucket is the queue variant: accepted requests wait in a queue of the bucket's size and leave it at the rate, one second after they arrive at the earliest. The meter variant, which rejects a request when a level that leaks away would overflow, behaves like a token bucket and is not modelled apart.
- Windows start at multiples of their length (0, 10, 20 s and so on), not at a client's first request. A window limiter allows the rate times the window length, and counts only the requests it accepted.
- The sliding window counter keeps this window's count and the last one's, and estimates the span as the last count times the share of that window still inside the span, plus this count, rounded down in whole requests. It assumes the last window's requests were spread evenly, so it can allow a little more or less than the exact log.
- With several gateways a balancer sends each request to one gateway at random (seeded). With local counters each gateway enforces the whole limit alone. With a shared counter there is one counter; with a delay, a gateway decides on the counter as it was that many seconds ago plus what it let in itself since, and the real counter then goes past its limit (a token bucket owes tokens). Within a second the gateways take turns going first.
- Not modelled: the cost and latency of reaching a shared store (the delay stands for them), the failure of a gateway or of the store (fail open or fail closed), one limit for each client, a cost that differs by request, clients that retry after a rejection, and the 429 response with Retry-After. The numbers are floors and illustrations, not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
The limiter shown is the fixed window. Of the 200 requests that have Arrived, 200 are Accepted and none is Rejected. Most in one second is 50, which is allowed. Most in 10 s is 100, twice the limit of 50 that the card under it shows, and the status sentence says it plainly: a burst at the end of one window and another at the start of the next are counted in two different windows. The chart marks the window boundaries, and the two bursts sit on either side of one.
The limit “50 in 10 seconds” was meant to protect the service from more than 50 requests in any 10 seconds, and it let through 100 in two. In general, the worst case of a fixed window is twice its limit within a time as short as you like: a full allowance just before the boundary and a full allowance just after.
What happens if the window is shorter or longer?
Section titled “What happens if the window is shorter or longer?”Change the Window to 8. The run starts again, the limit is now 40 in 8 seconds, and the fixed window accepts 80 requests, rejects 120, and shows Most in 8 s of 40. The double burst is gone. The boundaries are now at 8, 16, 24 and so on, and the two seconds of each burst (9 and 10, then 29 and 30) fall inside the same window, so the second burst finds the allowance already spent.
Now try a Window of 5. The limit is 25 in 5 seconds, a boundary falls between second 9 and second 10, and the sentence is back: 50 requests got through within 5 seconds, 2 times the limit of 25.
The lesson is not that 8 is a good window. This traffic happens to have its bursts at fixed places in a 20-second cycle, and the choice of window decides whether a boundary falls between them. Real traffic does not keep to a cycle. A fixed window always has boundaries, so there is always some traffic that can step over one.
All five on the same requests
Section titled “All five on the same requests”Set the Window back to 10, and read the comparison table under the chart. It runs the five limiters on exactly the same 200 requests.
| Limiter | Accepted | Rejected | Most in one second | Most in 10 s |
|---|---|---|---|---|
| Token bucket (bucket of 50) | 110 | 90 | 50 | 55 |
| Leaky bucket (queue) | 110 | 90 | 5 | 50 |
| Fixed window | 200 | 0 | 50 | 100 |
| Sliding window log | 100 | 100 | 50 | 50 |
| Sliding window counter | 100 | 100 | 50 | 50 |
The fixed window is the only one that accepts everything, and for that reason the only one that lets 100 through in 10 seconds. The token bucket of 50 takes the first burst whole and then only what has been refilled, so it shows 55: the bucket was emptied at second 9, one second’s refill of 5 comes before the second burst, and that is all that gets in. The leaky bucket passes 5 a second to the service, so its worst 10 seconds is 50. The two sliding windows are the subject of the next lesson.
A fixed window has a legitimate place. When the limit is a quota, such as 1,000 requests a day or 10,000 a month, a reset at a boundary is the intended behaviour and a double allowance across midnight is understood. It is a poor tool when the aim is to protect a service from a spike in a few seconds. Whichever you use, the client sees the same refusal: a 429, and a Retry-After that, for a fixed window, can simply be the time to the next boundary.
- A fixed window counts requests in blocks of time and resets at each boundary. It stores one number.
- The reset lets a burst on each side of a boundary pass together. The worst case is twice the limit in a short time.
- In the lab the fixed window accepts all 200 requests and lets 100 through in 10 seconds against a limit of 50. A window of 8 hides this for this traffic, and a window of 5 shows it again.
- The token bucket of 50 shows 55 in 10 seconds, the leaky bucket 50, and the two sliding windows 50.
- A fixed window suits a quota. It is a poor protection against a spike.
Next: sliding windows, which remove the boundary at a price.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.