(003) / 012
DistributedRateLimiter
One shared limit across any number of servers: every decision is a single atomic Lua script in Redis, and the limiter stays up when Redis does not.
- Category
- Infrastructure · Distributed systems
- Year
- 2026
- Last pushed
- Oct 2026
- Primary language
- TypeScript
(01) Overview
A distributed rate limiter for Node.js, backed by Redis. It enforces one shared limit across any number of servers, and it can be used two ways: as a stateless HTTP decision service called from any language, or as a library with Express/Connect middleware embedded in a Node service.
(02) Problem statement
A limiter that lives in one process stops being correct the moment there are two processes. Moving the counter into Redis fixes that, but creates new problems: two replicas racing for the last slot, clocks that disagree between servers, and a hard dependency on Redis being healthy.
(03) Technical approach
- Atomicity. Read, decide and write happen inside one Lua script, which Redis executes atomically.
- One clock. Scripts read the time from Redis, so drift between app servers cannot change a limit.
- Integer arithmetic. Avoids floating-point drift in token counts.
- Degrade, don't fail. Deadlines, a circuit breaker, a per-node fallback store and a local denial cache keep the limiter answering when Redis is slow or down.
(04) System architecture
Replicas are stateless; Redis is the single source of truth. Each replica holds only a short-lived denial cache and a fallback store used during outages.
Stage 01 — POST /v1/check
A client or gateway asks for a decision on { rule, key, cost }.
(05) Implementation details
Four algorithms are implemented, each as a Lua script with a line-for-line TypeScript twin used by the in-memory store:
- Token bucket — smooth refill with bursts; constant memory per key.
- Sliding window — a weighted estimate across two windows; constant memory.
- Sliding log — exact, with memory proportional to the limit; suited to small strict limits.
- Fixed window — the cheapest, with the known edge case at window boundaries.
Scripts are registered through ioredis, which uses EVALSHA and falls back to EVAL after a failover or script flush. Keys for one rule and key share a hash tag so multi-key scripts stay on one Redis Cluster slot. Configuration is a YAML file plus environment overrides, validated with zod.
(06) Main features
- 01Token bucket, sliding window, sliding log and fixed window, each as an atomic Lua script.
- 02Runs as a stateless HTTP decision service or as an embedded library with Express/Connect middleware.
- 03Time is read from the Redis clock, so drift between app servers cannot change limits.
- 04Deadlines, a circuit breaker, a per-node in-memory fallback and a local denial cache.
- 05Hash-tagged keys keep multi-key scripts on one Redis Cluster slot.
- 06Prometheus counters, histograms and gauges fed by limiter hooks.
(07) Technology stack
(08) Engineering challenges
- Proving two implementations agree. A shared contract suite runs against both the memory store and live Redis, so the Lua and TypeScript versions cannot drift apart silently.
- Exact retry times.
retryAfterMsis tested to the millisecond: a request is denied one millisecond before it and admitted at it. - Failure modes as a choice. When Redis is unavailable the limiter can fail open, fail closed, or fall back to the local store; the decision records which path answered.
(09) Evaluation and results
The repository states correctness properties verified by tests. It does not publish throughput or latency benchmarks, so none are claimed here.
| Multi-node race | 100 / 6008 connections race 600 requests at a limit of 100; exactly 100 admitted, for every algorithm |
|---|---|
| retryAfterMs | exactdenied 1 ms before, admitted at it |
| Store contract | sharedsame suite runs against the memory store and live Redis |
Source: README · Why it is correct across nodes
Links
- Repositorygithub.com/harshsuiiii/rate-limiter
Live demoNo hosted demo — run it locally from the repository.