What if your website unfairly blocks real users just because they share an internet connection?
Per-user vs per-IP limits in Rest API - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a popular website where many people log in and use your services. You want to stop anyone from sending too many requests and slowing down your system. So, you try to limit how many requests come from each IP address manually.
But this manual way is tricky because many users can share the same IP (like in offices or cafes), and some users might use different IPs. This causes unfair blocking or lets some users overload your system. It's slow and full of mistakes.
Using per-user and per-IP limits together helps you control traffic smartly. You can limit each user's requests no matter where they connect from, and also limit requests from suspicious IPs. This keeps your system fair and safe without blocking good users.
if requests_from_ip > 100: block_ip()
if requests_from_user > 50: block_user() if requests_from_ip > 100: block_ip()
This approach lets you protect your service from overload while treating users fairly, improving reliability and user experience.
A streaming service limits each logged-in user to 3 devices (per-user limit) and also blocks IPs that send thousands of requests in minutes (per-IP limit) to stop hackers and keep streams smooth.
Manual IP-only limits can block many good users sharing one IP.
Per-user limits track individual users regardless of IP changes.
Combining both limits protects systems fairly and effectively.
Practice
per-user and per-IP rate limits in REST APIs?Solution
Step 1: Understand per-user limits
Per-user limits count how many requests each user (identified by login or token) makes.Step 2: Understand per-IP limits
Per-IP limits count requests based on the IP address making the request, regardless of user identity.Final Answer:
Per-user limits track requests by user identity; per-IP limits track requests by the requester's IP address. -> Option CQuick Check:
Per-user = user identity, Per-IP = IP address [OK]
- Confusing user identity with IP address
- Thinking per-IP limits block users
- Assuming per-user limits apply only to logged-out users
Solution
Step 1: Identify per-user check
Per-user limits check how many requests a user has made, so the condition should compare requests_from_user to the limit.Step 2: Verify correct syntax
The correct syntax is to compare requests_from_user > limit and block if true.Final Answer:
if requests_from_user > limit: block_request() -> Option AQuick Check:
Check user requests count > limit [OK]
- Using IP variable for per-user limit
- Comparing user or IP directly to limit
- Using equality instead of greater than
requests_per_user = {"alice": 5, "bob": 3}
requests_per_ip = {"192.168.1.1": 10, "10.0.0.2": 2}
user = "alice"
ip = "192.168.1.1"
user_limit = 5
ip_limit = 10
if requests_per_user[user] >= user_limit:
print("User limit reached")
elif requests_per_ip[ip] >= ip_limit:
print("IP limit reached")
else:
print("Request allowed")What will be printed?
Solution
Step 1: Check user request count
requests_per_user["alice"] is 5, which is equal to user_limit (5), so the first if condition is true.Step 2: Determine which print runs
Since the first condition is true, it prints "User limit reached" and skips the rest.Final Answer:
User limit reached -> Option BQuick Check:
5 >= 5 triggers user limit [OK]
- Thinking IP limit triggers first
- Ignoring >= condition
- Assuming else runs when equal
requests_per_ip = {"1.2.3.4": 8}
ip_limit = 10
ip = "1.2.3.4"
if requests_per_ip[ip] > ip_limit:
print("Limit exceeded")
else:
print("Allowed")What is the bug and how to fix it?
Solution
Step 1: Analyze condition logic
The code blocks requests only if requests_per_ip[ip] > ip_limit, so if requests equal ip_limit, it allows the request.Step 2: Fix condition to include equal case
Change > to >= so requests equal to ip_limit also get blocked.Final Answer:
Bug: Uses > instead of >=; fix by changing to >=. -> Option AQuick Check:
Use >= to block at limit [OK]
- Ignoring equal case in condition
- Assuming IP variable type is wrong
- Thinking missing keys cause this bug
Solution
Step 1: Understand combined blocking logic
The request should be blocked if either the user or the IP exceeds their limit, so the condition must use OR.Step 2: Check condition correctness
if requests_per_user[user] > user_limit or requests_per_ip[ip] > ip_limit: block_request() uses OR with > comparisons, correctly blocking if user or IP exceeds limits.Final Answer:
if requests_per_user[user] > user_limit or requests_per_ip[ip] > ip_limit: block_request() -> Option DQuick Check:
Block if user OR IP exceeds limit [OK]
- Using AND instead of OR
- Using < instead of >
- Checking equality only
