Implementing Per-User and Per-IP Rate Limits in a REST API
📖 Scenario: You are building a simple REST API server that needs to control how many requests each user and each IP address can make. This helps keep the server safe and fair for everyone.
🎯 Goal: Build a basic Python REST API that tracks requests and limits them per user and per IP address.
📋 What You'll Learn
Create a dictionary called
user_requests to track requests per user IDCreate a dictionary called
ip_requests to track requests per IP addressSet a limit variable called
MAX_REQUESTS to 3Write a function
can_make_request(user_id, ip_address) that returns True if both user and IP are under the limit, otherwise FalsePrint the result of calling
can_make_request for a test user and IP💡 Why This Matters
🌍 Real World
APIs often need to limit how many requests a user or IP can make to prevent overload or abuse.
💼 Career
Understanding rate limiting is important for backend developers and API designers to build reliable and secure services.
Progress0 / 4 steps