Rate limiting / throttling using Redis
Async/await based. For callbacks checkout v1.x.
import slidingWindow from "./lib/slidingwindow.mjs";
import timers from "timers/promises";
for (let count = 0; count < 10; count++) {
let limited = await slidingWindow.check("counter", 10, 2);
if (limited) {
// Don't do anything, wait some amount of time
// and check rate limit again
console.log("Rate limited");
await timers.setTimeout(1000 * 5);
} else {
// Do work!
console.log("Doing work");
}
}
process.exit(0);
$ npm install redis-ratelimit
- Provides a distributed rate limit per key
- Sliding window uses redis sorted sets for high performance
- Fixed window using simple counter with expiration
Optionally configures the underlying redis instance used by the rate limiter.
Arguments
port
- Port redis is listening onhost
- Host redis is onoptions
- Options hash passed through to underlying redis createClient() call
A fixed window rate limiter. Rate limit is applied for duration of window. Reset at window expiration. No more requests are allowed until window expires.
Checks if the specified key is over or under the rate limit. If over rate limit the call to check is not counted against the rate limit
Arguments
key
- Unique key the rate limit is associated withwindowInSeconds
- The length of the window during which the calls are rate limited. This is a bucket not a sliding window. The underlying redis key expires after this duration.limit
- Number of calls that are allowed before the rate limit kicks in
Returns
- bool - Indicates if the rate limit has kicked in or not. If an error occurred limited will be set to true. If limited returns true.
Returns the number of calls that are currently counting against the rate limit. Note this number can be greater than the limit as each call to check increments the counter.
Arguments
key
- Unique key the rate limit is associated with
Returns
- int - The count of calls counting against the rate limit.
A fixed time window based rate limiter. Allows a fixed number of requests per time window. i.e. 10 requests per minute
Checks if the specified key is over or under the rate limit for the specified time window.
Arguments
keyPrefix
- Prefix for a unique key the rate limit is associated withwindowUnit
- The time unit for the rate limiter. Valid units:- second
- minute
- hour
- week
- isoWeek
- month
- quarter
- year
This is a bucket not a sliding window. The underlying redis key expires after the duration of the windowUnit.
limit
- Number of calls that are allowed before the rate limit kicks in
Returns
- bool - True if the rate limit has kicked in or not. If an error occurred true will be returned.
Returns the number of calls that are currently counting against the rate limit. Note this number can be greater than the limit as each call to check increments the counter.
Arguments
key
- Unique key the rate limit is associated with
Returns
- int - The count of calls counting against the rate limit.
A sliding window rate limiter. Individual requests are expired out based on request time. Once oldest request expires a new request will be allowed to occur.
Checks if the specified key is over or under the rate limit. If over rate limit the call to check is not counted against the rate limit
Arguments
key
- Unique key the rate limit is associated withwindowInSeconds
- The length of the window during which the calls are rate limited. This is not a bucket but a sliding windowlimit
- Number of calls that are allowed before the rate limit kicks in
Returns
- bool - True if the rate limit has kicked in or not. If an error occurred return will be true. If true is returned the call to check() does not count against the rate limit.
Returns the number of calls that are currently counting against the rate limit.
Arguments
key
- Unique key the rate limit is associated with
Returns
- int - The count of calls counting against the rate limit.
The author is Chris Kinsman