Distributed limiting with Redis
We make many gateways share one admission decision. Atomic arithmetic prevents local races, but failover and partitions still affect the recovered allowance. We will trace double spending, write a bounded script, partition policy keys, and plan limiter outages.
#Section 25The read-check-write race
The shared count is 99 under a 100-request policy. Gateway A reads 99, then gateway B reads 99. Both conclude that the final slot is free. If both increment after their separate decisions, the count becomes 101 and both may already have admitted work.
An atomic operation is observed as one indivisible state transition for the relevant concurrent callers. The whole read-check-update must be atomic, not just its final increment. Redis scripting serializes a script against other commands on that server, allowing one caller to see the state left by the other.
With accepted-only fixed-window accounting, A’s atomic operation sees 99 and stores 100. B’s then sees 100 and rejects without mutation. In a token bucket, the same order refills and spends a final token once. Local locking inside one gateway does not serialize another gateway’s calls.
#Section 26The atomic procedure and its limits
A bounded token-bucket script reads balance and last time, derives nonnegative elapsed time, refills under the cap, checks validated cost, and stores the resulting state and cleanup. It returns allow or deny with the remaining balance and advisory wait. No network call or unbounded scan belongs inside that procedure.
Atomic execution blocks competing commands while it runs, so long scripts can harm unrelated keys on the same server. Validate positive refill, capacity, cost, and time units. Redis script atomicity does not generally mean earlier writes are rolled back if a later command fails; preflight the bounded operation so the mutation sequence has no expected partial-error path.
Here is the bounded decision in pseudocode. The state load and store occur inside the same Redis script; clock() denotes the chosen authoritative time source, not an untrusted request timestamp. Validate all policy inputs and the expected stored types before the first mutation. The returned delay is advisory, while the update is one serialized decision.
validate_positive(capacity, refill_rate, request_cost)
balance, previous = load_checked_state(key, capacity)
now = max(previous, clock())
refilled = min(capacity, balance + (now - previous) * refill_rate)
allowed = refilled >= request_cost
remaining = refilled - request_cost if allowed else refilled
wait = 0 if allowed else (request_cost - refilled) / refill_rate
store_state(key, remaining, now)
expire_after(key, capacity / refill_rate)
return allowed, remaining, waitWith capacity 100 and refill 100 divided by 60, a completely empty bucket refills in 60 seconds. Expiring no earlier than that full horizon after the latest retained decision conservatively ensures that recreating a full bucket does not invent allowance. A balance of 15 would actually reach full after 51 seconds, but using the full horizon avoids a balance-dependent expiration rule. Round the stored lifetime upward to the API’s supported unit and keep state mutation and expiration together.
The script’s state should expire only after forgotten allowance is safe to recreate. A full idle token bucket can be recreated after its refill horizon, while a recent partially spent one cannot be discarded immediately without granting extra credit.
#Section 27Shards and multiple dimensions
At 1,000 admitted-policy checks per second, checking user, tenant, and endpoint rules separately can produce 3,000 limiter operations per second before retries. Combining related decisions can reduce round trips, but all required keys need a compatible atomic ownership boundary.
Redis Cluster maps keys to slots. Multi-key atomic operations require suitable key placement; hash tags can deliberately colocate related keys. The cluster specification explains the slot and replication model. A global tenant key can become a hot key even while user keys distribute well.
If budgets live on different shards, an atomic all-or-nothing debit is no longer supplied by a single local script. Choose deliberate partial charging, a reservation protocol, conservative allocation, or another authority. Do not pretend that compensating a failed second debit cannot itself race or fail.
#Section 28Unavailable limiter and partition choices
The Redis call times out. Fail open admits despite inability to verify the normal rule. Fail closed rejects because the normal decision is unavailable. Neither is universally correct: public read availability, abuse exposure, hard quota integrity, and protected resource capacity lead to different choices.
A bounded local fallback is an explicit third policy. Four gateways each using an independent 100-request allowance can admit 400 in aggregate, so it cannot preserve the original shared 100 limit. Dividing the allowance into 25 each bounds aggregate admission under the stated fixed membership, but uneven demand can strand unused allowance.
If each gateway holds 20 unused leased tokens when it disappears, four lost allocations can strand 80 tokens. Returning them safely requires knowing they cannot still be spent. This is the same ownership problem as recovering work under leases, now applied to admission credit.