npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

redis-sliding-window-limiter

v1.0.1

Published

Redis-based sliding window rate limiter for Node.js using Lua scripts

Readme

Redis Sliding Window Rate Limiter

Redis-based sliding window rate limiter using Lua for atomic execution.

GitHub npm

Features

  • Sliding window algorithm - More accurate than fixed window approach
  • Redis-backed - Works across multiple instances
  • Lua scripting - Atomic operations with no race conditions
  • Express middleware support - Easy integration with Express.js
  • Flexible client options - Supports both internal and external Redis clients

Installation

npm install redis-sliding-window-limiter

Basic Usage

import { createRateLimiter } from "redis-sliding-window-limiter";

const limiter = createRateLimiter({
  redisUrl: "redis://localhost:6379",
  windowSize: 10,
  maxRequests: 3,
});

Express Middleware

Basic Usage

import express from "express";
import { createRateLimiter, createExpressMiddleware } from "redis-sliding-window-limiter";

const app = express();
prefix
const limiter = createRateLimiter({
  redisUrl: "redis://localhost:6379",
  windowSize: 10,
  maxRequests: 3,
});

app.use(createExpressMiddleware(limiter));

app.get("/", (req, res) => {
  res.json({ message: "OK" });
});

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

Advanced Usage with Options

import express from "express";
import { createRateLimiter, createExpressMiddleware } from "redis-sliding-window-limiter";

const app = express();

const limiter = createRateLimiter({
  redisUrl: "redis://localhost:6379",
  windowSize: 10,
  maxRequests: 3,
});

const rateLimiter = createExpressMiddleware(limiter, {
  keyGenerator: (req) => req.user?.id || req.ip,
  message: "Too many requests. Please try again later.",
});

app.use(rateLimiter);

app.get("/", (req, res) => {
  res.json({ message: "OK" });
});

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

Middleware Options

| Option | Type | Description | |--------|------|-------------| | keyGenerator | function | Custom function to generate rate limit key (default: uses IP address) | | message | string | Custom error message when rate limit is exceeded |

Key Generator Examples:

  • (req) => req.user?.id - Rate limit per authenticated user
  • (req) => req.ip - Rate limit per IP address (default)
  • (req) => req.user?.id || req.ip - Use user ID if authenticated, otherwise IP
  • (req) => req.headers['x-api-key'] - Rate limit per API key

Using an External Redis Client

import Redis from "ioredis";
import { createRateLimiter } from "redis-sliding-window-limiter";

const redis = new Redis("redis://localhost:6379");

const limiter = createRateLimiter({
  redisClient: redis,
  windowSize: 10,
  maxRequests: 3,
});

Response Format

Allowed Request

{
  "allowed": true,
  "remaining": 2,
  "resetTime": 1776260236828,
  "limit": 3
}

Rate Limit Exceeded

{
  "message": "Too many requests",
  "allowed": false,
  "remaining": 0,
  "resetTime": 1776260235374,
  "limit": 3
}

Configuration Options

| Option | Type | Description | Default | |--------|------|-------------|---------| | redisUrl | string | Redis connection string | - | | redisClient | Redis | External Redis client instance | - | | windowSize | number | Time window in seconds | 60 | | maxRequests | number | Maximum requests per window | 10 | | prefix | string | Redis key prefix | rate_limit: |

Note: Use either redisUrl or redisClient, not both.

How It Works

  1. Timestamp Storage - Each request timestamp is stored in a Redis sorted set
  2. Window Cleanup - Old timestamps are removed using the sliding window logic
  3. Atomic Operations - Lua script ensures atomic execution (no race conditions)
  4. Request Decision - Requests are allowed or blocked based on count within the window

Algorithm Details

The sliding window algorithm works by:

  • Maintaining a sorted set of request timestamps for each client
  • Removing timestamps older than the current time minus the window size
  • Counting remaining timestamps in the window
  • Allowing or denying based on the configured limit

This approach is more accurate than fixed-window counters as it prevents the "boundary spike" problem where multiple requests at window edges can exceed the intended rate.

Links

License

MIT

Author

Dhiraj Barnwal