Lesson 4 of 9 · 27m
Distributed Limiters Without False Precision
Choose central, replicated, edge, and hybrid enforcement with a clear consistency budget instead of pretending every node sees the same counter.
The first distributed limiter most teams build is accidentally N times too permissive, where N is the number of application nodes. Each node enforces 100/minute locally, autoscaling adds nodes, and the customer experience is now defined by your autoscaler. Distributed limiting is not a library choice. It is a consistency, latency, and failure-mode design.
Local enforcement is fast but scales incorrectly
In-memory limiters are excellent for process protection and terrible for global fairness:
10 nodes x 100 requests/minute = 1000 requests/minute effectiveThis may still be correct if the goal is "do not let one node melt." It is wrong if the goal is "tenant receives 100 requests/minute total." Put the control objective in the configuration name or operators will misread it during an incident.
Central state gives shared truth with shared risk
Redis, Postgres, or a dedicated limiting service can coordinate counters globally. The critical operation must be atomic. A Redis Lua script can update bucket state and decide in one round trip:
-- simplified shape, not a complete production script
local tokens = redis.call('GET', KEYS[1])
if not tokens then tokens = tonumber(ARGV[1]) end
if tonumber(tokens) >= 1 then
redis.call('DECR', KEYS[1])
return {1, tokens}
end
return {0, tokens}Real implementations must refill based on time, set expiry, handle missing keys, and avoid trusting the caller's clock. Central truth costs latency and creates a dependency. Decide what happens when that dependency is down before deploying it.
Edge limiting changes both semantics and economics
Enforcing at a CDN or API gateway stops abusive traffic before it reaches origin compute. It also means the edge often knows IP, API key, host, and path but not tenant state or workflow cost. Edge limits are therefore strong for volumetric control and weak for business quotas:
edge broad request floods, auth spraying, malformed traffic
gateway route-level policy, key validation, coarse tenant caps
service weighted operation cost, workflow state, precise quotas
worker concurrency and downstream resource protectionLayered enforcement is not duplication when each layer knows different facts.
Approximation can be a feature
For extremely high-volume traffic, exact global counting may cost more than it protects. Techniques include per-node quotas, gossiped counters, probabilistic data structures, and control planes that periodically rebalance allowances. These designs trade exactness for survivability. Make the error bound explicit: "up to 15% over for thirty seconds" is an engineering policy; silence is a future incident review.
Clocks and time are dependencies
Distributed buckets depend on time. Prefer datastore time (redis.call('TIME')) or a single authoritative time source inside atomic scripts. Application-node clocks drift, especially during VM migrations and container pauses. A negative elapsed duration or impossible refill should be clamped, not allowed to corrupt bucket state.
Design the deployment transition
Rolling deploys routinely run old and new limiter logic together. If key format, cost weights, or fail-open behavior changes, version the state namespace:
rl:v2:bucket:{tenant}:{action}Otherwise one version interprets the other's counter. State-version discipline is unglamorous and saves migrations from becoming quota resets or accidental unlimited windows.
Field notes
- Exact global enforcement for every cheap read request is often wasteful; reserve precision for expensive or security-sensitive actions.
- A central limiter outage is part of the design, not an edge case operators should improvise around.
- Autoscaling changes effective capacity when limits are local; document whether the cap is per node or global.
- Do not use one shared bucket key schema across unrelated actions unless cross-action interference is intentional.
Was this lesson helpful?