Sliding window rate limiting: counting over a moving period

Sliding window rate limiting counts a client's requests over the most recent period, say the last 60 seconds, rather than over a calendar minute that resets on the clock. That removes the fixed window's weak spot, the boundary where a client can send two full allowances back to back. It comes in two forms: a log that stores every request, and a counter that estimates from two numbers.

Section Rate limiting Updated Variants window log, window counter

Quick facts

Fixed window
One counter per period, reset at a set moment; a client can send up to twice the limit across a boundary (Redis tutorial on five rate limiters)
Window log
One timestamp per request, kept in a sorted set in the Redis example; exact, memory grows with the limit (Redis tutorial on five rate limiters)
Window counter
Two numbers per counter; the previous window is weighted by its overlap with the trailing period (Redis tutorial on five rate limiters)
Accuracy
Approximate: the Redis tutorial rates it near-exact and warns it can let slightly more or fewer requests through than the exact limit (Redis tutorial on five rate limiters)
Neighbours
Token bucket and leaky bucket allow or smooth bursts instead of counting a moving period

All four algorithms are compared on rate limiting algorithms. This page goes deeper on the sliding pair.

The fixed window problem

A fixed window limiter divides time into blocks, keeps one counter per block, and refuses requests once the counter passes the limit. When the block ends, the counter starts again at zero. It is the cheapest design there is. The Redis tutorial lists its weakness as boundary bursts: it allows a burst of up to twice the limit at the edge of a window (source).

The Redis tutorial gives the concrete case: with a limit of 10 requests per 10 seconds, a client sends 10 at second 9 and 10 more at second 11. That is 20 requests in 2 seconds, and every one is inside the rules because the two bursts straddle a reset (source). Scale it to 100 per minute and the same trick yields 200 requests across a couple of seconds [derived].

Whether that matters depends on what the limit protects. For fairness between users it is a nuisance. For a downstream service sized for the stated rate, the doubled burst is the very event the limit was meant to stop. It is also a pattern a client can learn: the reset time is predictable, so a script can wait for it. The simpler counter is described under fixed window rate limiting.

Sliding window log

The log variant stores a timestamp for every accepted request. On each new request it drops timestamps older than the period, counts what is left, and admits the request only if the count is below the limit. The window is exactly the last N seconds, so no boundary exists. The Redis tutorial implements this with a sorted set whose scores are timestamps, pruning with ZREMRANGEBYSCORE and counting with ZCARD (source).

sliding window log, pseudocode
on request(key):
    log = timestamps[key]
    drop every t in log where t <= now - WINDOW
    if len(log) < LIMIT:
        append now to log
        return allow
    oldest = log[0]
    return refuse(retry_after = oldest + WINDOW - now)

Two properties follow from keeping the raw timestamps. The count is exact, and the refusal can say precisely when the oldest request leaves the window, which is the earliest moment a retry can succeed. The cost is storage. For a limit of 10,000 requests per hour, one client's sorted set can hold 10,000 entries, which the tutorial calls not ideal for high-volume, high-cardinality limiting.

Sliding window counter

The counter variant keeps two fixed-window counts, the current block and the one before it, and combines them. The previous count is multiplied by the share of that block still inside the trailing period, then the current count is added. Early in a block the old traffic counts almost fully; late in it, hardly at all.

Here is a worked example [derived]: a limit of 50 requests per minute, 42 requests in the previous minute, 18 in the current minute, and the current minute started 15 seconds ago. The previous window still overlaps the trailing period by (60 − 15) / 60 = 0.75, so the estimate is 42 × 0.75 + 18 = 49.5 requests, and one more request would push it over the limit. The Redis tutorial uses the same weighting, previous count times (1 − elapsed share of the window) plus the current count (source).

sliding window counter, pseudocode
on request(key):
    w = floor(now / WINDOW)
    s = state[key] rolled forward to window w
        (previous = count of w-1, or 0; current = count of w, or 0)
    position = (now mod WINDOW) / WINDOW
    estimate = s.previous * (1 - position) + s.current
    if estimate < LIMIT:
        s.current += 1
        return allow
    return refuse(retry_after = about WINDOW * (1 - position))

Whether the comparison is strict is a design choice; the Redis tutorial refuses when the estimate has reached the limit and increments only otherwise, so refused requests do not inflate the counter.

