Rate Limiting Not Working Fix
In this tutorial, you'll learn about Rate Limiting Not Working Fix. We cover key concepts, practical examples, and best practices.
Your rate-limiting configuration is not blocking excessive requests — the rate limiter is using the wrong identifier (IP behind proxy), the store is misconfigured, or the middleware order is wrong.
Step-by-Step Fix
1. Check the client IP extraction
from flask import request
import time
# Wrong — using remote_addr which may be the proxy IP
client_ip = request.remote_addr
# Returns 127.0.0.1 when behind nginx
# Right — use X-Forwarded-For header
def get_client_ip():
if "X-Forwarded-For" in request.headers:
return request.headers["X-Forwarded-For"].split(",")[0].strip()
return request.remote_addr
2. Fix nginx proxy configuration
# Wrong — not forwarding the real IP
location / {
proxy_pass http://app:8000;
}
# Right — pass the real client IP
location / {
proxy_pass http://app:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
3. Use a proper rate limiter with Redis
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
# Wrong — using in-memory storage (lost on restart)
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["100 per day"],
)
# Right — use Redis for persistent storage
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["100 per day"],
storage_uri="redis://localhost:6379",
)
4. Apply rate limits to specific routes
# Wrong — rate limit applied but not enforced
@limiter.limit("5 per minute")
@app.route("/login")
def login():
pass # Rate limit not working due to middleware order
# Right — apply limiter before route handler
@app.route("/login")
@limiter.limit("5 per minute")
def login():
pass
Common Mistakes
| Mistake | Fix |
|---|---|
| Using client IP when behind a proxy | Extract IP from X-Forwarded-For header |
| In-memory storage in multi-process setup | Use Redis or Memcached for shared rate limit state |
| Wrong decorator order in Flask | Apply @limiter.limit() after @app.route() |
| Rate limiting only on some routes | Apply a default limit globally and override per route |
| No rate limits on auth endpoints | Always rate-limit login, registration, and password reset endpoints |
Prevention
- Use a distributed rate limiter backed by Redis.
- Rate limit by user ID for authenticated endpoints, by IP for public ones.
- Return proper
Retry-Afterheaders for throttled requests. - Set rate limits on all authentication and data-modification endpoints.
- Monitor rate limit hit rates to detect brute force attempts.
DodaTech Tools
Doda Browser's rate limit tester sends controlled bursts to validate throttling behavior. DodaZIP archives rate limit configuration snapshots for deployment audit trails. Durga Antivirus Pro uses rate limiting itself to prevent API abuse across its alerting endpoints.
Common Mistakes with limiting config
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world RATE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro