Rate Limiter Redis is a JavaScript library that provides different algorithms to rate limit your requests using Redis as shared state between the distributed services.
This library is useful for controlling the rate of requests from your application to a third-party service or API.
-
Cost-Effective: Instead calling HTTP APIs and handling execptions might throw from the Third-party API, you can block the request before it reaches the API, or retry the request later.
-
Distributed: Rate Limiter Redis is designed to work in a distributed environment, where multiple instances of your application can share the same rate limit.
To install the library, use npm:
npm i -P @fsilva3/rate-limiter-redis
Here is a few examples of how to use Rate Limiter Redis:
- Token Bucket Instance
import { TokenBucket, TokenBucketSettings } from ('@fsilva3/rate-limiter-redis')
const second = 1000
// 60 tokens (requests) per minute
const tbSettings: TokenBucketSettings = {
capacity: 60,
interval: (60*second),
key: 'my-rate-limiter-bucket' // optional key param to identify the bucket, otherwise it will use the default key
}
// Create a new token bucket instance
const bucket = await TokenBucket.create(tbSettings);
- Take Method
// Takes the first token created in the bucket, if exists! Otherwise the token will be null
const token = await bucket.take();
if (!token) {
// re-queue the message, throw exception or return error
}
...
// Call the Third-party API
- Delay Method
// Usually from HTTP frameworks to cancel requests
const controler = new AbortController()
// In case the bucket is empty, it will block the operation until receive a new token!
// This method accepts abort signal, which means you can cancel the operation at any time
const token = await bucket.delay(controler.signal);
const {
value, // value is a hash string if a token is available, null otherwise
timestamp, // timestamp when the token was created
remaning // remaining is the number of tokens
} = token;
...
// Call the Third-party API