Highlights

Contents

Part IV
04

Sliding window counter

We approximate a moving interval with a small amount of bucket state. The estimate saves memory by assuming where older admissions occurred. We will calculate the interpolation, expose its error, handle skipped buckets, and compare its guarantee with exact history.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Rate limitingrequirements2Fixed window counter3Sliding window log4Sliding window counter5Token bucket6Leaky bucket7Distributed limiting withRedis8Case study: the completelimiter

#Section 13Two counters approximate the recent past

Heron wants smoother boundaries than a fixed window without retaining every timestamp. A sliding-window counter commonly combines the current fixed bucket with a weighted share of the previous bucket. It estimates how much of that older bucket belongs in the trailing interval.

C^=Cnow+(1−f)Cprevious.\widehat C=C_{\text{now}}+(1-f)C_{\text{previous}}.

Read this as estimated recent count C^\widehat C equals the current bucket count plus the previous bucket count multiplied by its remaining overlap fraction. Here ff is elapsed time in the current bucket divided by bucket length. This approximation assumes older admissions were sufficiently spread for time overlap to predict count overlap.

THE SLIDING COUNTER WEIGHS THE PREVIOUS BUCKET BY HOW MUCH OF IT STILL OVERLAPSTHE SLIDING COUNTER WEIGHS THE PREVIOUS BUCKET BY HOW MUCH OF IT STILL OVERLAPSprevious: C_prevcurrent: C_nowoverlap 1 − fĈ = C_now + (1 − f) × C_prevassumes previous events were spread evenly
Figure 12.14Two-bucket interpolation. The estimate is not exact trailing-window history.

It needs a bucket identity as well as counts, because state must distinguish a recent previous bucket from an old one after long inactivity. Serialize rollover and admission in the same operation. Otherwise one gateway can rotate counts while another increments the obsolete bucket.

#Section 14A complete numerical estimate

The illustrative previous bucket count is 80 and current count is 30. At 15 seconds into a 60-second bucket, elapsed fraction is 15 divided by 60, or 0.25. Previous overlap weight is 0.75, so the estimated recent count is 30 plus 80 times 0.75, or 90.

At 30 seconds, the fraction is 0.5 and the same counts produce 30 plus 40, or 70. The estimated count drops as older work ages out even when the current count remains unchanged. A cost-one request checks the estimate including its prospective cost before it is admitted.

WITH 80 PREVIOUS AND 30 CURRENT: ESTIMATE 90 AT 15 S, 70 AT 30 SWITH 80 PREVIOUS AND 30 CURRENT: ESTIMATE 90 AT 15 S, 70 AT 30 S15 s in30 + 80 × 0.759030 s in30 + 80 × 0.570the counts never changed; only the assumed overlap did
Figure 12.15Illustrative counts and times. The drop comes from the assumed overlap, not from observed event expirations.

Floating-point rounding can alter a boundary decision. Use a documented precision and arithmetic representation that matches the policy. Multiplying through by window length can avoid a division in a comparison, but overflow and clock units still require validation. The simplest sound implementation is the one whose stored state and inequalities you can trace.

#Section 15Where the estimate is wrong

Place all 80 previous-bucket events near that bucket’s end. At 15 seconds into the new bucket, those events can still belong to the true trailing interval. With 30 current events, true recent count is 110 while the interpolation gives 90. The estimate undercounts by 20.

Place older events near the previous bucket’s beginning instead, and many may already be expired while the estimate retains a weighted share. That overcount rejects work the exact log could admit. No choice of fractional arithmetic recovers timestamp placement that the counters never stored.

THE SAME 80 / 30 STATE CAN HIDE VERY DIFFERENT HISTORIESTHE SAME 80 / 30 STATE CAN HIDE VERY DIFFERENT HISTORIESprevious events latetrue count 110,estimate 90: undercountprevious events earlymost already expired,estimate 90: overcountillustrative placements; two counters cannot see where events fell
Figure 12.16Illustrative 80/30 state at 15 seconds. Event placement determines error that the two counters cannot observe.

If a contract requires a strict upper bound in every trailing interval, use exact history or a conservative algorithm designed for that bound. If approximate fairness and cheap smooth limiting are acceptable, document the counter error. Avoid claiming that every sliding-window algorithm has identical semantics; the name covers several different constructions.

#Section 16Rollover, idle gaps, and alternatives

The next request arrives after the current bucket changes. If it advances by one bucket, move the former current count into previous state and start the new current count. If more than one entire bucket has passed, old counters no longer contribute and can be cleared. Store the bucket identity so this distinction is explicit.

SAME BUCKET: COUNT. NEXT BUCKET: SHIFT. LONG GAP: CLEAR BOTHSAME BUCKET: COUNT. NEXT BUCKET: SHIFT. LONG GAP: CLEAR BOTHsame bucketprev 80now 30 → 31next bucketprev 31now 1idle 3 bucketsprev 0now 1store the bucket number with the count, or an old count looks current
Figure 12.17Counter state transition. Time units and rollover arithmetic must be consistent at every gateway.

A ring of smaller time buckets can improve resolution at a cost in state and update work. It still groups arrivals inside each subwindow, so endpoint exactness depends on the chosen construction. Compare the guarantee, state memory, admission cost, and boundary error rather than treating algorithm names as a ranking.

For Heron, the strict user promise can choose a sliding log, while a broad low-cost abuse rule may choose the counter approximation. Different limits can deliberately use different semantics if the user-facing policy and operational rules say so.