This article explores the fundamental **fixed window counter** algorithm for rate limiting, detailing its basic operation and inherent limitations. It covers how this simple approach works, where it falls short in handling traffic spikes at window boundaries, and introduces potential solutions to mitigate these issues, providing a foundational understanding for building more robust rate limiting systems.
Read original on Medium #system-designRate limiting is a critical component in distributed systems, designed to control the rate at which a client or user can make requests to a service. Its primary goals are to prevent resource exhaustion, protect against DoS attacks, and ensure fair usage among all consumers. This mechanism often operates at the API Gateway or individual service level.
The fixed window counter is one of the simplest rate limiting algorithms. It divides time into fixed-size windows (e.g., 1 minute). For each window, a counter is maintained for each client. When a request arrives, the system checks if the counter for the current window has exceeded the predefined limit. If not, the request is processed, and the counter is incremented. If the limit is reached, the request is rejected. After the window expires, the counter is reset.
Fixed Window Counter Example
Imagine a limit of 10 requests per minute. Between 00:00 and 00:59, a client can make 10 requests. At 01:00, the counter resets, and the client can make another 10 requests.
The primary drawback of the fixed window counter is its burstiness at window boundaries. A client could make `N` requests just before a window ends and another `N` requests immediately after the new window begins, effectively making `2N` requests within a short span (e.g., 20 requests in 2 seconds if `N=10` and the window is 1 minute). This can still overwhelm downstream services.
While simple, the fixed window counter's burstiness makes it unsuitable for strict rate limiting. More sophisticated algorithms like sliding log or sliding window counter are often preferred. However, for less critical applications where approximate rate limiting is acceptable, its simplicity can be an advantage. Implementations often involve a distributed cache like Redis to store and increment counters across multiple application instances.