Limit concurrent requests using a balancer or a middleware

One Paragraph Explainer

Rate limiting should be implemented in your application to protect a Node.js application from being overwhelmed by too many requests at the same time. Rate limiting is a task best performed with a service designed for this task, such as nginx, however it is also possible with rate-limiter-flexible package or middleware such as express-rate-limiter for Express.js applications.

Code example: pure Node.js app with

  1. const http = require('http');
  2. const redis = require('redis');
  3. const { RateLimiterRedis } = require('rate-limiter-flexible');
  4. const redisClient = redis.createClient({
  5. enable_offline_queue: false,
  6. });
  7. // Maximum 20 requests per second
  8. const rateLimiter = new RateLimiterRedis({
  9. storeClient: redisClient,
  10. points: 20,
  11. duration: 1,
  12. blockDuration: 2, // block for 2 seconds if consumed more than 20 points per second
  13. });
  14. http.createServer(async (req, res) => {
  15. try {
  16. const rateLimiterRes = await rateLimiter.consume(req.socket.remoteAddress);
  17. // Some app logic here
  18. res.writeHead(200);
  19. res.end();
  20. } catch {
  21. res.writeHead(429);
  22. res.end('Too Many Requests');
  23. }
  24. })
  25. .listen(3000);

You can find more examples in the documentation.

Code example: Express rate limiting middleware for certain routes

Using express-rate-limiter npm package

  1. const RateLimit = require('express-rate-limit');
  2. // important if behind a proxy to ensure client IP is passed to req.ip
  3. app.enable('trust proxy');
  4. const apiLimiter = new RateLimit({
  5. windowMs: 15*60*1000, // 15 minutes
  6. max: 100,
  7. });
  8. // only apply to requests that begin with /user/
  9. app.use('/user/', apiLimiter);

What Other Bloggers Say

From the NGINX blog:

Rate limiting can be used for security purposes, for example to slow down brute‑force password‑guessing attacks. It can help protect against DDoS attacks by limiting the incoming request rate to a value typical for real users, and (with logging) identify the targeted URLs. More generally, it is used to protect upstream application servers from being overwhelmed by too many user requests at the same time.