Skip to content

Sliding windows: exact, or estimated

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 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.

Traffic
Pattern
5 per second

Nothing, then 50 requests in each of 2 seconds, every 20 seconds. The average stays at the load.

Limiter
Limiter shown
5 per second

Requests a second the limit allows on average. A window allows this times its length: 50 in 10 seconds.

50

Tokens in the token bucket, and places in the leaky bucket's queue. The three window limiters do not use it.

10 s

Seconds in a window, for the three window limiters. Windows start at 0 and then at every multiple of their length.

Gateways
Gateways
1
Counters
0 s

Seconds by which a gateway reads the shared counter late. It decides on the counter as it was then, plus what it has let in itself since.

A balancer sends each request to one gateway at random. Changing the rate, the bucket, the window or the gateways starts the run again from second 0, so that all five limiters are compared from the same start.

Time controls
Time: 40 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

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
Requests each second, accepted and rejected
02550040 seconds

Accepted Rejected (hatched) Rate limit, 5 a second

The five limiters on the same requests, after 40 s
LimiterAcceptedRejectedMost in one secondMost in 10 sKept in memory
Token bucket1109050551 (the tokens left)
Leaky bucket (queue)110905505 (requests waiting in the queue)
Fixed window2000501001 (the count of this window)
Sliding window log (shown)10010050500 (a timestamp for each accepted request)
Sliding window counter10010050502 (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
The last 20 seconds, Sliding window log
SecondArrivedAcceptedRejectedReached the service behind
200000
210000
220000
230000
240000
250000
260000
270000
280000
295050050
30500500
310000
320000
330000
340000
350000
360000
370000
380000
390000

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’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.

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.

Traffic
Pattern
5 per second

Most seconds carry about half the load, and about one second in eight carries 5 times the load. The seeded draws put the spikes in the same seconds on every run.

Limiter
Limiter shown
5 per second

Requests a second the limit allows on average. A window allows this times its length: 50 in 10 seconds.

50

Tokens in the token bucket, and places in the leaky bucket's queue. The three window limiters do not use it.

10 s

Seconds in a window, for the three window limiters. Windows start at 0 and then at every multiple of their length.

Gateways
Gateways
1
Counters
0 s

Seconds by which a gateway reads the shared counter late. It decides on the counter as it was then, plus what it has let in itself since.

A balancer sends each request to one gateway at random. Changing the rate, the bucket, the window or the gateways starts the run again from second 0, so that all five limiters are compared from the same start.

Time controls
Time: 40 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

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
Requests each second, accepted and rejected
02550040 seconds

Accepted Rejected (hatched) Rate limit, 5 a second Window boundary every 10 s

The five limiters on the same requests, after 40 s
LimiterAcceptedRejectedMost in one secondMost in 10 sKept in memory
Token bucket186025741 (the tokens left)
Leaky bucket (queue)18605503 (requests waiting in the queue)
Fixed window180625721 (the count of this window)
Sliding window log16026255030 (a timestamp for each accepted request)
Sliding window counter (shown)1592725522 (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
The last 20 seconds, Sliding window counter
SecondArrivedAcceptedRejectedReached the service behind
203303
213303
2225111411
233303
243303
253303
263303
273303
283303
293303
303303
313303
323303
333303
343303
353303
363303
373303
383303
393303

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.

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 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.

Traffic
Pattern
4 per second

Most seconds carry about half the load, and about one second in eight carries 5 times the load. The seeded draws put the spikes in the same seconds on every run.

Limiter
Limiter shown
5 per second

Requests a second the limit allows on average. A window allows this times its length: 50 in 10 seconds.

20

Tokens in the token bucket, and places in the leaky bucket's queue. The three window limiters do not use it.

10 s

Seconds in a window, for the three window limiters. Windows start at 0 and then at every multiple of their length.

Gateways
Gateways
1
Counters
0 s

Seconds by which a gateway reads the shared counter late. It decides on the counter as it was then, plus what it has let in itself since.

A balancer sends each request to one gateway at random. Changing the rate, the bucket, the window or the gateways starts the run again from second 0, so that all five limiters are compared from the same start.

Time controls
Time: 40 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

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
Requests each second, accepted and rejected
01020040 seconds

Accepted Rejected (hatched) Rate limit, 5 a second

The five limiters on the same requests, after 40 s
LimiterAcceptedRejectedMost in one secondMost in 10 sKept in memory
Token bucket (shown)131320531 (the tokens left)
Leaky bucket (queue)13135502 (requests waiting in the queue)
Fixed window134020561 (the count of this window)
Sliding window log1286205020 (a timestamp for each accepted request)
Sliding window counter129520512 (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
The last 20 seconds, Token bucket
SecondArrivedAcceptedRejectedReached the service behind
202202
212202
222017317
232202
242202
252202
262202
272202
282202
292202
302202
312202
322202
332202
342202
352202
362202
372202
382202
392202

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.

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 your understanding: windows

This quiz needs JavaScript to check your answers.

Question 1 of 3

A fixed window limiter allows 50 requests in each block of 10 seconds. The blocks start at second 0, 10, 20 and so on. A client sends 50 requests at second 9 and 50 more at second 10.

How many of the 100 requests are accepted?

Enter a number and choose its unit.

requests
Question 2 of 3

The same two bursts, 50 requests at second 9 and 50 at second 10, meet a sliding window log that allows 50 requests in any 10 seconds.

What happens?

Choose one answer.

Question 3 of 3

A sliding window counter allows 50 requests in 10 seconds. The window from second 0 to 9 counted 50 requests. At second 10, the first second of the next window, 50 new requests arrive.

How many of the new requests are accepted?

Choose one answer.

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.
  1. Line 1 of 10A client that has been quiet can send 60 requests at once, and then only 5 a second.
  2. Line 2 of 10The delay before the next request is allowed is exactly (1 − tokens) / rate.
  3. Line 3 of 10The service behind never sees more than 5 requests in a second, whatever the burst.
  4. Line 4 of 10The last of 60 queued requests waits about 12 seconds before it reaches the service.
  5. Line 5 of 10nginx's limit_req delays requests above the rate, up to its burst, and refuses the rest.
  6. Line 6 of 10It stores a single count and the number of the window it belongs to.
  7. Line 7 of 10A burst just before a boundary and another just after it both get the full allowance.
  8. Line 8 of 10In any 10 seconds, no more than the limit gets through, because every accepted request is remembered.
  9. Line 9 of 10It keeps two counts and weighs the older one by how much of its window is still inside the span.
  10. Line 10 of 10Its memory for one client grows with the number of requests it has accepted in the window.

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.