Skip to main content

A flexible and efficient rate limiting library for Django applications

Project description

Django Smart Ratelimit

CI PyPI version Downloads Python Versions Django Versions License

A high-performance, stateless rate limiting library for Django. Protects your APIs from abuse with atomic Redis operations, multiple algorithms, circuit breaking, and full async support -- optimized for distributed systems.

Key Features

  • Stateless and Modern -- Dual-mode support (sync and async) with no database dependencies
  • Enterprise Reliability -- Built-in circuit breaker, automatic failover, and fail-open strategies
  • Multiple Algorithms -- Token bucket, sliding window, fixed window, and leaky bucket
  • Flexible Backends -- Redis (recommended), async Redis, in-memory, MongoDB, or custom backends
  • Precise Control -- Rate limit by IP, user, header, or any custom callable
  • Prometheus Metrics -- Built-in /metrics endpoint for monitoring
  • Structured JSON Logging -- ELK/Datadog/Splunk-compatible structured log output
  • Adaptive Rate Limiting -- Dynamic limits based on CPU, memory, latency, and custom load indicators

Quick Start

Installation

pip install django-smart-ratelimit[redis]

Basic Usage

from django_smart_ratelimit import ratelimit

@ratelimit(key='ip', rate='5/m', block=True)
def login_view(request):
    return authenticate(request)

Keys and algorithms accept plain strings, or the RateLimitKey and Algorithm enums if you prefer autocomplete and a typo-proof contract. The two are interchangeable:

from django_smart_ratelimit import ratelimit
from django_smart_ratelimit.enums import Algorithm, RateLimitKey

@ratelimit(key=RateLimitKey.USER_OR_IP, rate='5/m', algorithm=Algorithm.TOKEN_BUCKET)
def login_view(request):
    return authenticate(request)

Async Support

from django_smart_ratelimit import aratelimit

@aratelimit(key='user', rate='100/h', block=True)
async def api_view(request):
    return await process(request)

Class-Based Views

Apply the decorator to a method with Django's method_decorator:

from django.utils.decorators import method_decorator
from django.views import View

from django_smart_ratelimit import ratelimit


class LoginView(View):
    @method_decorator(ratelimit(key='ip', rate='5/m', block=True))
    def post(self, request):
        return authenticate(request)

Configuration

Add to your Django settings:

RATELIMIT_BACKEND = 'redis'
RATELIMIT_REDIS = {'host': 'localhost', 'port': 6379, 'db': 0}
# Or point at a Redis URL instead of host/port:
# RATELIMIT_REDIS = {'url': 'redis://localhost:6379/0'}

# Optional: enable structured logging
RATELIMIT_LOGGING = {
    'ENABLED': True,
    'FORMAT': 'json',  # "json" or "text"
}

# Optional: enable Prometheus metrics
RATELIMIT_PROMETHEUS = {
    'ENABLED': True,
}

If RATELIMIT_BACKEND is unset, the in-memory backend is used by default.

Documentation

Detailed documentation is available in the docs/ folder:

Topic Description
Full Documentation Start here for the complete guide
Migration Guide Steps for upgrading from django-ratelimit
Algorithms Deep dive into token bucket, sliding window, and more
Configuration Advanced settings, backends, and circuit breakers
Design Philosophy Architecture decisions and comparison with alternatives

Compatibility

Supported Versions
Python 3.9, 3.10, 3.11, 3.12, 3.13
Django 3.2, 4.0, 4.1, 4.2, 5.0, 5.1

Contributing

Contributions are welcome. Please see CONTRIBUTING.md for details on how to submit pull requests, report issues, and set up your development environment.

Community and Support

Sponsors

Support the ongoing development of Django Smart Ratelimit:

Sponsor

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_smart_ratelimit-3.0.0.tar.gz (336.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_smart_ratelimit-3.0.0-py3-none-any.whl (167.3 kB view details)

Uploaded Python 3

File details

Details for the file django_smart_ratelimit-3.0.0.tar.gz.

File metadata

  • Download URL: django_smart_ratelimit-3.0.0.tar.gz
  • Upload date:
  • Size: 336.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_smart_ratelimit-3.0.0.tar.gz
Algorithm Hash digest
SHA256 119ce2c932dccf450f836d3038a195bdce767a683c8356923a14a8ef286cb49a
MD5 e6b5d3dc8eeab3de829a47b0a5fb076c
BLAKE2b-256 c20b19969098006fd6b3106dd605e891f207883e903900408827197ab9194d14

See more details on using hashes here.

File details

Details for the file django_smart_ratelimit-3.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_smart_ratelimit-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5208509a81f49efd2922dc04444f27c5558b910bd43c5aeb0f4f20a34dfaf490
MD5 c84345d21ee5f72362ddbe4233792959
BLAKE2b-256 05d85a6f29b52ceb2c07f31f903617b7615bbb3f57e7b7ac35286f5e6a44829f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page