Fixed Window Algorithm - Rate Limter
The Fixed Window Algorithm is one of the simplest rate-limiting mechanisms. It controls how many requests a user or system can make within a defined, fixed time interval. This technique is widely used in API rate limiting and web services where managing a predictable flow of traffic is crucial.
By enforcing a limit on the number of requests, the Fixed Window algorithm prevents resource overloading, ensuring fair use of services.
How It Works
- Time Window: The system sets a fixed time window (e.g., 1 minute, 10 seconds, etc.). All requests made by the user are tracked within this time frame.
- Request Counting: During each time window, the system counts the number of requests sent by the user.
- Request Limits: If the user exceeds the set limit within the current time window, any further requests are denied until the next window begins.
Use Cases
Letβs assume a user is allowed to make 100 API requests per minute. Within the Time Window: If the user sends 50 requests in 30 seconds, they are allowed to make 50 more during the remaining time. At the Limit: If the user hits 100 requests before the 60-second window ends, no more requests are accepted. They must wait for the next minute to start fresh.
Scenarios
- Steady Traffic Scenarios: The Fixed Window algorithm works well in environments where you expect a consistent and predictable flow of requests, such as a steady API call rate from multiple users.
- Systems Not Sensitive to Traffic Bursts: If your system can handle short-term bursts of traffic without being overloaded, this algorithm is appropriate.
- Basic Rate Limiting: Ideal for basic rate-limiting rules where simplicity and predictability are more important than flexibility.
- Start: The algorithm begins tracking requests as soon as the user starts making them.
- Define Time Window: A fixed time window is established (e.g., 1 minute).
- Track User Requests: All incoming requests from the user are counted.
- Decision: The system checks whether the user has exceeded their request limit:
- If No, the request is allowed, and the request count is incremented.
- If Yes, the request is rejected, and the system waits for the next time window.
- Reset: Once the next time window starts, the request counter resets, allowing the user to send requests again.
Limitations
Though simple, the Fixed Window algorithm has some limitations:
- Handling Traffic Spikes: It is not ideal for handling traffic spikes effectively. For example, if a user sends 100 requests right before the end of a minute and then 100 more immediately at the start of the next minute, the system will get hit by 200 requests almost simultaneously, which can overwhelm servers.
- No Smoothing: Unlike more advanced algorithms (like sliding window), the Fixed Window approach doesnβt smooth out the flow of requests. It simply tracks them in chunks of time.
How system get more than limit
The Fixed Window Algorithm struggles to handle traffic spikes effectively. Consider this case:
- A user sends 100 requests at the very end of one time window (e.g., second 59).
- When the next time window starts (e.g., at second 60), they send another 100 requests.
In this case, the system experiences 200 requests in a short burst, which can cause temporary overload.
To illustrate this scenario visually:
In this sequence, although the system limits requests to 100 per minute, the user can still send a significant number of requests within a short time, leading to a burst in traffic.
Next
In the following tutorials, we will explore how the limitations of the Fixed Window Algorithm are addressed by other rate-limiting algorithms. Each algorithm introduces its own approach to managing traffic, offering solutions for handling burst traffic, smoothing request flow, and ensuring system stability. As we dive deeper, you will see how techniques like the Sliding Window and Token Bucket algorithms tackle the challenges posed by the Fixed Window method, improving accuracy, flexibility, and efficiency.