Case study: the complete limiter
We trace an authenticated request through accounting, rejection, recovery, and verification. The final design exposes the guarantee actually implemented and tests its hardest boundaries. We will reconcile state cost, capacity, abuse protection, and client behavior.
#Section 29The accepted request
A trusted gateway authenticates the user, derives the policy key, and performs coarse endpoint validation before costly work. For the selected strict trailing-minute Heron policy, the Redis script removes expired accepted events, counts current history, and appends a unique admission only if the prospective count remains within 100.
The application then executes under its own deadline and resource limits. Rate admission does not reserve database capacity or establish command idempotency. A state-changing command retains its original effect identity independently of the admission identifier. The policy must say whether a replay recovering an old result consumes a new allowance.
Cleanup, counting, and appending are internal to the atomic limiter decision. The actual network exchange is gateway to limiter and back. Each additional policy dimension and retry belongs in the limiter workload calculation.
#Section 30The rejected request and Retry-After
The selected rule has no remaining allowance. Return a clear rejection without running the expensive application operation. HTTP status 429 identifies excessive request rate; RFC 6585 allows a Retry-After field and requires such responses not to be stored by a cache.
For a token-bucket alternative with 0.5 tokens and cost one, the deficit is 0.5. At refill 100 divided by 60, the modeled refill delay is 0.3 seconds. A protocol field expressed as whole seconds needs deliberate rounding and the current protocol’s syntax; never round advice downward so it claims earlier allowance than the model supplies.
Clients should stop immediate retries, honor useful advice, and add jitter where shared recovery can synchronize them. The server should keep the rejection path cheap, avoid logging each abusive request at an expensive level, and distinguish policy rejection from an internal limiter fault in metrics.
#Section 31Clock, crash, and concurrency tests
Test the last allowance under simultaneous calls. Start a token bucket with one token and no elapsed time; after one serialized cost-one decision, the next must reject. Test clock rollback with retained time 10 and supplied time 9; elapsed refill must be zero and retained time must stay at least 10.
For fixed windows, test the adjacent-bucket burst explicitly. For logs, test equality at the cutoff and equal arrival timestamps with unique members. For counters, test the 80/30 state with late-clustered history and prove that the estimate of 90 can hide true count 110.
Test Redis timeout before execution, timeout after execution with a lost reply, failover to older state, and gateway restart with a leased allocation. An uncertain admission result is not automatically safe to retry with a fresh admission identifier. The chosen accounting can conservatively charge it, recover it, or deliberately tolerate over-admission.
#Section 32Count the complete design
At Heron’s illustrative 1,000 checks per second and 3-millisecond round trip, mean outstanding limiter work is 3 calls. Separate user, tenant, and endpoint checks raise the operation count to 3,000 per second. These values omit retries, protocol overhead, hot-key skew, and the cost of cleanup.
For 100,000 active users at a full 100-event log, simplified retained history is 10,000,000 events and 240,000,000 bytes under the assumed 24-byte event size. The simpler counter model is 3,200,000 bytes at 32 per user, but changes the exactness guarantee. Compare those trade-offs using actual memory and tail-latency measurements before selecting deployment capacity.
This chapter has not implemented a billing ledger or a durable distributed quota protocol. Those need stronger retained evidence if lost allowance can create unacceptable financial effects. The next unit separates media bytes from metadata so admission and upload traffic can protect the application without making it proxy every file.