Highlights

Contents

Part II
02

Fixed window counter

We count requests inside explicit clock buckets. The simple state makes this algorithm cheap, but resetting a counter can admit a boundary burst. We will trace bucket selection, rejected attempts, reset behavior, and atomic expiration.

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 5Fixed-window buckets

A request arrives just before the minute changes. A fixed window places time into nonoverlapping intervals and counts work in the selected interval. Each identity has a counter for a bucket rather than a history of all recent arrivals.

w(t)=⌊t/T⌋.w(t)=\left\lfloor t/T\right\rfloor.

Read this as bucket identity ww is time tt divided by window length TT, rounded down. With a 60-second window, time 60 selects bucket 1 when the illustrative epoch begins at zero. Bucket identifiers are accounting coordinates, not local calendar labels.

The limiter derives the subject key and bucket, atomically increments or conditionally admits against that bucket, then sets cleanup past the bucket's useful lifetime. Every gateway that checks the same rule must choose compatible time and key conventions. A per-process counter is not a shared user limit.

A FIXED WINDOW KEYS THE COUNTER BY MINUTE; AT 60 S A FRESH KEY STARTS AT ZEROA FIXED WINDOW KEYS THE COUNTER BY MINUTE; AT 60 S A FRESH KEY STARTS AT ZERO0306090120 srl:u41:bucket0 = 100rl:u41:bucket1 = 0resetbucket = floor(time ÷ 60); every gateway must use the same rule
Figure 12.5Illustrative epoch and 60-second windows. The reset is the algorithm's stated semantics.

#Section 6The boundary burst

Suppose the user sends 100 accepted requests at illustrative time 59.9 seconds and another 100 at 60.1. Each bucket obeys its own limit, yet 200 requests occur inside a 0.2-second span. Dividing 200 by 0.2 gives a 1,000-request-per-second average over that small span.

This does not violate the fixed-window contract. It violates a different contract that promised at most 100 in every trailing minute. The distinction matters when a downstream system cannot absorb the permitted boundary burst. A separate burst or concurrency control can protect it, or the policy can choose a moving-window algorithm.

100 AT 59.9 S AND 100 AT 60.1 S: BOTH BUCKETS OBEY, YET 200 PASS IN 0.2 S100 AT 59.9 S AND 100 AT 60.1 S: BOTH BUCKETS OBEY, YET 200 PASS IN 0.2 S0306090120 s200 in 0.2 s = 1,000/sfine for a fixed-window contract; a trailing-window promise is broken
Figure 12.6Illustrative arrival times. Both fixed buckets obey 100, while a trailing-window rule would reject part of the second burst.

Different users can synchronize near a reset even without coordinated abuse. A client that displays the reset clock encourages this pattern. Jittering client retries can spread demand, but it does not change the policy's admitted upper bound. State the burst tolerance in the service design.

#Section 7What counts when admission fails?

The fixed counter is 100. The next request is rejected. Should it leave the counter at 100 or increment it to 101? An increment-first policy counts attempts and allows only values up to the limit; a conditional admission policy can count accepted work only. Both are possible, but their exposed remaining allowance differs.

For Heron, write that choice into the policy. If every attempted request consumes state, the counter may grow while the service keeps rejecting. If only accepted work counts, the atomic operation reads the count, checks the remaining allowance, and increments only on success. Malformed or unauthorized requests may need separate abuse accounting.

COUNT EVERY ATTEMPT, OR ONLY ADMITTED WORK: THE COUNTER MEANS DIFFERENT THINGSCOUNT EVERY ATTEMPT, OR ONLY ADMITTED WORK: THE COUNTER MEANS DIFFERENT THINGScount attempts103INCR every request, reject if > 100rejected retries keep the count highcount admissions100check, then INCR only if allowedone atomic scriptwrite the choice into the policy
Figure 12.7Two valid accounting contracts. A retry and an invalid request need explicit treatment under either policy.

Do not refund work merely because the application returns an error after consuming resources. Refunding can let expensive failing requests repeat without cost. A reservation-and-reconciliation design is possible for variable-cost work, but it needs operation identities and atomic correction rules to avoid negative or duplicated accounting.

#Section 8Atomic increment and expiration

A gateway increments a new counter, then crashes before setting its expiration. The key may remain indefinitely. Atomicity of an individual INCR does not make an entire increment-and-expire procedure atomic. Redis's counter documentation describes this cleanup race.

Combine the necessary state change and expiration decision inside one script or an appropriate atomic transaction. For accepted-only counting, combine the read, comparison, increment, and expiration rule. Expire based on the bucket's remaining useful lifetime plus any deliberate cleanup margin rather than extending the policy interval on every request.

INCR, THEN CRASH BEFORE EXPIRE: THE KEY LIVES FOREVERINCR, THEN CRASH BEFORE EXPIRE: THE KEY LIVES FOREVERgatewayRedisnext gateway1 INCR rl:u412 EXPIRE (never sent)finds stale keyfix: one Lua script does INCR and EXPIRE together
Figure 12.8Crash gap between separate commands. A script can apply the counter and cleanup rule in one serialized operation.

The script must remain bounded and validate its inputs before mutating state. An atomic script does not promise rollback of earlier mutations after every possible runtime error. Nor does it make asynchronous replicas current. The distributed parts return to these separate guarantees.