Skip to content

Lesson 3 of 9 · 24m

Fixed Windows, Sliding Windows, and Counter Cardinality

Compare window algorithms by boundary behavior, memory cost, timestamp sensitivity, and the operational blast radius of high-cardinality keys.

Windowed counters look simpler than token buckets, which is why they end up in production by accident. They are simple only until the boundary arrives: a client sends the full allowance at 11:59:59 and again at 12:00:00. You have either created a burst amplifier or accepted it as the price of approximate enforcement. Know which one you chose.

Fixed windows are cheap and forgiving operationally

A fixed-window counter stores one count per key per period:

ratelimit:tenant:acme:login:2026-11-30T14:20Z = 37

Implementation is easy: increment atomically, set expiration, compare with the cap. Redis makes the shape familiar:

INCR ratelimit:tenant:acme:login:202611301420
EXPIRE ratelimit:tenant:acme:login:202611301420 90 NX

The expiry is intentionally longer than the window so clock skew and command latency do not erase a counter early. The cost is boundary doubling: up to nearly 2N requests can pass in a short interval around the reset.

Sliding-window log is accurate and expensive

A sliding-window log stores event timestamps and counts how many fall inside the last period:

now - 60 seconds < timestamp <= now

This approximates true behavior closely but retains memory proportional to volume. For high-traffic keys, the storage and computation become the abuse target. It is a good fit for low-volume, high-risk actions such as password resets or privileged invitations, not for every read request.

Sliding-window counter is the practical compromise

The common hybrid weights the previous fixed window by overlap:

estimated = current_count + previous_count * (overlap_ratio)

If 20 seconds remain in the current minute, one-third of the previous minute overlaps conceptually. This is not exact; it is a smoothing estimator. It reduces boundary cliffs without storing every event. Say "estimate" in documentation because incident reviews become unpleasant when teams treat it as a ledger.

The key cardinality problem will find you

Every algorithm multiplies state by key cardinality:

per IP           millions of keys in hostile traffic
per account      bounded by account inventory
per target       attacker can mint targets unless constrained
per endpoint     usually bounded and useful as a second dimension

Composite keys are often correct: (tenant, endpoint), (account, action), (target_phone, ip). But every dimension adds storage, observability noise, and incident complexity. Cap key cardinality explicitly, especially when user-controlled strings can enter the key.

Window alignment is a product decision

Calendar windows (per day) reset at a visible boundary and suit quotas. Rolling windows suit abuse prevention. Customers tolerate a daily API quota resetting at midnight; they do not tolerate "you may try your password again in 43 minutes" because their first attempt was 59 minutes ago. Choose semantics that match the control's purpose, then make reset time visible.

Field notes

  • INCR without expiration is a slow memory leak, not a rate limiter.
  • Test boundary behavior intentionally; do not discover double-burst semantics during an incident.
  • Redis clock and application clock are not independent truths when scripts use both.
  • High-cardinality keys can exhaust memory before traffic exhausts application capacity.

Was this lesson helpful?

Sign in to rate