DEV Community

Cover image for Distributed Rate Limiter
Dhroov Gupta
Dhroov Gupta

Posted on

Distributed Rate Limiter

Introduction

I recently created a rate limiter library for the distributed systems that can be used to control and limit the number of requests made within a specific period of time.

The Idea

The idea behind the library is to create a token bucket that replenishes tokens at a certain rate. Each time a request is made, the library checks if there are enough tokens available in the bucket. If there are, it removes a token and allows the request. If not, it rejects the request.

The Solution

I implemented the token bucket algorithm using Redis as a distributed storage system. The library, called dist-rate, takes an options object that includes the number of tokens in the token bucket, the duration for which the tokens are replenished, and an instance of the Redis client to use for distributed locking and storage.

Usage

To use the dist-rate-limiter library in your project, simply install it via npm:

npm install dist-rate-limiter
Enter fullscreen mode Exit fullscreen mode

To use the package, create an instance of the DistRate class and call the execute() method with a unique ID for each request. The method returns a boolean indicating whether the request should be allowed or not.

import IORedis from "ioredis";
import { DistRate } from "distrate";

const redisClient = new IORedis.Cluster([...]);

const rateLimiter = new DistRate({
  tokens: 10,
  duration: 60,
  redisClient,
});

const allowed = await rateLimiter.execute("user123");
Enter fullscreen mode Exit fullscreen mode

You can then use the rateLimiter instance to control and limit the number of requests made to your API or server.

Links

The dist-rate library is available on both GitHub and npm. You can find the GitHub repository here:

Github - https://github.com/Dhroov7/distRate
NPM - https://www.npmjs.com/package/dist-rate

Top comments (0)