API Rate Limiting Explained: Top 4 Algorithms for System Scalability and Protection

Dive deep into the four essential rate limiting algorithms—Fixed Window, Sliding Log, Sliding Window Counter, and Token Bucket—to protect your APIs from abuse.

AuthorBy The APIGate TeamOct 21, 20253 min read

Introduction

API Rate Limiting is a defensive mechanism that controls the number of requests a user or client can make to an API over a specific period. This prevents API abuse, protects against DoS/DDoS attacks, and ensures fair resource allocation. The effectiveness of your rate limit depends on the underlying algorithm you choose.

The Top 4 Rate Limiting Algorithms

1. Fixed Window Counter ⏱️

  • **How it Works:** Divides time into fixed, non-overlapping intervals (e.g., 60 seconds). A counter increments with each request. Once the limit is hit, all subsequent requests until the end of the window are rejected.
  • **Pro:** Simple and easy to implement.
  • **Con (The "Burst" Problem):** Allows a double-capacity burst of requests near the window edges (e.g., limit of 100/min. A client can send 100 requests at 0:59 and 100 more at 1:00).

2. Sliding Log 📜

  • **How it Works:** For each client, the Gateway keeps a timestamped log of all successful requests. For a new request, it counts all timestamps within the last *X* seconds (the window). If the count exceeds the limit, the request is denied.
  • **Pro:** Extremely accurate and avoids the Fixed Window burst problem.
  • **Con:** High memory consumption, as every request's timestamp must be stored and queried. Not suitable for massive scale.

3. Sliding Window Counter (Hybrid) 📊

  • **How it Works:** A hybrid approach using two fixed windows (the current and the previous). It smoothly estimates the request count using a weighted average. For example, if 70% of the previous window has passed, it credits the current window with 30% of the previous count.
  • **Pro:** Good compromise between accuracy and memory efficiency. Smoother rate limiting than the Fixed Window.
  • **Con:** Still an approximation, which can slightly over- or under-limit the traffic.

4. Token Bucket (Best for Burst Tolerance) 🪙

  • **How it Works:** A virtual bucket holds a fixed number of "tokens." Requests consume one token, and tokens are added to the bucket at a fixed refill rate. If the bucket is empty, the request is denied.
  • **Pro:** Allows for controlled bursts (up to the size of the bucket) while limiting the long-term average rate to the refill rate. Excellent for managing short, spikey loads.
  • **Con:** Requires tuning two parameters: bucket size and refill rate.

Conclusion

For most high-traffic API Gateways, the **Token Bucket** and **Sliding Window Counter** offer the best balance of performance, accuracy, and resource efficiency. Choose the algorithm that best aligns with your application's tolerance for bursts and your acceptable level of memory usage.

Share this post:

Explore our API security tools. Learn more at APIGate.