Redis
Redis-based rate limiting for distributed systems.
Overview
- Redis-based rate limit store for distributed environments.
- Uses keys per
(namespace, identifier)for fixed windows and a token bucket.
Usage
import { createClient } from "redis";
import { RedisRateLimitStore, usefulkey } from "usefulkey";
const client = createClient({ url: process.env.REDIS_URL });
await client.connect();
const rateLimitStore = new RedisRateLimitStore(client, { keyPrefix: "usefulkey:rl" });
const uk = usefulkey({ adapters: { rateLimitStore } });Options
| Option | Type | Default | Description |
|---|---|---|---|
keyPrefix | string | "usefulkey:rl" | Namespace prefix for counters and token buckets. |
Client compatibility
-
This has been tested with
redisio. -
This adapter automatically adapts
redisioClient instances for proper TypeScript compatibility. -
You are welcome to submit PRs for other Redis clients. See contribution guide for more information.
-
Other clients may work but have not been tested.
-
Fixed window prefers
eval; falls back toincr+pexpire. -
Token bucket prefers
eval; falls back toget/set.
Keys
- Fixed window:
"<keyPrefix>:<namespace>:<identifier>"with an expiration time - Token bucket: same key storing
"<tokens>:<lastRefillMs>"