Rate limiting helps stop too many requests from one user or computer. It protects your app from being overloaded or attacked.
Rate limiting for protection in Flask
from flask_limiter import Limiter from flask_limiter.util import get_remote_address limiter = Limiter( app, key_func=get_remote_address, default_limits=["5 per minute"] ) @app.route("/some_route") @limiter.limit("10 per hour") def some_route(): return "Hello!"
Use Limiter to set rules for how many requests are allowed.
key_func decides how to identify users, usually by IP address.
limiter = Limiter(app, key_func=get_remote_address, default_limits=["100 per day"])@app.route("/login") @limiter.limit("5 per minute") def login(): return "Login page"
@app.route("/api/data") @limiter.limit("20 per hour") def api_data(): return "API data"
This Flask app has two routes. The home page allows 3 requests per minute by default. The /limited page allows only 2 requests per minute per user IP. If a user sends more requests, they get blocked temporarily.
from flask import Flask from flask_limiter import Limiter from flask_limiter.util import get_remote_address app = Flask(__name__) limiter = Limiter( app, key_func=get_remote_address, default_limits=["3 per minute"] ) @app.route("/") def home(): return "Welcome to the home page!" @app.route("/limited") @limiter.limit("2 per minute") def limited(): return "This page is rate limited to 2 requests per minute." if __name__ == "__main__": app.run(debug=True)
Make sure to install Flask-Limiter with pip install flask-limiter.
Rate limits reset after the time window (like 1 minute) passes.
You can customize the error message or response when limits are exceeded.
Rate limiting controls how many requests a user can make in a time period.
It helps protect your app from overload and abuse.
Flask-Limiter is an easy way to add rate limiting to Flask apps.
Practice
Solution
Step 1: Understand rate limiting concept
Rate limiting restricts the number of requests a user can send to prevent overload.Step 2: Identify the purpose in Flask apps
It protects the app from abuse and excessive traffic by limiting requests.Final Answer:
To control how many requests a user can make in a given time period -> Option AQuick Check:
Rate limiting = request control [OK]
- Confusing rate limiting with database optimization
- Thinking it changes UI behavior
- Assuming it encrypts data automatically
Solution
Step 1: Check Flask-Limiter initialization syntax
Flask-Limiter requires the Flask app instance and a key function to identify clients.Step 2: Identify correct parameters
Passing both app and key_func=get_remote_address is necessary for proper setup.Final Answer:
limiter = Limiter(app, key_func=get_remote_address) -> Option CQuick Check:
Limiter init requires app and key_func [OK]
- Omitting the app parameter
- Not specifying key_func
- Calling Limiter without arguments
@app.route('/api')
@limiter.limit('2 per minute')
def api():
return 'OK'What happens if a user sends 3 requests within one minute?
Solution
Step 1: Understand the rate limit decorator
The decorator limits requests to 2 per minute per user.Step 2: Analyze the third request behavior
The third request exceeds the limit and triggers a 429 error response.Final Answer:
The third request is blocked with a 429 Too Many Requests error -> Option BQuick Check:
Exceeding limit = 429 error [OK]
- Assuming all requests succeed
- Thinking server crashes on limit
- Believing all requests get blocked
limiter = Limiter()
@app.route('/test')
@limiter.limit('5 per minute')
def test():
return 'Test'What is the likely problem?
Solution
Step 1: Check Limiter initialization
Limiter must be initialized with the Flask app instance to work.Step 2: Identify missing app parameter
Here, Limiter() is called without app, so rate limiting won't activate.Final Answer:
Limiter was not given the Flask app instance -> Option DQuick Check:
Missing app in Limiter = no limit [OK]
- Ignoring the need to pass app to Limiter
- Thinking limit string format causes failure
- Assuming route decorator is missing
Solution
Step 1: Understand user identification for rate limiting
To differentiate limits, key_func must identify users uniquely.Step 2: Use custom key_func logic
Return user ID for logged-in users, IP for guests to apply different limits.Final Answer:
Use a custom key_func that returns user ID if logged in, else IP address -> Option AQuick Check:
Custom key_func distinguishes users [OK]
- Applying same limit to all users ignoring login status
- Not customizing key_func for user identity
- Disabling limits for logged-in users without reason
Ready to prove it?
Pass the test and this pattern moves unknown
