Try
↑ ↓ move↵ openesc close
In this lesson · topic 5 of 7
  1. 1. Why callers need a limit
  2. 2. Five ways to count
  3. 3. Race five limiters across a window edge
  4. 4. What each limiter admits at the edge
  5. 5. The sliding counter, worked by hand
  6. 6. Pricing requests and stacking limits
  7. 7. Choosing a limiter under pressure

Topic 5

The sliding counter, worked by hand

6 min read

The sliding window counter is the algorithm people trust without checking. It is worth doing the sum once, because the sum shows both why it works and where it lies.

The estimate

A limiter allows 100 requests per 60 seconds. It stores three things per caller: the count of the previous fixed window, the count of the current one, and when the current one started.

At any moment, the question is how many requests arrived in the last 60 seconds. The counter does not know. It guesses by assuming the previous window’s requests were spread evenly:

estimate = previous count × share of the previous window still in view + current count

The share in view is one minus the fraction of the current window already elapsed.

A worked run

The previous window saw 84 requests. The current window has seen 36 so far, and it is 15 seconds old, so 25 percent of it has passed.

  • Share of the previous window in view: 1 − 0.25 = 0.75.
  • Estimate: 84 × 0.75 + 36 = 63 + 36 = 99.

The estimate is under 100, so the next request is admitted. The current count becomes 37 and the estimate becomes 100. The request after that is rejected, because the estimate is at the limit.

Fifteen seconds later the weight of the previous window has dropped to 0.5. The estimate is 84 × 0.5 + 37 = 79, and the caller can send 21 more. The allowance comes back smoothly, not all at once at the edge. That smooth return is what the fixed window lacks.

Predict first

The limit is 100 per minute. The previous window had 40 requests and the current one has 70, and it is exactly halfway through. Treating time as frozen, how many more requests are admitted?

Where the estimate lies

The even-spread assumption is the whole approximation. Keep the same numbers, 84 before and 36 now, 15 seconds in, and change only where the 84 fell.

All 84 bunched at the very end of the previous window. The true trailing 60 seconds covers the last 45 seconds of the previous window and the first 15 of this one. All 84 are inside it. The true count is 84 + 36 = 120. The estimate said 99. The limiter admits traffic it should have refused, by up to 21 here.

All 84 bunched at the very start of the previous window. None of them are inside the trailing 60 seconds, because that span starts 45 seconds before the edge. The true count is 36. The estimate still says 99. The limiter refuses traffic it should have admitted, by up to 63 here.

So the error is bounded by the previous window’s count, and it is smaller early in a window than late. Both directions are possible. For traffic that is roughly smooth, which is most traffic from many independent users, the error is small. For a single caller that fires in bursts on a schedule, such as a cron job at the top of the minute, the error is the point.

Checking it in the lab

In the rate limiter lab, pick the boundary traffic and read the worst-window column for the sliding counter and the fixed window. Then switch to bursty traffic and compare the counter with the log. The counter should stay at or just above the log. If you can make it go far above, you have found the bunching case above.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms