Sliding windows: exact, or estimated
Windows that move
Section titled “Windows that move”The last lesson ended with a limit that forgets everything at a boundary. A sliding window does not have a boundary: at any moment, the window is the last 10 seconds, whenever “now” is. There are two ways to keep it.
The sliding window log keeps the time of every accepted request. To decide, it throws away the entries older than the window, counts the others, and accepts the request only if the count is below the limit. This is exact: in any 10 seconds, no more than the limit gets through. In the lab, a request at second s counts for exactly the window length, and then leaves.
The sliding window counter keeps two numbers instead of a list: the count of the current fixed window and the count of the one before. It estimates the last 10 seconds as the previous count, weighted by how much of that window is still inside the span, plus the current count. Cloudflare’s article about its rate limiting gives the same formula: the rate is the previous count times the share of the previous period left, plus the current count. Its assumption is stated too: it treats the previous window’s requests as if they had been spread evenly across it.
Mental model. The log remembers every request and counts them again each time. The counter remembers two totals and guesses how many of the older ones are still inside the window. The log is right and heavy. The counter is light and approximately right.
The log: no boundary, a larger bill
Section titled “The log: no boundary, a larger bill”The first scenario is the double burst of the last lesson, now with the Limiter shown set to the sliding window log: 50 requests in each of two consecutive seconds every 20 seconds, against 50 in 10 seconds. Press Pause and then Reset, so that the lab stands at second 40.
The same burst against a window that slides with time: only 50 requests get through in any 10 seconds, so the double burst is gone.
What is happening
The limit is 5 requests a second on average: 50 in 10 seconds.
Sliding window log: the time of every accepted request is kept, and at most 50 are allowed in any 10 seconds. It is exact, and it stores one entry for each accepted request.
Traffic is bursty: nothing arrives, except 50 requests in each of two seconds, every 20 seconds. The average is 5 a second.
At most 50 requests got through in any 10 seconds, within the limit of 50.
- Arrived
- 2000 in the last second
- Accepted
- 10050% of the requests
- Rejected
- 10050% of the requests
- Most in 10 s
- 50the limit is 50
- Most in one second
- 50the rate is 5 a second
Accepted Rejected (hatched) Rate limit, 5 a second
| 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 | 200 | 0 | 50 | 100 | 1 (the count of this window) |
| Sliding window log (shown) | 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 | 0 | 50 | 0 |
| 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.
Of the same 200 requests, 100 are Accepted and 100 are Rejected, and Most in 10 s is 50, never more. The first burst of 50 is accepted whole; the second, one second later, finds the 50 of the second before still inside the window, and is refused. The fixed window accepted all 200 and let 100 through in 10 seconds, so the price of the exact limit is a heavier refusal: 100 requests were turned away where the fixed window turned away none. That is the limit doing its job, since 100 requests in two seconds is twice what was meant to be allowed.
The log’s cost is memory. It stores one entry for each accepted request that is still inside the window. At 5 requests a second, a 10-second window holds 50 entries per client; at 1,000 requests a second it would hold 10,000 per client, and every decision has to clear the old entries. The lab’s table has a column for this, Kept in memory, which the next scenario shows.
The counter: two numbers and a weight
Section titled “The counter: two numbers and a weight”The counter’s estimate is easier to see with numbers. Suppose the window is 10 seconds, the limit is 50, the last window held 40 requests, and we are 3 seconds into the current window, which has 5 requests so far. Seven of the last window’s ten seconds are still inside the span, so the estimate is 40 × 7 / 10 + 5, which is 33. The counter allows 50 − 33, which is 17 more requests. Three seconds later the previous window’s weight is smaller and more room opens, without a boundary where everything is reset.
What happens if the spikes are uneven?
Section titled “What happens if the spikes are uneven?”The second scenario uses seeded spikes: most seconds carry about half of 5 requests, and about one second in eight carries 25. Press Pause and Reset again.
Seeded spikes against two counts and a weighted estimate. It stays close to the exact log, but it is an approximation: here 52 requests get through in one 10-second span, against 50 for the log.
What is happening
The limit is 5 requests a second on average: 50 in 10 seconds.
Sliding window counter: two counts are kept, this window's and the last one's. The last 10 seconds are estimated as the last count, weighted by how much of that window is still inside the span, plus this count. It is an approximation: it assumes the last window's requests were spread evenly.
Traffic is spiky: most seconds carry about half of 5, and about one second in eight carries 25 requests.
52 requests got through in 10 seconds, above the limit of 50.
- Arrived
- 1863 in the last second
- Accepted
- 15985% of the requests
- Rejected
- 2715% of the requests
- Most in 10 s
- 52the limit is 50
- Most in one second
- 25the 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 | 186 | 0 | 25 | 74 | 1 (the tokens left) |
| Leaky bucket (queue) | 186 | 0 | 5 | 50 | 3 (requests waiting in the queue) |
| Fixed window | 180 | 6 | 25 | 72 | 1 (the count of this window) |
| Sliding window log | 160 | 26 | 25 | 50 | 30 (a timestamp for each accepted request) |
| Sliding window counter (shown) | 159 | 27 | 25 | 52 | 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 | 3 | 3 | 0 | 3 |
| 21 | 3 | 3 | 0 | 3 |
| 22 | 25 | 11 | 14 | 11 |
| 23 | 3 | 3 | 0 | 3 |
| 24 | 3 | 3 | 0 | 3 |
| 25 | 3 | 3 | 0 | 3 |
| 26 | 3 | 3 | 0 | 3 |
| 27 | 3 | 3 | 0 | 3 |
| 28 | 3 | 3 | 0 | 3 |
| 29 | 3 | 3 | 0 | 3 |
| 30 | 3 | 3 | 0 | 3 |
| 31 | 3 | 3 | 0 | 3 |
| 32 | 3 | 3 | 0 | 3 |
| 33 | 3 | 3 | 0 | 3 |
| 34 | 3 | 3 | 0 | 3 |
| 35 | 3 | 3 | 0 | 3 |
| 36 | 3 | 3 | 0 | 3 |
| 37 | 3 | 3 | 0 | 3 |
| 38 | 3 | 3 | 0 | 3 |
| 39 | 3 | 3 | 0 | 3 |
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.
At second 40, 186 requests have Arrived. The counter Accepted 159 and Rejected 27, and Most in 10 s is 52, above the limit of 50, and the status sentence says so: 52 requests got through in 10 seconds. The comparison table shows the log on the same requests: 160 accepted, 26 rejected, and 50 at most in 10 seconds. The counter let through 2 more than the limit in its worst span and refused one more request in total. Its assumption that the last window was even is wrong when a spike sits at one end of it: the weight then counts spike requests that have already left the span, or forgets ones that are still in it, and the estimate is a little high or low.
The table’s last column is the reason to accept that error. At second 40 the log keeps 30 timestamps, the counter keeps 2 numbers, and the fixed window and the token bucket keep 1. On the same requests the fixed window accepted 180 and let 72 through in 10 seconds, far above 52, and the token bucket (a bucket of 50 here) let 74 through.
How large is the counter’s error in a real system? Cloudflare tested it on 400 million requests and reported that 0.003% of requests were wrongly allowed or rate limited, and an average difference of 6% between the real rate and the approximate one. Those are Cloudflare’s own figures for its own traffic. The lab’s 52 against 50 is a different measurement, on seeded spikes chosen to show the effect. Neither number predicts the error on your traffic.
A third design: segments
Section titled “A third design: segments”ASP.NET Core’s sliding window limiter takes another approach. It divides the window into segments, for example three segments in a 30-second window, and moves the window one segment at a time. When a segment expires, the requests counted in it are given back to the allowance. The window therefore slides in steps of window length divided by segments, not continuously. More segments make the slide finer and mean more numbers to keep. The lab does not model this variant.
The five limiters on one spiky run
Section titled “The five limiters on one spiky run”The last scenario puts all five limiters side by side. Spikes arrive at random, seeded so that everyone sees the same ones, and the average load is 4 a second against a limit of 5. The table that compares the five limiters shows what each one did with exactly the same requests.
Seeded spikes of 20 requests hit a bucket of 20 tokens that refills at 5 a second. Compare how the five limiters treat the same spikes.
What is happening
The limit is 5 requests a second on average: 50 in 10 seconds.
Token bucket: the bucket holds up to 20 tokens and gets 5 back every second. Each request takes one token, so up to 20 requests can pass at once, and over a long time 5 a second.
Traffic is spiky: most seconds carry about half of 4, and about one second in eight carries 20 requests.
Token bucket: up to 53 requests got through in 10 seconds against 50, because a full bucket lets a burst of 20 pass at once on top of what is refilled.
- Arrived
- 1342 in the last second
- Accepted
- 13198% of the requests
- Rejected
- 32% of the requests
- Most in 10 s
- 53the limit is 50
- Most in one second
- 20the rate is 5 a second
Accepted Rejected (hatched) Rate limit, 5 a second
| Limiter | Accepted | Rejected | Most in one second | Most in 10 s | Kept in memory |
|---|---|---|---|---|---|
| Token bucket (shown) | 131 | 3 | 20 | 53 | 1 (the tokens left) |
| Leaky bucket (queue) | 131 | 3 | 5 | 50 | 2 (requests waiting in the queue) |
| Fixed window | 134 | 0 | 20 | 56 | 1 (the count of this window) |
| Sliding window log | 128 | 6 | 20 | 50 | 20 (a timestamp for each accepted request) |
| Sliding window counter | 129 | 5 | 20 | 51 | 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 | 2 | 2 | 0 | 2 |
| 21 | 2 | 2 | 0 | 2 |
| 22 | 20 | 17 | 3 | 17 |
| 23 | 2 | 2 | 0 | 2 |
| 24 | 2 | 2 | 0 | 2 |
| 25 | 2 | 2 | 0 | 2 |
| 26 | 2 | 2 | 0 | 2 |
| 27 | 2 | 2 | 0 | 2 |
| 28 | 2 | 2 | 0 | 2 |
| 29 | 2 | 2 | 0 | 2 |
| 30 | 2 | 2 | 0 | 2 |
| 31 | 2 | 2 | 0 | 2 |
| 32 | 2 | 2 | 0 | 2 |
| 33 | 2 | 2 | 0 | 2 |
| 34 | 2 | 2 | 0 | 2 |
| 35 | 2 | 2 | 0 | 2 |
| 36 | 2 | 2 | 0 | 2 |
| 37 | 2 | 2 | 0 | 2 |
| 38 | 2 | 2 | 0 | 2 |
| 39 | 2 | 2 | 0 | 2 |
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.
At second 40, 134 requests have Arrived. The fixed window accepts all 134 and rejects none, because a spike that lands in one window is under the limit of its window. The sliding window log rejects 6, the sliding window counter 5, and the token bucket and the leaky bucket 3 each. The fixed window has the highest figure for Most in 10 s, 56, against 50 for the log and the leaky bucket, 51 for the counter and 53 for the token bucket. No limiter is “right” here: they differ in how much of a spike they forgive.
Choosing a limiter
Section titled “Choosing a limiter”| Limiter | Stores | Burst behaviour | Is the last window exact? |
|---|---|---|---|
| Token bucket | tokens left | lets a burst the size of the bucket through | no: it limits the rate with a burst allowance |
| Leaky bucket (queue) | requests waiting | none: smooths to the rate, and the requests wait | no: it limits the output rate |
| Fixed window | one count | up to twice the limit around a boundary | no: the count resets at the boundary |
| Sliding window log | a timestamp per request | none beyond the limit in any window | yes |
| Sliding window counter | two counts | close to the limit, a little over or under | approximately |
The right choice follows from what you need. A client that should be able to burst and then be held to a rate wants a token bucket. A downstream that cannot take a spike wants a leaky bucket. A quota wants a fixed window. A strict “never more than N in any 10 seconds” wants a sliding window, and the counter is the one that keeps the memory small.
Check yourself
Section titled “Check yourself”Check your understanding: windows
This quiz needs JavaScript to check your answers.
Challenge
Which limiter does this?
Four ways to count requests. Put each statement under the limiter it describes: the token bucket, the leaky bucket used as a queue, the fixed window, or a sliding window (the log or the counter).
Sorting needs JavaScript.
Categories
- Token bucket
- Tokens refill at the rate, up to the size of the bucket.
- Leaky bucket (queue)
- Requests wait in a queue that drains at a constant rate.
- Fixed window
- One count for each block of time, reset at the boundary.
- Sliding window
- The last window length before now, kept as a log or estimated from two counts.
Check your answers at least once before looking at the answers.
- A sliding window has no boundary: the window is the last 10 seconds, whenever you ask.
- The log is exact and stores a timestamp for each accepted request: 30 at second 40 in the spiky scenario. On the double burst it accepts 100 of 200 and never lets more than 50 through in 10 seconds.
- The counter stores two numbers and estimates: previous count × the share of that window still inside + current count. With 40 before, 5 now and 3 s in, that is 33, so 17 more are allowed.
- On the spiky traffic the counter lets 52 through in 10 seconds against the log’s 50. Cloudflare reports 0.003% of 400 million requests wrongly handled by this method on its traffic.
- Choose by what you need to protect: a burst allowance, a steady feed, a quota, or a strict window.
Next: distributed limits, when more than one server is counting.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.