Sliding Window Counter Algorithm - Rate Limiter

Sliding Window Counter Algorithm

The Sliding Window Counter Algorithm is an improved rate-limiting technique that divides a long time window into smaller intervals to manage requests more effectively. It balances simplicity and precision by continuously counting requests across overlapping intervals.

It is useful when you want to avoid sudden bursts of traffic without tracking every request in detail, as in the Sliding Window Log. Instead, it tracks requests in smaller intervals (e.g., 10 seconds), making it efficient and better at handling traffic spikes.


How It Works

  1. Time Window Division:
    • The algorithm splits a 60-second time window into smaller intervals (e.g., 6 intervals of 10 seconds each).
  2. Counting Requests:
    • Within each interval, the system counts the number of requests.
  3. Sliding Effect:
    • As time progresses, new intervals are added while older intervals β€œslide out” of the window.
  4. Summing Requests:
    • At any given moment, the system sums the requests in all active intervals and checks if they exceed the allowed limit.

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): User sends 40 requests.
  • Interval 2 (10-20s): User sends 30 requests (Total so far: 70).
  • Interval 3 (20-30s): User sends 15 requests (Total so far: 85).
  • Interval 4 (30-40s): User sends 10 requests (Total so far: 95).
  • Interval 5 (40-50s): User sends 5 requests (Total so far: 100).
  • Interval 6 (50-60s): User sends 10 requests (Total so far: 110, exceeding the limit).

Sliding Effect Explained

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

The sliding nature ensures that the count is updated continuously, always representing the last 60 seconds.


Sliding Window Counter Flow

βœ… No

❌ Yes

Snapshot at 50s

Total: 30 Requests

πŸ“Š Active Sliding Window - Last 60s

Interval 5: 40-50s

5 Requests

πŸ“Š Active Sliding Window - Last 60s

Interval 4: 30-40s

10 Requests

πŸ“Š Active Sliding Window - Last 60s

Interval 3: 20-30s

15 Requests

Interval 2: 10-20s

30 Requests

Interval 1: 0-10s

40 Requests

Snapshot at 40s

Total: 55 Requests

Snapshot at 30s

Total: 85 Requests

πŸš€ Start Request

πŸ•’ Split into 10s Intervals

πŸ“Š Track Requests in Each Interval

Sliding Forward to 40s

Sliding Forward to 50s

Total Requests > 100?

πŸ‘ Allow Request

🚫 Reject Request

Explanation of the Flow

  1. Start Request:
    • When a request is made, it is counted within the appropriate interval.
  2. Divide into Intervals:
    • The 60-second window is divided into 6 intervals of 10 seconds each.
  3. Snapshots:
    • At 30 seconds, the system adds up requests in 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:
    • The total count is checked against the allowed limit (e.g., 100 requests).
    • If the total exceeds the limit, additional requests are rejected.

Why Use the Sliding Window Counter Algorithm?

  1. Efficient Memory Use:
    • It tracks counts for each interval rather than logging every individual request, using less memory than the Sliding Window Log.
  2. Smooth Traffic Handling:
    • The sliding effect ensures that bursts of traffic are managed more smoothly compared to the Fixed Window, which resets after each fixed period.
  3. Continuous Updates:
    • The count is continuously updated to reflect the most recent 60 seconds, providing better real-time control.

Use Cases

  1. API Rate Limiting:
    • Effective for managing burst traffic in APIs without using excessive memory.
  2. Microservices:
    • Keeps microservices stable by controlling request flow smoothly.
  3. Web Applications:
    • Handles user traffic spikes without sudden rejections, providing a better user experience.

FAQs

Q1: How does the Sliding Window Counter differ from the Fixed Window?

  • The Sliding Window Counter divides the total window into smaller intervals and continuously sums requests, providing smoother traffic handling than the Fixed Window, which resets periodically.

Q2: How does it prevent traffic bursts?

  • By summing requests across overlapping intervals, the algorithm maintains a consistent count, avoiding sudden traffic spikes.

Q3: Why are smaller intervals used?

  • Smaller intervals provide more frequent updates, making the rate limiter more adaptive to changing traffic patterns while maintaining efficiency.

Q4: Is this method memory-efficient?

  • Yes, the Sliding Window Counter uses less memory than the Sliding Window Log, as it only maintains counts for intervals instead of individual timestamps.

Summary

The Sliding Window Counter Algorithm effectively manages request rates by dividing the time window into smaller intervals, ensuring smoother traffic control than the Fixed Window while using less memory than the Sliding Window Log. This makes it ideal for systems that require efficient handling of burst traffic without tracking each request in detail.

Clap here if you liked the blog