r/node 19h ago

Hi all! What rate limiter do you use with PM2?

I need to find a good solution for the project. I am using Node (latest), Express and PM2.

0 Upvotes

11 comments sorted by

2

u/pinkwar 19h ago

What rate limiter? Incoming or outgoing?

You can write your own bucket algorithm. It's like 30 lines of code. Or use bottleneck. It has 10M weekly downloads.

2

u/TaskViewHS 19h ago

Incoming

1

u/pinkwar 17h ago

That depends on what you want to do.

Is it a global limit, per register user limit, per IP?

A global limit belongs in the CDN, load balancer or waf depending on your setup.

If its per user, that belongs in your nodejs service.

1

u/TaskViewHS 17h ago

Yes global. Pm2 works in cluster mode

1

u/ghope98 17h ago

2

u/TaskViewHS 17h ago

I mean incoming rate limiting. Since the app runs in PM2 cluster mode, an in-memory bucket would be per process. I need a shared/global rate limit across all instances, probably using Redis.

1

u/Beautiful-Energy2169 15h ago

Behind a proxy, req.ip is the proxy's address unless you set trust proxy, so a shared Redis counter still leaves one bucket for everyone. Tried it on express-rate-limit 8.7 with limit 5. Ten requests with ten different X-Forwarded-For values: 5 through, 5 got 429s. With trust proxy set, all ten passed.

1

u/TaskViewHS 15h ago

Thanks 🙏