Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is sliding time window rate limiting supported? #317

Open
Dlvnkenye opened this issue Mar 29, 2024 · 3 comments
Open

Is sliding time window rate limiting supported? #317

Dlvnkenye opened this issue Mar 29, 2024 · 3 comments

Comments

@Dlvnkenye
Copy link

Dlvnkenye commented Mar 29, 2024

it seems like rate limiting is only base on fixed time window algorithm. this approach can allow Burt request which in my case such burst is costly. I have some services where each request cost me money. Is there a way using djano-ratelimit to enforce a sliding time window rate limiting? or is there a way to implement custom sliding rate limiting?

@benjaoming
Copy link
Contributor

Can you explain what you mean by "sliding time window"? The rate limiting does "slide", as in if you say "10 requests per minute", then it's the previous 10 minutes. You can then apply several rate limiting rules:

@ratelimit(rate="10/m", ...)
@ratelimit(rate="5/s", ...)

or is there a way to implement custom sliding rate limiting?

If that doesn't work, check the callable rates: https://django-ratelimit.readthedocs.io/en/stable/rates.html#callables

@Dlvnkenye
Copy link
Author

Dlvnkenye commented Mar 29, 2024

The sliding window rate limiting algorithm is based on a dynamic time window that moves with time, allowing for more flexibility in managing bursts of traffic which is in contrast to fixed window rate limiting algorithm.
For instance applying the following rate limits :

@ratelimit(rate="10/m", ...)
@ratelimit(rate="5/s", ...)

This will only partially mitigate the burst of requests. cause during the last 2 seconds of a time window and the first 2 seconds of the subsequent time window a bad actor can send send 20 request under 4 seconds. this may not be that much in terms of CPU cycles but for some services where each request cost money, this becomes an issue.

@benjaoming
Copy link
Contributor

Did you read about callable rates?

Can you imagine how you would like your decorators too look? (pretend django-ratelimit had the feature that you are looking for)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants