Redis Store for Express Rate Limit

4.3.1 · active · verified Wed Apr 22

rate-limit-redis is a Redis-backed storage engine designed for the `express-rate-limit` middleware, enabling distributed rate limiting across multiple application instances. It is currently at stable version 4.3.1, with frequent minor and patch releases, as indicated by the recent changelog entries, reflecting an active maintenance schedule. This library supports popular Redis clients such as `node-redis` and `ioredis`, and also explicitly lists compatibility with `redict` and `valkey`, offering flexibility in deployment. A key differentiator is its flexible `sendCommand` abstraction, which allows seamless integration with various Redis client libraries by adapting their specific command execution functions. It requires Node.js 16 or above and Redis 2.6.12 or above for operation. The project maintains an active development status, ensuring compatibility with the latest `express-rate-limit` versions and modern Node.js environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up an Express application with rate limiting using Redis as a store, configured with the node-redis client. It connects to Redis and applies the rate limiter to all routes.

import { rateLimit } from 'express-rate-limit';
import { RedisStore } from 'rate-limit-redis';
import { createClient } from 'redis';
import express from 'express';

const app = express();

async function setupRateLimiter() {
  // Create a `node-redis` client
  const client = createClient({
    url: process.env.REDIS_URL ?? 'redis://localhost:6379'
  });

  // Then connect to the Redis server
  client.on('error', (err) => console.error('Redis Client Error', err));
  await client.connect();
  console.log('Connected to Redis');

  // Create and use the rate limiter
  const limiter = rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100, // Limit each IP to 100 requests per window
    standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
    legacyHeaders: false, // Disable the `X-RateLimit-*` headers

    // Redis store configuration
    store: new RedisStore({
      sendCommand: (...args: string[]) => client.sendCommand(args),
    }),
  });

  app.use(limiter);

  app.get('/', (req, res) => {
    res.send('Hello, you are rate-limited!');
  });

  const PORT = process.env.PORT || 3000;
  app.listen(PORT, () => {
    console.log(`Server running on http://localhost:${PORT}`);
  });
}

setupRateLimiter().catch(console.error);

view raw JSON →