SGrondin/bottleneck

Accuracy of reservoir refresh interval?

swayam18 opened this issue · 3 comments

In the docs it says that the accuracy of reservoirRefreshInterval is +-5 seconds.

This is a problem because many APIs have limits with 1 second granularity, i.e. they allow x requests every second. So, we may be off their rate limit by nearly 5x.

Is there something I can do to make sure the reservoir is refreshed accurately every second? Should I be using a custom timeout + incrementReservoir function?

It's an oversight in the docs, it's actually only 5s (5000ms) when using Clustering and 250ms otherwise.

It's set to 5000 for Clustering for performance reasons and it can be altered by passing the (currently undocumented) heartbeatInterval option. I'll fix the docs. It could only really affects performance with a very high number of limiters.

Ah, makes sense. I will add the heartbeatInterval option to my cluster and set it to 250ms. Cheers

Glad that helps! Here's a few more details you might be interested in.

The inaccuracy is only "below", not "above". What I mean is that the actual Reservoir Refresh Interval might be slower than your reservoirRefreshInterval setting by up to heartbeatInterval, but it will never be too fast. It's designed this way to ensure you never blow up your rate limits.

Also, assuming all of your servers have an accurate and consistent (across the cluster) internal clock, then the more limiters you have sharing the same id in the Cluster, the more accurate it will be, even with a high heartbeatInterval.

Finally, the more traffic limiters get, the more accurate your refresh interval is. Again, it will never be "faster than reservoirRefreshInterval", it can only be "a bit slower than reservoirRefreshInterval".

As usual, make sure to benchmark and adjust it for your own use case.