A Comparative Survey of API Rate-Limiting Algorithms: Token Bucket, Leaky Bucket, and Sliding Window
This paper surveys and experimentally compares five widely used API rate-limiting algorithms—token bucket, leaky bucket, fixed window, sliding window log, and sliding window counter—to evaluate their trade-offs in burst tolerance and precision, ultimately providing guidance for selecting the most appropriate algorithm based on specific traffic characteristics and system constraints.
Original paper licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Modern digital services rely on a delicate balance between availability and protection. When millions of people try to access a website or an application at once, the servers behind the scenes can become overwhelmed, much like a single-lane bridge clogged by a sudden surge of traffic. To prevent this collapse, engineers use a mechanism called rate limiting, which acts as a gatekeeper. This gatekeeper counts how many requests a specific user or device sends within a certain period and blocks any that exceed a safe threshold. The goal is not to punish users, but to ensure that the system remains stable for everyone, preventing a few heavy users from consuming all the available resources. However, not all traffic arrives in a steady stream; sometimes it comes in sudden, sharp bursts, such as when a popular news story breaks or a system retries a failed connection. The challenge for engineers is deciding how to handle these bursts: should the system allow a temporary spike to pass through, or should it strictly enforce a flat limit regardless of the situation?
A recent study by Umair Saleem investigates the different mathematical rules used to build these digital gatekeepers. The research focuses on five specific methods that are commonly used in the industry: the token bucket, the leaky bucket, the fixed window counter, the sliding window log, and the sliding window counter. Each of these methods has a different way of tracking time and counting requests, leading to different behaviors when a surge of traffic hits. To understand which method works best, the author did not rely on theory alone but built a computer simulation to test them all under identical conditions. The simulation created a realistic stream of over one thousand requests over a period of one hundred seconds. This stream included a steady background flow of eight requests per second, interrupted by two distinct bursts of activity: a five-second period where traffic jumped to forty requests per second, followed by a sharper, two-second spike reaching sixty requests per second. By running this exact same traffic pattern through each of the five algorithms, the study could measure exactly how many requests each method accepted, how many it rejected, and how the system behaved during the spikes.
The results revealed a clear split in how these algorithms handle the pressure of a traffic surge. The token bucket and the leaky bucket performed in a nearly identical manner when used simply to decide whether to accept or reject a request. Both methods allowed the system to absorb the bursts more effectively than the others, accepting a total of 844 requests out of the 1,057 sent, which translates to an acceptance rate of roughly 80 percent. During the first major burst, these two methods allowed 69 requests through, and during the second, sharper burst, they allowed 38 requests through. This happened because these algorithms are designed with a built-in capacity to store "spare" permission for future use, allowing them to smooth out the spikes without immediately turning users away. In contrast, the sliding window log was the most rigid of all the methods. It never allowed more than ten requests to pass in any single second, strictly adhering to the configured limit. While this provided the most precise protection against overload, it came at a high cost: it rejected the most traffic overall, accepting only 67.9 percent of the requests. It was the only method that guaranteed the system would never see a spike above the limit, but it did so by turning away legitimate users more frequently than the other methods.
The remaining three methods fell somewhere in the middle, showing predictable flaws based on how they measured time. The fixed window counter, which resets its count at the start of every new second, suffered from a timing error at the boundaries. Because it could reset its counter just as a burst of traffic arrived, it allowed a temporary spike of up to fifteen requests in a single second, which was higher than the intended limit. The sliding window counter tried to fix this by looking at the previous second as well, but it only partially corrected the issue, peaking at thirteen requests. The study found that the choice of algorithm depends entirely on what the system needs to protect. If the goal is to keep users happy and allow for natural bursts of activity, such as a page reloading with multiple data calls, the token bucket is the superior choice because it balances high acceptance with steady performance. If the goal is to protect a fragile downstream system that cannot tolerate any spike whatsoever, the sliding window log is the better option, despite its lower acceptance rate. The research concludes that there is no single perfect tool for every job; instead, engineers must choose the method that aligns with their specific tolerance for traffic spikes and their available memory resources.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.