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 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-redis-rate-limiter

v1.0.4

Published

Rate-limiter in Node.js, powered by Redis.

Downloads

344

Readme

async-redis-rate-limiter

Package Version Dependencies Requires Node

Tests Test Coverage Maintainability Score

Rate limit any action using a centralized Redis instance.

async-redis-rate-limiter is intended to be used when your servers are distributed across multiple systems or nodes (such as in Function-as-a-Service case) and you want to use a centralized location (the Redis instance) to store request counts.

  • Fixed-window algorithm
  • Window with milliseconds precision
  • Performance-oriented (uses the lowest number of operations possible, and only O(1) operations)
  • Tested with a throughput up to ≈ 5.7M actions per minute, using a local Redis instance
  • No race conditions

The fixed window algorithm counts all the actions performed during the window. When the window ends the counter is restarted from zero. The window will start when the first action arrives.

This is not a rolling window! For example, if you specify 100 requests / hour, a user would be able to execute 1 request at 00:00, 99 requests at 00:59 and then another 100 requests at 01:00, resulting in 199 requests made in few minutes. If you want to avoid this situation use a rolling-window algorithm (but consider that a fixed-window algorithm is simpler and faster).

Quickstart

To use async-redis-rate-limiter with the fixed window counter...

1. Install

npm install async-redis-rate-limiter
npm install redis

2. Use

Example usage:

// Get dependencies.
const { FixedWindowRateLimit, incrementFixedWindowCounter } = require("async-redis-rate-limiter");
const redis = require("redis");

// Initialize Redis client.
const redisHost = "localhost";
const redisPort = 6379;
const redisClient = redis.createClient({ url: `redis://${redisHost}:${redisPort}` });
await redisClient.connect();

// Define the action id that will be used for the counter,
// by using the same action id you're incrementing the same counter.
// A few good examples of data that can be used in the action id are: an IP address, a user identifier, an API key.
const userId = "userId";
const actionId = `action-name-${userId}`;

// Increment counter.
// In this example: max 10 actions, window of 3 minutes.
const response = await incrementFixedWindowCounter(redisClient, new FixedWindowRateLimit(actionId, 10, 3*60*1000));

// Check if over the limit.
if (response.isOverLimit) {
  console.warn("User reached the rate limit for the action."
    + `The new value of the counter is ${response.newValue},`
    + `the window will close in ${response.remainingTime} milliseconds.`);
} else {
  // Perform action.
  // This section can be executed at most 10 times in the window of 3 minutes (given the same actionId).
}

Methods

incrementFixedWindowCounter(redisClient, rateLimit)

Increments the value in the current window. Returns an object with the information about the fixed-window counter.

Arguments

Returns

Promise: A Promise containing the information about the updated fixed-window counter e.g.:

{
  newValue: 1, // Updated value of the counter.
  remainingTime: 180000, // Milliseconds till the end of the window.
  isOverLimit: false, // Boolean indicating if the counter is over the limit.
}

Classes

FixedWindowRateLimit(actionId, limit, window)

Constructs a new instance of the FixedWindowRateLimit class.

Arguments

  • actionId: A string representing the action to be rate-limited (same string = same counter). For security reasons it is recommended to avoid using a variable inputted by the user (if a user-input is used as actionId, a malicious user could modify other counters).
  • limit: An integer number specifying the limit for the rate limiter, for example a limit equal to 10 means that the action can be performed at most 10 times during the fixed-window.
  • window: An integer number specifying the duration of the window in milliseconds.

Example: maximum 10 visualization per user every 3 minutes:

new FixedWindowRateLimit(`view-${userId}`, 10, 3*60*1000);

Returns

FixedWindowRateLimit: A new instance of the FixedWindowRateLimit class.

License

Apache Version 2.0

See LICENSE