0
0
Rest APIprogramming~30 mins

Fixed window algorithm in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Implementing Fixed Window Rate Limiting Algorithm
📖 Scenario: You are building a simple REST API server that limits how many requests a user can make in a fixed time window. This helps prevent abuse and keeps the server stable.
🎯 Goal: Build a fixed window rate limiter that counts requests per user in a 60-second window and blocks requests exceeding the limit.
📋 What You'll Learn
Create a dictionary to store request counts per user
Add a variable for the fixed window duration in seconds
Implement the fixed window logic to count requests and reset counts after the window expires
Print whether a request is allowed or blocked based on the rate limit
💡 Why This Matters
🌍 Real World
Rate limiting is used in APIs to prevent too many requests from one user, protecting servers from overload and abuse.
💼 Career
Understanding rate limiting algorithms is important for backend developers and API designers to build reliable and secure services.
Progress0 / 4 steps
1
Create the request count storage
Create a dictionary called request_counts to store the number of requests per user. Initialize it as an empty dictionary.
Rest API
Need a hint?

Use curly braces {} to create an empty dictionary in Python.

2
Set the fixed window duration
Create a variable called window_duration and set it to 60 to represent the fixed window length in seconds.
Rest API
Need a hint?

Just assign the number 60 to the variable window_duration.

3
Implement the fixed window logic
Write a function called is_request_allowed(user_id, current_time) that uses request_counts and window_duration to count requests per user. If the user has made fewer than 5 requests in the current window, increase the count and return True. Otherwise, return False. Use a dictionary value as a tuple (window_start_time, count) to track the window start and count.
Rest API
Need a hint?

Use the current time to check if the window expired. Reset count if expired, else increment if under limit.

4
Test and print the rate limiter result
Call is_request_allowed('user1', 100) five times and print the result each time. Then call it a sixth time and print the result. This shows the limiter blocking the sixth request.
Rest API
Need a hint?

Use a for loop to call the function 6 times and print the results with the request number.