Imagine a busy coffee shop that only has 10 seats. What is the main reason the shop owner would limit the number of customers inside at once?
Similarly, why do APIs use rate limiting?
Think about how too many people in a small space can cause problems.
Rate limiting helps protect services by controlling how many requests a user or client can make in a given time. This prevents overload and keeps the service stable.
Consider this simplified Python code snippet for rate limiting an API endpoint:
requests = 0
limit = 3
for i in range(5):
if requests < limit:
requests += 1
print("Request allowed")
else:
print("Too many requests")What is the output?
requests = 0 limit = 3 for i in range(5): if requests < limit: requests += 1 print("Request allowed") else: print("Too many requests")
Count how many times the request is allowed before the limit is reached.
The code allows 3 requests, then blocks the next 2 because the limit is 3.
Look at this code snippet that tries to limit requests to 2 per user:
user_requests = {}
limit = 2
requests = ["user1", "user1", "user1"]
for user in requests:
if user_requests.get(user, 0) < limit:
user_requests[user] = user_requests.get(user, 0) + 1
print(f"Request from {user} allowed")
else:
print(f"Request from {user} blocked")What is the output?
user_requests = {}
limit = 2
requests = ["user1", "user1", "user1"]
for user in requests:
if user_requests.get(user, 0) < limit:
user_requests[user] = user_requests.get(user, 0) + 1
print(f"Request from {user} allowed")
else:
print(f"Request from {user} blocked")Check how the count is updated and compared before allowing requests.
The code correctly blocks the third request because it increments the count before the next check.
Find the syntax error in this Python code that tries to implement rate limiting:
limit = 5
requests = 0
while requests <= limit
print("Request allowed")
requests += 1limit = 5 requests = 0 while requests <= limit print("Request allowed") requests += 1
Check the syntax of the while loop header.
Python requires a colon ':' at the end of the while condition line.
This code implements a sliding window rate limiter allowing 3 requests per 10 seconds:
import time
class SlidingWindowLimiter:
def __init__(self, limit, window):
self.limit = limit
self.window = window
self.timestamps = []
def allow_request(self):
now = time.time()
self.timestamps = [t for t in self.timestamps if now - t < self.window]
if len(self.timestamps) < self.limit:
self.timestamps.append(now)
return True
return False
limiter = SlidingWindowLimiter(3, 10)
results = []
for _ in range(5):
results.append(limiter.allow_request())
time.sleep(1)
print(results)What will be printed?
import time class SlidingWindowLimiter: def __init__(self, limit, window): self.limit = limit self.window = window self.timestamps = [] def allow_request(self): now = time.time() self.timestamps = [t for t in self.timestamps if now - t < self.window] if len(self.timestamps) < self.limit: self.timestamps.append(now) return True return False limiter = SlidingWindowLimiter(3, 10) results = [] for _ in range(5): results.append(limiter.allow_request()) time.sleep(1) print(results)
Think about how the sliding window removes timestamps older than 10 seconds.
The limiter allows the first 3 requests because they are within the limit. The 4th and 5th requests happen within 10 seconds and exceed the limit, so they are blocked.