The estimate rests on an assumption: requests in the previous window were spread evenly, because the weight scales the whole count linearly. Where they were not, the number is off. Here is a case worked from the formula [derived]. With a limit of 100 per minute, a client sends 100 requests in the last second of one minute. A quarter of the way into the next minute the weight of the old count is 0.75, so the estimate starts at 75 and about 25 more requests are admitted. A true rolling window would hold that client at 100 across the same 60 seconds; the estimate has let 125 through.

That is a constructed case, not a measurement. The Redis tutorial says only that the count is an approximation which, in rare edge cases, can let slightly more or fewer requests through than the exact limit, and that for most applications the margin is negligible (source). Treat the size of the error as workload-dependent and measure it on your own traffic.

Memory, accuracy and when to pick which

Three counting methods, per client
MethodState keptAccuracyBoundary burst
Fixed windowOne counterExact inside a blockUp to twice the limit across a reset
Window logOne timestamp per request in the windowExactNone
Window counterTwo countersApproximate, assumes even spreadSmoothed, not removed for concentrated traffic

The Redis tutorial suggests starting with the window counter when unsure, for its balance of accuracy and low memory, and reserving the log for cases where exact counts or an audit trail of timestamps matter, such as payment or authentication endpoints with modest volume. That is a vendor recommendation, not a rule, but it matches the arithmetic: storage per client is fixed for the counter and proportional to the limit for the log.

A few questions settle most choices. Will a client ever be hurt by an estimate that is off by a few percent? Is the limit small enough for a log to stay cheap? Do you need a precise Retry-After value, or is "about the rest of this window" good enough for a 429 Too Many Requests response?

If legitimate clients send bursts, a moving window will refuse them just as a fixed one does, only more evenly. When bursts are normal, a token bucket states the burst allowance outright, and when the system behind the limit needs a steady flow, the leaky bucket algorithm spreads arrivals out. Bursts and rate limits explains why the shape of arrivals matters more than the average.

Keeping it correct in practice

The algorithm is the easy half. A limiter reads state, decides, and writes state, and with concurrent requests two of them can read the same count and both be admitted. The Redis tutorial names this a time-of-check-time-of-use race and runs each decision as one Lua script so the read, decision and write happen as a single step. Its counter variant stores the previous window under a key that outlives the window, with a lifetime of twice the window length, because the next window still needs to read it.

Which key the count belongs to is a separate decision, covered in limiting by IP, user or API key. Spreading the limit across several servers brings its own overshoot, described under distributed rate limits. For the other direction, an existing proxy: nginx rate limiting shows what a configured limit looks like in practice.

Where a WAF helps and where it does not

A rate limit sees requests and a key, nothing more. It cannot tell a customer who clicked refresh four times from a script, and a sliding window will refuse both once the count passes the limit. It does not read request bodies for attacks either; that is the work of filtering rules, a separate layer. What a limit does well is bound how much any one key can ask of an origin in a period.

Choosing a window length and limit blind is how real users get refused. Run the limit first in a mode that records what it would have refused, read the log for who and how often, and only then let it refuse anything. That order is the whole point of monitor mode, and testing a rate limit covers how to check a rule against realistic traffic.

Bridge WAF applies rate limiting per domain, with three modes: off, monitor and block. This page does not state which counting method any part of the platform uses, and nothing here is a claim about live protection.

Questions

What is sliding window rate limiting?

A way of limiting requests in which the period being counted moves with the clock instead of resetting at fixed boundaries. A request is judged against the traffic of the last N seconds, either by listing every request in that span (sliding window log) or by estimating it from two adjacent counters (sliding window counter).

What is the difference between a fixed window and a sliding window?

A fixed window resets its counter at set moments, so a client can spend one full allowance just before a reset and another just after it. A sliding window always looks back over the most recent period, so there is no reset moment to exploit.

Is the sliding window counter exact?

No. It weights the previous window's count by how much of that window still overlaps the trailing period, and it assumes the earlier requests were spread evenly. The Redis tutorial calls it near-exact and notes that in rare edge cases it can let slightly more or fewer requests through than the exact limit; measure the error on your own traffic.

Which uses less memory, the sliding window log or the counter?

The counter. It keeps two numbers per client, while the log keeps one entry per request still inside the window, so its size grows with the limit. The log is the choice when exact counts and exact retry times matter more than storage.

Does this page say which algorithm Bridge WAF uses?

No. Bridge WAF applies rate limiting per domain with three modes: off, monitor and block. This page explains the algorithm family in general and does not state which counting method any part of the platform uses.