What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

A Redis sliding-window counter estimates how many requests arrived in the most recent rolling interval by combining the current fixed-window count with a weighted share of the previous one. It smooths the sharp boundary of a basic fixed-window limit while storing two counters per identity instead of a timestamp for every request. The trade-off is that the result is an estimate, not an exact rolling count.

How the sliding-window counter estimates a rolling quota

Suppose a service allows 100 requests per minute. A sliding-window counter divides time into fixed one-minute buckets and keeps the count for the current bucket and the immediately preceding bucket. To estimate requests in the latest 60 seconds, it takes the current count and adds the previous count multiplied by the fraction of that prior bucket that still overlaps those 60 seconds.

Estimated count = current-window count + previous-window count × (1 − elapsed fraction of current window)

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For an illustrative example, assume the current minute is 30 seconds old. Half of the previous minute overlaps the latest 60 seconds, so the estimate is the current count plus half of the previous count. If the previous minute held 40 requests and the current minute has 30, the estimate is 30 + (40 × 0.5) = 50. These numbers illustrate the calculation; they are not performance or accuracy measurements.

At the start of a bucket, almost all of the previous bucket overlaps the rolling interval. As the current bucket progresses, the previous bucket’s weight decreases toward zero. This gradual adjustment reduces the abrupt reset that lets a fixed-window counter admit a burst around a boundary.

What the estimate does—and does not—guarantee

The weighting assumes requests in the previous bucket were spread evenly over time. A counter does not retain their individual timestamps, so it cannot know exactly how many are still inside the rolling interval. If requests were concentrated early or late in that bucket, the weighted share can differ from the true rolling count. Redis’s March 20, 2026 tutorial describes the method as near-exact, not exact.

That approximation is the point of the design: two counters per identity use much less state than retaining every request timestamp, while providing smoother boundary behavior than a single fixed counter. It is appropriate when an efficient, useful estimate is sufficient. If the policy requires an exact rolling count, use a design that tracks individual request times or otherwise preserves the necessary event detail.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How Redis applies the decision atomically

Redis’s tutorial implementation keeps current and previous counts in string keys. A Lua script reads the counts, computes the weighted estimate, decides whether to admit the request, and increments the current counter as one atomic server-side operation. Atomicity matters when multiple service instances share the limiter: one request must not read an old count, make an admission decision, and then overwrite or race another request’s update.

The tutorial expires the current key when it is first created, and uses a common hash tag in the two key names so Redis Cluster places them in the same slot. These are implementation details of that example, not universal protocol requirements. A production design should ensure both counters represent the intended adjacent time buckets, handle a missing key as a zero count, and apply a consistent policy for what is counted. In the admission-control example, denied requests are not added as admitted traffic.

Expiration also needs to be coordinated with counter updates. Redis documents a failure mode for a simple separate INCR then EXPIRE sequence: if the increment succeeds but setting the expiry fails, the counter can remain without an expiry. A Lua script can make the increment-and-expiration sequence atomic. That concern is distinct from the sliding-window weighting itself, but it is important for any expiring Redis counter.

How it compares with other rate-limiting algorithms

Algorithm Boundary accuracy Storage per identity Burst behavior Implementation trade-off
Fixed window Counts accurately within each fixed bucket, but does not represent a rolling interval. Low: a counter for the active bucket. Requests can cluster on opposite sides of a boundary, allowing a burst across adjacent windows. Simplest approach; suitable when boundary bursts are acceptable.
Sliding-window log Exact rolling-window view in the Redis tutorial’s comparison, because it retains individual request timestamps. Proportional to requests retained in the window. Applies a rolling quota without fixed-bucket boundary resets. More state and bookkeeping: store timestamps, remove expired entries, then count.
Sliding-window counter Near-exact estimate; weighted prior count smooths the boundary but cannot reconstruct request timing. Two string counters in Redis’s tutorial example. Smoother than fixed windows; burst allowance still depends on the quota and admission policy. Balances lower storage with a useful rolling estimate.
Token bucket Not a rolling-count method; governs tokens replenished over time. Depends on implementation. Useful when controlled bursts should be allowed. Choose it when burst tolerance is an explicit requirement.
Leaky bucket Not a rolling-count method; constrains or regulates flow. Depends on implementation. Useful when the design needs strict no-burst behavior; distinguish policing, which rejects excess traffic, from shaping, which queues or smooths it. Choose based on whether excess requests should be rejected or delayed.

There is no single best algorithm. William Johnston, author of Redis’s tutorial, puts it plainly: “There’s no single best algorithm.” The right choice follows from the policy: whether the quota must be exact, how much burst capacity is acceptable, how much state each identity can consume, and whether excess traffic should be rejected or paced.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Where Redis INCREX fits

Redis 8.8 documentation describes INCREX as a native operation combining counter increments, bounds, and expiration. It may simplify common counter-based rate-limit patterns. It should not be treated as a drop-in implementation of the weighted two-counter sliding-window algorithm: that depends on the exact command semantics and the Redis version deployed. Verify those semantics against the target version before using it for this design.

Choosing and operating the design

  • Choose the sliding-window counter when reducing per-identity storage matters and an estimate is acceptable.
  • Choose a sliding-window log when an exact rolling-window count justifies retaining and expiring individual timestamps.
  • Choose a token bucket when controlled bursts are part of the intended policy, or a leaky-bucket design when traffic must be paced or excess requests rejected without bursts.
  • Use atomic server-side decision and update logic when concurrent service instances share Redis state.
  • Decide explicitly whether rejected requests count toward any separate abuse or billing metric; the admission counter and those metrics may have different meanings.

Redis’s tutorial comparisons are qualitative; they do not establish throughput, latency, memory benchmarks, or a numeric error bound for the estimate. Treat the weighted count as a practical approximation and validate that its behavior matches the quota policy and traffic patterns of the service using it.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.