Girish Maity

Work / Case study

A rate limiter that stays exact across instances

Three algorithms as atomic Redis Lua scripts on Redis server time, with tenant and route policies synced across instances and a chosen behaviour for when Redis is down.

  • Node.js
  • TypeScript
  • Redis
  • Lua
  • Express

A limit of 100 requests a minute is easy to enforce on one server. Run three copies behind a load balancer and each one counts only what it sees, so the real limit becomes 300. Share a counter in Redis and a new problem appears: two instances can both read "99", both decide there is room, and both let a request through.

The limiter also needs different rules for different tenants and routes, rules that can change while it runs, and a deliberate answer to what happens when Redis is unreachable.

Schematic Requests are spread over three instances. Each instance asks Redis for a decision by running one Lua script that checks and updates the counter in a single step, so the instances together admit exactly the limit. Policy changes reach running instances over pub/sub.

One atomic script per decision

Each algorithm's check-and-update runs as a single Redis Lua script, so no other request can run between the read and the write. Two instances can never both take the last free slot. Scripts are loaded once and called by their hash, and reloaded automatically if Redis forgets them.

Redis's clock, not the instance's

The sliding-window and token-bucket scripts read the time from Redis itself instead of trusting each instance's clock. If one server's clock drifts, windows don't stretch or shrink.

Three algorithms, chosen per policy

  • Fixed window counts per interval. It is the cheapest, but allows a burst across a window boundary. The rate-limiting lab shows exactly that.
  • Sliding window counts over a rolling period. It is fairer, and costs more Redis work.
  • Token bucket refills over time and allows short bursts up to its capacity.

Policies that change at runtime

A request's policy is chosen by route, method and tenant, with explicit priorities. Admin updates are stored in Redis, so instances started later load them, and published over pub/sub, so running instances apply them immediately.

When Redis is down

The behaviour is a setting, because the right answer depends on the API. Fail open keeps serving without limits. Fail closed rejects requests. Local fallback counts per instance, so the effective limit becomes the limit times the number of instances, with bounded memory.

The integration tests run two limiter engines with separate Redis connections, standing in for two instances, and split concurrent requests between them. With 500 concurrent requests and a limit of 100, exactly 100 were allowed; counters kept per instance would have allowed 200. A burst policy of 5 plus 3 allowed exactly 8.

The cluster benchmark ran 3 instances against one Redis for 15 seconds per algorithm, from one load-generator process with 60 concurrent connections, all on one Windows laptop.

Cluster benchmark: 3 instances, one Redis, limit 1,000
AlgorithmRequestsAllowedFailedThroughputp50 / p99
Sliding window55,6021,00003,707 req/s14.9 / 33.9 ms
Fixed window56,3241,00003,755 req/s14.2 / 36.2 ms
Token bucket52,6231,00003,508 req/s15.1 / 39.2 ms

A re-run the same day held the exact limit again for all three algorithms, over 49,809 to 67,405 requests per run.

The throughput column describes this laptop setup, not the limiter's capacity. One instance alone measured the same rate (3,866 req/s), so the single load-generator process was the bottleneck. The result that matters is correctness: across three instances, the cluster admitted exactly the configured limit.