Highlights

Contents

Part III
03

Sliding window log

We keep the recent accepted arrival history to enforce a true moving interval. Exact history removes the reset burst but costs memory and cleanup. We will define the interval endpoints, remove expired entries, preserve unique arrivals, and calculate the state budget.

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 9A log of recent admissions

The policy now means no more than 100 accepted requests in every trailing 60 seconds. A sliding-window log stores the accepted request times that remain inside that moving interval. Each new request first removes expired history, then counts retained admissions before adding itself.

Choose endpoint semantics explicitly. This chapter uses (t−T,t](t-T,t]: an event exactly at t−Tt-T is expired, while an event at current time tt counts. If the retained count is below the limit, add the new accepted event. If it equals the limit, reject without appending to the accepted-work log.

A SLIDING LOG DROPS TIMESTAMPS ≤ t − T, COUNTS THE REST, ADMITS IF UNDER THE LIMITA SLIDING LOG DROPS TIMESTAMPS ≤ t − T, COUNTS THE REST, ADMITS IF UNDER THE LIMITwindow (t − 60, t]: 9 keptnow tillustrative timestamps; append only accepted events, each with a unique member
Figure 12.9Endpoint convention used by this chapter. Accepted-work history differs from a log of every attempted request.

A sorted set is one possible Redis representation because it orders timestamp scores and supports range removal and counting. The representation still needs unique member identities. Timestamp equality must not collapse distinct admitted requests, and client-provided times must not allow callers to erase or extend history.

#Section 10Intermediate log states

Use a deliberately small illustrative arrival trace to see the mechanism, without changing Heron’s policy limit. The log contains accepted events at times 0, 10, 40, and 59 seconds. At time 60 with a 60-second interval, the cutoff is zero.

The event at zero is removed because it lies at the excluded left endpoint. Times 10, 40, and 59 remain, so the retained count is 3. If there is allowance, accepting the current event adds time 60 and raises the count to 4. The history is now [10, 40, 59, 60].

THE LEFT ENDPOINT FALLS OUT OF THE MOVING WINDOW BEFORE ADMISSIONTHE LEFT ENDPOINT FALLS OUT OF THE MOVING WINDOW BEFORE ADMISSION010405960remove t=0admit t=60window at t = 60: (0, 60]retained 10, 40, 59 → append 60 → count 4
Figure 12.10Illustrative times in seconds. The trace shows state transitions rather than replacing the 100-request policy.
THIS CHAPTER USES (t − T, t]: AN EVENT EXACTLY AT t − T HAS EXPIREDTHIS CHAPTER USES (t − T, t]: AN EVENT EXACTLY AT t − T HAS EXPIREDt − T excludedt includedthe cleanup code must use the same inequality: remove timestamp ≤ t − T
Figure 12.11Chosen interval semantics. A different documented endpoint convention must use matching cleanup code.

The whole procedure must be serialized. If gateways independently clean, count, and append, two callers can both see the final slot and append. Exact storage does not imply exact enforcement without the atomic decision boundary.

#Section 11Memory and time costs

A log costs more than a counter because it stores each recent accepted admission. Under an illustrative 100,000 active subjects at their 100-event limit, there are 10,000,000 retained events. Assuming 24 bytes per retained event for a simplified accounting exercise gives 240,000,000 bytes.

This byte assumption excludes real allocator, key, node, member, and replication overhead. It is a lower-detail sizing model, not a Redis memory measurement. The comparable counter exercise assumes 32 bytes per subject, giving 3,200,000 bytes. Exact real costs need the actual representation and workload.

100,000 USERS: A FULL LOG IS 240,000,000 B; A COUNTER IS 3,200,000 B100,000 USERS: A FULL LOG IS 240,000,000 B; A COUNTER IS 3,200,000 Bsliding log: 100,000 × 100 events × 24 B240,000,000 Bcounter: 100,000 × 32 B3,200,000 Bsimplified sizes, no allocator or replication overhead: measure before deploying
Figure 12.12Illustrative byte assumptions exclude implementation overhead. Compare measured memory before deployment.

Redis sorted-set insertion and removal carry representation-dependent costs described by the command documentation. Batch removal of many expired events can make one decision slower than the usual small case. Keep the maximum recent history bounded by the admitted-work limit and test cleanup after an idle period.

#Section 12Rejection time from retained history

The retained count equals the limit, so the next request cannot be admitted. The earliest retained accepted event determines when at least one slot can expire. With the chapter’s endpoint convention, a candidate time at that oldest timestamp plus the interval length makes that event no longer count.

The server can return an advisory retry time based on this state. Concurrent admissions can take the newly available slot before the rejected client returns, so the advice is not a reservation. A client still needs backoff and jitter if many users were rejected together.

THE LOG IS FULL; THE OLDEST ENTRY PLUS 60 S IS THE EARLIEST POSSIBLE NEXT SLOTTHE LOG IS FULL; THE OLDEST ENTRY PLUS 60 S IS THE EARLIEST POSSIBLE NEXT SLOTt=14t=19…t=71t=73oldestRetry-After ≈ 14 + 60 − 73 = 1 sadvice, not a booking: another caller may take that slot firstillustrative timestamps
Figure 12.13Moving-log rejection logic. Another request can consume the future slot first.

Do not expose detailed arrival history to a caller who only needs a limit decision. Return the policy identity, remaining allowance when useful, and a documented delay. Guard the rejection path against excessive serialization or logging because abusive rejected traffic can still overwhelm a limiter.