Bird
Raised Fist0
Flaskframework~20 mins

Rate limiting for protection in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rate Limiting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
Understanding basic rate limiting behavior in Flask
Given this Flask app snippet using Flask-Limiter, what happens when a client exceeds the limit?
Flask
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 index():
    return "Hello, world!"
AThe client is blocked permanently after 3 requests.
BThe client receives a 429 Too Many Requests error after 3 requests in one minute.
CThe client receives a 500 Internal Server Error after 3 requests.
DThe client can make unlimited requests without any error.
Attempts:
2 left
💡 Hint
Think about what '3 per minute' means for request limits.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in Flask-Limiter setup
Which option contains a syntax error in setting up Flask-Limiter with a custom limit?
Flask
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=["5 per minute"])

@app.route("/data")
@limiter.limit("10 per minute")
def data():
    return "Data response"
A
@limiter.limit(10 per minute)
def data():
    return "Data response"
B
@limiter.limit('10 per minute')
def data():
    return "Data response"
C
@limiter.limit("10 per minute")
def data():
    return "Data response"
D
@limiter.limit("10/minute")
def data():
    return "Data response"
Attempts:
2 left
💡 Hint
Check how the limit string is passed in the decorator.
🔧 Debug
advanced
2:00remaining
Why does the rate limit not apply as expected?
A developer uses Flask-Limiter with this code but finds the limit is never enforced. What is the cause?
Flask
from flask import Flask
from flask_limiter import Limiter

app = Flask(__name__)
limiter = Limiter(key_func=lambda: "fixed_key")

@app.route("/test")
@limiter.limit("2 per minute")
def test():
    return "Test endpoint"
AThe Limiter is not attached to the Flask app instance, so limits are not enforced.
BThe key_func returns a fixed string, so all clients share the same limit, causing no limit.
CThe route decorator is missing, so the function is never called.
DThe limit string "2 per minute" is invalid and ignored.
Attempts:
2 left
💡 Hint
Check how Limiter is initialized with the app.
❓ state_output
advanced
2:00remaining
What is the value of remaining requests after 2 calls?
Using Flask-Limiter with a limit of 3 requests per minute, what is the remaining request count after two successful calls from the same client?
Flask
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("/resource")
def resource():
    return "Resource accessed"
A2
B0
C1
D3
Attempts:
2 left
💡 Hint
Think about how many requests remain after two out of three allowed.
🧠 Conceptual
expert
2:00remaining
Why use a distributed rate limiter in Flask apps?
In a Flask app deployed on multiple servers behind a load balancer, why is using a distributed rate limiter important?
ABecause distributed rate limiters automatically block all IP addresses permanently.
BBecause distributed rate limiters are faster than local ones in single-server setups.
CBecause Flask does not support local rate limiting without a distributed system.
DBecause local in-memory limits on each server won't share state, allowing clients to bypass limits by switching servers.
Attempts:
2 left
💡 Hint
Consider what happens when requests come to different servers.

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

  1. Step 1: Understand rate limiting concept

    Rate limiting restricts the number of requests a user can send to prevent overload.
  2. Step 2: Identify the purpose in Flask apps

    It protects the app from abuse and excessive traffic by limiting requests.
  3. Final Answer:

    To control how many requests a user can make in a given time period -> Option A
  4. 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

  1. Step 1: Check Flask-Limiter initialization syntax

    Flask-Limiter requires the Flask app instance and a key function to identify clients.
  2. Step 2: Identify correct parameters

    Passing both app and key_func=get_remote_address is necessary for proper setup.
  3. Final Answer:

    limiter = Limiter(app, key_func=get_remote_address) -> Option C
  4. 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

  1. Step 1: Understand the rate limit decorator

    The decorator limits requests to 2 per minute per user.
  2. Step 2: Analyze the third request behavior

    The third request exceeds the limit and triggers a 429 error response.
  3. Final Answer:

    The third request is blocked with a 429 Too Many Requests error -> Option B
  4. 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:
limiter = Limiter()

@app.route('/test')
@limiter.limit('5 per minute')
def test():
    return 'Test'

What is the likely problem?
medium
A. The limit string format is incorrect
B. The function name is invalid
C. The route decorator is missing
D. Limiter was not given the Flask app instance

Solution

  1. Step 1: Check Limiter initialization

    Limiter must be initialized with the Flask app instance to work.
  2. Step 2: Identify missing app parameter

    Here, Limiter() is called without app, so rate limiting won't activate.
  3. Final Answer:

    Limiter was not given the Flask app instance -> Option D
  4. Quick Check:

    Missing app in Limiter = no limit [OK]
Hint: Always pass app to Limiter for it to work [OK]
Common Mistakes:
  • Ignoring the need to pass app to Limiter
  • Thinking limit string format causes failure
  • Assuming route decorator is missing
5. You want to apply different rate limits for logged-in users and guests in Flask using Flask-Limiter. Which approach correctly implements this?
hard
A. Use a custom key_func that returns user ID if logged in, else IP address
B. Apply the same limit to all users without distinction
C. Set limit only on the route without key_func customization
D. Disable rate limiting for logged-in users completely

Solution

  1. Step 1: Understand user identification for rate limiting

    To differentiate limits, key_func must identify users uniquely.
  2. Step 2: Use custom key_func logic

    Return user ID for logged-in users, IP for guests to apply different limits.
  3. Final Answer:

    Use a custom key_func that returns user ID if logged in, else IP address -> Option A
  4. Quick Check:

    Custom key_func distinguishes users [OK]
Hint: Custom key_func distinguishes users for different limits [OK]
Common Mistakes:
  • Applying same limit to all users ignoring login status
  • Not customizing key_func for user identity
  • Disabling limits for logged-in users without reason
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard