Registry / security / async-redis-rate-limiter

async-redis-rate-limiter

JSON →
library1.0.4jsnpmunverified

Fixed-window rate limiter for Node.js using a centralized Redis instance. Current stable version is 1.0.4 (released March 2022), with infrequent updates. It implements a fixed-window algorithm with millisecond precision, using only O(1) Redis operations for high performance (tested up to ~5.7M actions/minute locally). Unlike rolling-window limiters, it resets counters at window boundaries, which can allow bursts. Provides TypeScript types and promises-based API built on the 'redis' package v4+. Minimal dependencies (only @types/redis for TypeScript). Suitable for distributed systems (FaaS, microservices) requiring a centralized counter.

npm install async-redis-rate-limiter
INSTALL
IMPORT
SIG · ASYNC-REDIS-RATE-L
A
async-redis-rate-limiter
securityjavascriptv1.0.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

FixedWindowRateLimit
import { FixedWindowRateLimit } from 'async-redis-rate-limiter'
const FixedWindowRateLimit = require('async-redis-rate-limiter').FixedWindowRateLimit
ESM imports are supported; CommonJS require works but must destructure correctly.
incrementFixedWindowCounter
import { incrementFixedWindowCounter } from 'async-redis-rate-limiter'
const incrementFixedWindowCounter = require('async-redis-rate-limiter')
Do not default-import; the module exports named members.
FixedWindowRateLimit (type)
import type { FixedWindowRateLimit } from 'async-redis-rate-limiter'
import { FixedWindowRateLimit } from 'async-redis-rate-limiter' when only used as type
For TypeScript, if used only as type, use 'import type' to avoid runtime overhead.

Shows how to import and use the fixed-window rate limiter with a Redis client.

import { FixedWindowRateLimit, incrementFixedWindowCounter } from 'async-redis-rate-limiter'; import { createClient } from 'redis'; const redisClient = createClient({ url: process.env.REDIS_URL ?? 'redis://localhost:6379' }); await redisClient.connect(); const actionId = `action-user-${'userId'}`; const limit = new FixedWindowRateLimit(actionId, 10, 3 * 60 * 1000); const response = await incrementFixedWindowCounter(redisClient, limit); if (response.isOverLimit) { console.log(`Rate limited. Counter: ${response.newValue}, remaining time: ${response.remainingTime}ms`); } else { // Perform action } await redisClient.disconnect();
Debug
Known issues
gotchaFixed-window algorithm allows bursts at window boundaries. Not a rolling window.
fix
Use a rolling-window limiter if burst behavior is unacceptable.
affects: >=0.0.0
deprecatedThe 'redis' client constructor changed in v4; v3 style createClient() with legacy options is deprecated.
fix
Use `redis.createClient({ url: 'redis://...' })` and call `.connect()`.
affects: >=4.0.0
gotchaRedis client must be connected before calling incrementFixedWindowCounter.
fix
Always await `redisClient.connect()` before rate-limiting.
affects: >=0.0.0
Errors
Common errors & fixes
ERR wrong number of arguments for 'EVAL' command
Improperly formatted Lua script arguments due to incorrect parameters
fix
Check that the Redis client is connected and that the FixedWindowRateLimit object has valid fields.
TypeError: redisClient.sendCommand is not a function
Using an older version of the 'redis' package (v3) with this library which expects v4+
fix
Upgrade to 'redis' v4+ and use createClient() and connect().
Upgrade
Version history
1.0.4latest on npm
Audit
Dependencies
redisrequiredPeer dependency; used to create a Redis client for counter storage
@types/redisoptionalTypeScript type definitions for redis (included as dependency for TS users)
Agent activity
28 hits · last 30 days
node
26
OpenAI (training)
1
Resources
async-redis-rate-limiter — npm install async-redis-rate-limiter · libregistry