Flask-Limiter helps you control how many times users can use your web app in a short time. This stops too many requests that can slow down or break your app.
Flask-Limiter for rate limiting
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=["5 per minute"] ) @app.route("/some_path") @limiter.limit("10 per hour") def some_function(): return "Hello!"
key_func tells Flask-Limiter how to identify users, usually by IP address.
You can set default_limits for all routes or use @limiter.limit to set limits per route.
limiter = Limiter(app, key_func=get_remote_address, default_limits=["10 per minute"]) @app.route("/api") @limiter.limit("3 per second") def api(): return "API response"
@app.route("/login") @limiter.limit("5 per minute") def login(): return "Login page"
limiter = Limiter(app, key_func=lambda: "user123", default_limits=["100 per day"]) @app.route("/dashboard") def dashboard(): return "User dashboard"
This Flask app limits all routes to 3 requests per minute by default. The /fast route is stricter and allows only 1 request every 10 seconds per user IP.
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("/fast") @limiter.limit("1 per 10 seconds") def fast(): return "This route is limited to 1 request every 10 seconds." if __name__ == "__main__": app.run(debug=True)
Flask-Limiter returns a 429 error automatically when the limit is exceeded.
You can customize the error message or handle it with Flask error handlers.
Use key_func wisely to identify users correctly (IP, user ID, etc.).
Flask-Limiter helps protect your app by limiting how often users can make requests.
You can set limits globally or per route using simple decorators.
It uses a key function to identify users, usually by IP address.
Practice
Flask-Limiter in a Flask application?Solution
Step 1: Understand Flask-Limiter's role
Flask-Limiter is designed to control the number of requests a user can make to prevent abuse.Step 2: Compare options with Flask-Limiter's purpose
Options A, B, and D relate to documentation, caching, and migrations, which are unrelated to rate limiting.Final Answer:
To limit how many requests a user can make in a given time -> Option AQuick Check:
Flask-Limiter = request limiting [OK]
- Confusing Flask-Limiter with caching tools
- Thinking it manages database or docs
- Assuming it speeds up the app
Solution
Step 1: Check the syntax for rate limit string
The correct format is a string like '5/minute' specifying count and time unit.Step 2: Validate each option's syntax
@limiter.limit('5/minute')\ndef my_route():\n return 'Hello' uses '5/minute' which is correct. Options B, C, and D use incorrect formats or missing quotes.Final Answer:
@limiter.limit('5/minute')\ndef my_route():\n return 'Hello' -> Option DQuick Check:
Correct limit string format = @limiter.limit('5/minute')\ndef my_route():\n return 'Hello' [OK]
- Omitting quotes around the limit string
- Using numbers without units
- Incorrect time unit format
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)
@app.route('/')
@limiter.limit('2/minute')
def home():
return 'Welcome!'
# Assume the same client makes 3 quick requests
What will happen on the third request from the same client within one minute?Solution
Step 1: Understand the limit set
The route is limited to 2 requests per minute per client IP.Step 2: Analyze the third request behavior
The third request exceeds the limit, so Flask-Limiter blocks it with a 429 error.Final Answer:
The third request will be blocked with a 429 Too Many Requests error -> Option AQuick Check:
Requests > limit = 429 error [OK]
- Thinking extra requests succeed silently
- Assuming app crashes on limit exceed
- Believing requests get delayed instead of blocked
from flask import Flask
from flask_limiter import Limiter
app = Flask(__name__)
limiter = Limiter(app)
@app.route('/test')
@limiter.limit('3/minute')
def test():
return 'Test'
When running this code, the rate limiting does not work as expected. What is the most likely cause?Solution
Step 1: Check Limiter initialization
Limiter is created without a key_func, so it uses default which may not distinguish clients properly.Step 2: Understand key_func role
Without key_func like get_remote_address, all requests share the same limit, causing unexpected behavior.Final Answer:
Missing key_func argument to identify clients, so all requests share the same limit -> Option CQuick Check:
key_func missing = shared limit problem [OK]
- Assuming limit string syntax is wrong
- Thinking decorator order matters here
- Believing app creation order affects Limiter
/special to have a higher limit of 30 requests per minute using Flask-Limiter. Which code snippet correctly implements this?Solution
Step 1: Set global default limits
Usedefault_limits=['10/minute']in Limiter to apply global rate limits.Step 2: Override limit on specific route
Use@limiter.limit('30/minute')decorator on/specialroute to allow higher limit.Final Answer:
limiter = Limiter(app, key_func=get_remote_address, default_limits=['10/minute']) @app.route('/special') @limiter.limit('30/minute') def special(): return 'Special access' -> Option BQuick Check:
Global default + route override = limiter = Limiter(app, key_func=get_remote_address, default_limits=['10/minute']) @app.route('/special') @limiter.limit('30/minute') def special(): return 'Special access' [OK]
- Confusing default_limits with per-route limits
- Not setting key_func for client identification
- Trying to change default_limits after initialization
Ready to prove it?
Pass the test and this pattern moves unknown
