Skip to content

BurstyRateLimiter

Roman edited this page Jul 12, 2020 · 10 revisions

BurstyRateLimiter

Allow traffic bursts with BurstyRateLimiter implementation easier than with TokenBucket.

The idea is to rate limit traffic by two limiters: limiter and burst limiter. If there are no points in the first, try to consume from the second limiter. The scond limiter usually configured with wider duration. See example for details.

const {RateLimiterMemory, BurstyRateLimiter} = require('rate-limiter-flexible');
const http = require('http');

const burstyLimiter = new BurstyRateLimiter(
  new RateLimiterMemory({
    points: 2,
    duration: 1,
  }),
  new RateLimiterMemory({
    keyPrefix: 'burst',
    points: 5,
    duration: 10,
  })
);

const srv = http.createServer(async (req, res) => {
  burstyLimiter.consume('test')
    .then((rlRes) => {
      res.end(JSON.stringify(rlRes));
    })
    .catch((rej) => {
      res.writeHead(429);
      res.end(JSON.stringify(rej));
    });
});

srv.listen(3000);

This burstyLimiter limits traffic to 2 requests per second with additional allowance of traffic burst up to 5 requests per 10 seconds.

consume method of BurstyRateLimiter resolves and rejects with RateLimiterRes object from the first limiter, but msBeeforeNext may be set from the burst limiter if it is less.consume method never exposes the second limiter's remaining or consumed points.

All limiters from this package can be used for BurstyRateLimiter creation.