A flexible and efficient rate limiting library for Django applications
Project description
Django Smart Ratelimit
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
/metricsendpoint 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)
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
from django_smart_ratelimit import RateLimitMixin
class LoginView(RateLimitMixin, View):
ratelimit_key = 'ip'
ratelimit_rate = '5/m'
ratelimit_block = True
Configuration
Add to your Django settings:
RATELIMIT_DEFAULT_BACKEND = 'redis'
RATELIMIT_REDIS_URL = 'redis://localhost:6379/0'
# Optional: enable structured logging
RATELIMIT_LOGGING = {
'ENABLED': True,
'LEVEL': 'INFO',
'FORMAT': 'json',
}
# Optional: enable Prometheus metrics
RATELIMIT_PROMETHEUS = {
'ENABLED': True,
}
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
- GitHub Discussions -- Ask questions and share ideas
- Issues -- Report bugs
- Changelog -- Release history
Sponsors
Support the ongoing development of Django Smart Ratelimit:
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_smart_ratelimit-2.2.1.tar.gz.
File metadata
- Download URL: django_smart_ratelimit-2.2.1.tar.gz
- Upload date:
- Size: 278.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80cc34b5b0803cbaa348c420fb53cd79ac72008a7f41118d02200df716585d6d
|
|
| MD5 |
390a77d14cd1d5d6d80d685b8df48300
|
|
| BLAKE2b-256 |
a37696391289b7dfc0ca3ddf1abf5e1c50a6ea11e78d6b0838b8b47784238510
|
File details
Details for the file django_smart_ratelimit-2.2.1-py3-none-any.whl.
File metadata
- Download URL: django_smart_ratelimit-2.2.1-py3-none-any.whl
- Upload date:
- Size: 133.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a4cc42c5a10e38d4069d3a730c1de7c7404f350a3c6753622a1274a87a20931
|
|
| MD5 |
8cc6d268dd9dc9a8015b76ce6f040f93
|
|
| BLAKE2b-256 |
4a1a7d8ce130c2b4646765a67318e283ea96a4ef3da7c3a3a6c7be9ce11d76fe
|