upstash / ratelimit-js

Rate limiting library for serverless runtimes
https://ratelimit-with-vercel-kv.vercel.app
MIT License
1.66k stars 33 forks source link

Fix sliding window algorithm #61

Closed mdumandag closed 1 year ago

mdumandag commented 1 year ago

The previous window calculation was wrong. After quantizing the current time into the current window, the previous window is not the one current window - window duration, but simply current window - 1. This miscalculation was causing sliding window to behave like fixed window.

Also, this is a bit subjective but I believe the remaining value we return from the lua script was wrong. In the sliding window, we are not really operating within a fixed window, but an imaginary one that slides as the requests come.

We were simply returning max tokens - number of requests in the current window, but that is possibly more than the number of tokens we would allow, because that was not taking the weighted number of requests from the previous window into account.

If we were %10 into the current window with the number of requests equal to 10, and there were 50 requests in the previous window with the max tokens of 100, we were returning the value 100 - 10 = 90 as the remaining tokens to the user. But, if the user was to make 90 requests at that time, half of them would be rejected, because we are taking the 50 * (1 - 0.1) = 45 requests from the previous window into account while checking if the request should be rejected or not. Therefore, I adjusted the remaining tokens returned from the lua script accordingly.

vercel[bot] commented 1 year ago

@mdumandag is attempting to deploy a commit to the Upstash Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
ratelimit-with-vercel-kv ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 10, 2023 9:44am