r/javascript • • Oct 26 '26

A New Node.js library

https://www.npmjs.com/package/cluster-rate-limiter

When you scale a Node.js backend using node:cluster, you run into a surprisingly annoying problem:

How do you rate-limit external APIs like Stripe or OpenAI while making sure all workers share the same rate limit?

The first solution that comes to mind is usually: "Just use Redis."

But does it really make sense to introduce additional infrastructure complexity, network overhead, and an entire external dependency just to coordinate a few API calls coming from the same server?

That's exactly why I built cluster-rate-limiter.

A Node.js library that gives you coordinated rate limiting across workers, powered entirely by Node.js's native IPC messaging, with no Redis and absolutely zero external dependencies.

Rather than diving into the internals here, here's what you get out of the box:

  • Zero Setup: Install it and start using it. No additional infrastructure required.
  • Developer-Friendly API: The run() method accepts your fetch call or any function directly, along with all its arguments. No complicated callback patterns.
  • Two Types of Limits: Supports both Semaphore-based concurrency limits and Duration-based rate limits for enforcing strict time windows, such as those imposed by Stripe APIs.
  • Typed Errors: If a request times out waiting for permission, the library throws a clear, specific error that you can easily translate into an HTTP 429 Too Many Requests response.

And when it comes to performance, the core data structure for tracking request timestamps uses a zero-allocation approach built around a Float64Array ring buffer.

This eliminates the need for repeated array reallocations and significantly reduces garbage collection pressure under heavy load.

If you're interested in the low-level details, I've explained the ring buffer implementation and how it reduces memory overhead in the README.

If you're running a clustered Node.js backend and want to avoid the overhead of introducing Redis just for rate limiting, check out the links below, explore the architecture, and let me know what you think!

0 Upvotes

5 comments sorted by

2

u/ortegaj50 4d ago

This covers workers on one host; how would you handle the same API quota across multiple Node instances?

1

u/swif_dev 2d ago

thanks for your comment, this is clearly said in the readme :
"
Scope, explicitly: this coordinates the workers spawned by one primary process on one machine, via cluster.fork(). It is not a distributed rate limiter. If you run multiple machines or containers, each one gets its own independent limiter and its own independent pool — the true aggregate rate across N machines is N times whatever you configure, not the configured value. If you need one shared limit across a horizontally-scaled, multi-host deployment, you want something backed by a shared external store (Redis, etc.), not this.
"
the solution to make this available for multi-instances is to spawn a dedicated node process as an IPC server, but such a solution doesn't add anything new to using redis for limit management ( which here using in memory db as redis is better )

if the external API offers its rate limit per machine (as the most cases are) and you use (one node primary and its worker) per machine : this library is for you

1

u/swif_dev 2d ago

I realized just now that this readme part is somehow ambiguous and need rewriting for clarity, thanks and waiting for your feedback to improve

2

u/[deleted] Oct 27 '26

[removed] — view removed comment

1

u/wilson_leo_807 Oct 29 '26

The 429 cooldown is the sneaky one: without sharing Retry-After, every worker can be "within limit" and the API still gets hammered as a team sport.