Highlights

Contents

Part I
01

Rate limiting requirements

We decide whose requests count, what they cost, and which interval matters. A limit described only as requests per minute leaves several different algorithms possible. We will define Heron’s user policy, distinguish rate from concurrency, and choose the enforcement boundary.

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 1A hundred requests per user per minute

A viewer reloads a score repeatedly while another viewer sends an automated scraper. Heron wants to limit each authenticated user to 100 requests per minute, but the phrase alone does not say whether a burst is allowed or how the minute moves. A rate limit is an admission rule that restricts work over time for a named identity or resource.

Make the contract explicit. Does the limit mean at most 100 accepted operations in every trailing 60 seconds? Does it mean 100 in each wall-clock bucket? Or does it mean a sustained refill rate with room for a burst? These policies answer the same words differently at interval boundaries. The rest of the chapter builds each policy rather than hiding that choice.

The illustrative sustained rate is 100 divided by 60, or 1.666666 recurring requests per second. It is an average replenishment rate; it does not imply fractional requests can be admitted. Algorithms can keep fractional accounting internally while each ordinary request costs one full unit.

"100 PER MINUTE" CAN MEAN A MOVING 60 S WINDOW, OR A BUCKET REFILLING AT 100/60 PER SECOND"100 PER MINUTE" CAN MEAN A MOVING 60 S WINDOW, OR A BUCKET REFILLING AT 100/60 PER SECONDtrailing-window limit015304560 sany 60 s: ≤ 100burst-capable raterefill 1.666666 tokens/scapacity chosen separatelypick the guarantee before the algorithm
Figure 12.1Illustrative Heron policy alternatives. Select the intended admission guarantee before the algorithm.

#Section 2The identity comes from a trusted boundary

A request claims to come from user u7. If the limiter trusts an arbitrary request header, the client can change that value and gain a new allowance. A limiting identity is the validated subject whose work the policy counts. Authentication or a trusted gateway must establish it before a user-scoped rule can be meaningful.

For Heron, scope a key by tenant, authenticated user, operation class, and policy version where those dimensions matter. An IP-address rule can protect a pre-authentication endpoint, but many people may share an address and attackers may distribute requests across addresses. It is a coarse abuse control, not a perfect user identity.

THE LIMITER KEY IS BUILT FROM TRUSTED IDENTITY, NEVER FROM A FIELD THE CLIENT TYPEDTHE LIMITER KEY IS BUILT FROM TRUSTED IDENTITY, NEVER FROM A FIELD THE CLIENT TYPEDX-User: anyoneauthrl:tenant9:user41:upload:v3tenant · user · operation · policy versiona client-chosen key would mint itself a fresh allowance on every request
Figure 12.2Identity construction. A user-supplied field cannot mint its own fresh trusted allowance.

Validate the length and allowed shape of key dimensions. Otherwise an attacker can create unbounded state even when every new key admits little work. Expiration helps reclaim inactive state, but it does not prevent a fast cardinality attack. Bound anonymous identities and separate their policy from authenticated users.

#Section 3Rate, concurrency, and quota are different

A user makes few requests, but each launches a long clip conversion. A rate policy can admit that user while the conversions fill every worker. A concurrency limit bounds work currently in flight. A quota bounds a cumulative allocation over a stated accounting period or resource total.

These controls can coexist. Heron admits API calls by a user rate policy, bounds active conversions separately, and may cap retained upload bytes. A successful rate decision does not establish that a downstream worker is available. Perform admission near the resource it protects as well as at the external policy boundary.

RATE COUNTS ARRIVALS OVER TIME, CONCURRENCY COUNTS WORK IN FLIGHT, QUOTA COUNTS A TOTALRATE COUNTS ARRIVALS OVER TIME, CONCURRENCY COUNTS WORK IN FLIGHT, QUOTA COUNTS A TOTALrateaccepted cost over timeconcurrencywork in flight nowquota6.4 of 10 GBcumulative allocationadmitted by the rate limiter does not mean a worker slot is free
Figure 12.3Three distinct accounting rules. They can apply to one request without being interchangeable.

At Heron’s illustrative peak, 1,000 requests per second each mean 0.2 seconds of work imply 200 mean requests in flight. A user policy of 100 per minute cannot establish that fleet-wide concurrency fits the available pool. The performance and reliability units explain how waiting can increase that pool demand even when rates do not change.

#Section 4Where enforcement belongs

The database should not spend its expensive query work discovering that the caller exceeded a cheap gateway rule. Put early coarse protection before costly parsing or downstream calls, then perform trusted user admission after authentication. Protect internal resources with their own bounded concurrency and workload rules.

The gateway needs a prompt outcome from the limiter. Under an illustrative 3-millisecond limiter round trip and 1,000 requests per second, the mean limiter calls in flight are 3. That is an arithmetic workload relation, not a Redis benchmark. Serialization, network tail delay, and hot keys can dominate real behavior.

A COARSE EDGE RULE BY IP, THEN AUTH, THEN THE USER POLICY, THEN THE WORKA COARSE EDGE RULE BY IP, THEN AUTH, THEN THE USER POLICY, THEN THE WORKedge ruleper IP, coarseauthenticatewho is it?user admissionper user policyapplicationexpensive effect1,000 checks/s × 3 ms limiter round trip = 3 calls in flight on average
Figure 12.4Enforcement path. Internal capacity controls can still reject admitted work when a protected resource is full.

If the limiter is unavailable, the decision is part of the service contract. Public score reads may use a bounded local fallback. Sensitive write operations may reject when their required shared admission state cannot be checked. Neither choice should occur accidentally because a timeout handler forgot to return a result.