A fixed window rate limiting based on Redis
- Python >= 3.6
- Django >= 1.11
- Redis
To install django-redis-ratelimit, simply:
$ pip install django-redis-ratelimit
NB! django-redis-ratelimit requires a running Redis server. See Redis's quickstart for installation instructions.
First, add the middleware to your settings.py
:
MIDDLEWARE = [
# ...
'redis_ratelimit.middleware.RateLimitMiddleware',
]
this will make sure that end user sees the HTTP 429 response.
Next, apply the ratelimit
decorator to the view:
from django.http import HttpResponse
from redis_ratelimit import ratelimit
@ratelimit(rate='5/m')
def index(request):
return HttpResponse("Hello World!")
For this example we will assume that each key takes up roughly 250 bytes and each value is 4 bytes:
250 + 4 * 1 million unique hits = ~254 Megabytes
MIT