📖 Scenario: You are building a simple Flask web app that serves a welcome message. To keep your app safe from too many requests in a short time, you want to add rate limiting. This means users can only make a few requests per minute.
🎯 Goal: Build a Flask app with a route / that returns a welcome message. Add rate limiting to allow only 3 requests per minute per user IP address.
📋 What You'll Learn
Create a Flask app with a single route /
Set up a rate limiter that allows 3 requests per minute per IP
Return a simple welcome message from the route
Use the Flask-Limiter extension for rate limiting
💡 Why This Matters
🌍 Real World
Rate limiting protects web apps from too many requests that can overload servers or cause abuse.
💼 Career
Understanding rate limiting is important for backend developers and security engineers to build reliable and secure web services.
Progress0 / 4 steps
1
Create a basic Flask app with a route
Import Flask from flask. Create a Flask app called app. Define a route / with a function home that returns the string 'Welcome to the site!'.
Flask
Hint
Use @app.route('/') to create the route and define a function that returns the welcome message.
2
Import and configure Flask-Limiter
Import Limiter and HEADERS from flask_limiter. Create a Limiter instance called limiter with app as the Flask app and key_func set to lambda: request.remote_addr. Also import request from flask.
Flask
Hint
Use Limiter(app, key_func=lambda: request.remote_addr) to limit by user IP address.
3
Apply rate limit to the route
Add the decorator @limiter.limit('3 per minute') above the home function to limit requests to 3 per minute per IP.
Flask
Hint
Put @limiter.limit('3 per minute') just above the home function.
4
Add app run block for local testing
Add the block if __name__ == '__main__': and inside it call app.run(debug=True) to run the Flask app locally with debug mode.
Flask
Hint
Use the standard Flask app run block to start the server.
Practice
(1/5)
1. What is the main purpose of rate limiting in a Flask application?
easy
A. To control how many requests a user can make in a given time period
B. To speed up the database queries
C. To change the user interface dynamically
D. To encrypt user data automatically
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 A
Quick Check:
Rate limiting = request control [OK]
Hint: Rate limiting limits requests per time to protect apps [OK]
Common Mistakes:
Confusing rate limiting with database optimization
Thinking it changes UI behavior
Assuming it encrypts data automatically
2. Which of the following is the correct way to initialize Flask-Limiter in a Flask app?
easy
A. limiter = Limiter(app)
B. limiter = Limiter(key_func=get_remote_address)
C. limiter = Limiter(app, key_func=get_remote_address)
D. limiter = Limiter()
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 C
Quick Check:
Limiter init requires app and key_func [OK]
Hint: Always pass app and key_func when creating Limiter [OK]
Common Mistakes:
Omitting the app parameter
Not specifying key_func
Calling Limiter without arguments
3. Given this Flask route with rate limiting:
@app.route('/api')
@limiter.limit('2 per minute')
def api():
return 'OK'
What happens if a user sends 3 requests within one minute?
medium
A. The third request returns 'OK' as usual
B. The third request is blocked with a 429 Too Many Requests error
C. All requests are blocked
D. The server crashes
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 B
Quick Check:
Exceeding limit = 429 error [OK]
Hint: Requests over limit get 429 error response [OK]
Common Mistakes:
Assuming all requests succeed
Thinking server crashes on limit
Believing all requests get blocked
4. You wrote this code but rate limiting is not working: