UsefulKey

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 "betterkey";

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

OptionTypeDefaultDescription
keyPrefixstring"usefulkey:rl"Namespace prefix for counters and token buckets.

Client compatibility

  • Fixed window prefers eval; falls back to incr + pexpire.
  • Token bucket prefers eval; falls back to get/set.

Keys

  • Fixed window: "<keyPrefix>:<namespace>:<identifier>" with TTL
  • Token bucket: same key storing "<tokens>:<lastRefillMs>"