Sliding Window Counter Algorithm - Rate Limiter

The Sliding Window Counter Algorithm balances simplicity and precision by dividing a long time window into smaller intervals. It continuously counts requests across overlapping intervals, allowing for smoother traffic handling without logging every individual request. It’s especially useful when you want to manage burst traffic effectively but need to avoid the memory overhead of detailed logging.

How the Sliding Window Counter Algorithm Works

  1. Time Window Division:
    • The 60-second window is divided into smaller intervals (e.g., 6 intervals of 10 seconds each).
  2. Counting Requests:
    • The algorithm tracks requests within each interval. Requests are grouped based on when they arrive.
  3. Sliding Effect:
    • As time progresses, older intervals slide out of the window while newer ones are added.
  4. Summing Requests:
    • The algorithm continuously sums the requests across all active intervals, ensuring that the total request count reflects the last 60 seconds.

Example: Sliding Window Counter in Action

Imagine a user is allowed to make 100 API requests per 60 seconds.

Interval Breakdown

  • Interval 1 (0-10s): 40 requests.
  • Interval 2 (10-20s): 30 requests.
  • Interval 3 (20-30s): 15 requests.
  • Interval 4 (30-40s): 10 requests.
  • Interval 5 (40-50s): 5 requests.
  • Interval 6 (50-60s): 20 requests.

Sliding Effect Explained

  1. At 30 seconds:
    • The system adds up requests from Intervals 1, 2, and 3: 40 + 30 + 15 = 85.
  2. At 40 seconds:
    • Interval 1 (0-10s) slides out, and Interval 4 (30-40s) is added: 30 + 15 + 10 = 55.
  3. At 60 seconds:
    • The window includes Intervals 4, 5, and 6: 10 + 5 + 20 = 35.

The sliding nature ensures that the count is continuously updated and reflects the most recent 60 seconds of traffic.

βœ… No

❌ Yes

Snapshot at 60s

Total: 35 Requests in Intervals 4, 5, 6

Snapshot at 40s

Total: 55 Requests in Intervals 2, 3, 4

Snapshot at 30s

Total: 85 Requests in Intervals 1, 2, 3

Sliding Window - Last 60s

0-10s: 40 Requests

10-20s: 30 Requests

20-30s: 15 Requests

30-40s: 10 Requests

40-50s: 5 Requests

50-60s: 20 Requests

πŸš€ Start Request

πŸ•’ Split into 10s Intervals

πŸ“Š Track Requests in Each Interval

Slide to 40s

Slide to 50s

Total Requests > 100?

πŸ‘ Allow Request

🚫 Reject Request

Explanation of the Flow

  1. Start Request:
    • When a request is made, it is counted in the relevant interval.
  2. Divide into Intervals:
    • The 60-second window is split into 6 intervals of 10 seconds each.
  3. Snapshots:
    • At 30 seconds, the count includes requests from Intervals 1, 2, and 3.
    • At 40 seconds, Interval 1 slides out, Interval 4 is added, and the count is updated.
    • At 60 seconds, the window includes Intervals 4, 5, and 6.
  4. Limit Check:
    • If the total count exceeds 100, requests are rejected; otherwise, they are allowed.

Benefits of the Sliding Window Counter Algorithm

  1. Efficient Memory Use:
    • It tracks counts for intervals, using less memory than the Sliding Window Log.
  2. Smoother Traffic Handling:
    • It manages burst traffic better than the Fixed Window, avoiding sudden drops or spikes in requests.
  3. Continuous Updates:
    • It always represents the most recent 60 seconds of traffic, making it more adaptive to real-time changes.

Use Cases

  1. API Rate Limiting:
    • Balances precision and efficiency, making it suitable for API rate limiting.
  2. Microservices:
    • Helps microservices handle a steady flow of requests while managing burst traffic.
  3. Web Applications:
    • Provides smoother rate limiting for user interactions, preventing sudden rejections.

FAQs

Q1: How does it differ from the Fixed Window Algorithm?

  • The Sliding Window Counter divides the total window into smaller intervals, providing more adaptive rate limiting than the Fixed Window, which resets periodically.

Q2: How does it handle traffic bursts?

  • By counting requests in overlapping intervals, it ensures a more accurate count and avoids sudden traffic bursts.

Q3: Is it memory-efficient?

  • Yes, it only maintains counts for intervals, making it more memory-efficient than the Sliding Window Log.

Summary

The Sliding Window Counter Algorithm provides a smoother and more efficient way to manage request rates. It continuously updates the count over smaller intervals, ensuring better control over traffic while using less memory than the Sliding Window Log.

Clap here if you liked the blog