Rate Limiting Strategies

RouteMQ supports multiple rate limiting algorithms, each with different characteristics and use cases. Choose the right strategy based on your application's needs.

Available Strategies

1. Sliding Window (Default)

Most accurate and recommended for most use cases

sliding_window = RateLimitMiddleware(
    max_requests=100,
    window_seconds=60,
    strategy="sliding_window"
)

How It Works

The sliding window algorithm maintains a continuous window that moves with time:

Time:     0    15    30    45    60    75    90
Window:   |-------- 60s --------|
          |           |-------- 60s --------|
          |                      |-------- 60s --------|
  • Tracks exact timestamps of each request

  • Continuously slides the time window

  • Provides the most accurate rate limiting

  • Prevents burst traffic at window boundaries

Implementation Details

Uses Redis sorted sets to store request timestamps:

Pros and Cons

Pros:

  • Most accurate rate limiting

  • Smooth traffic distribution

  • No burst at window boundaries

  • Fair for all users

Cons:

  • Higher memory usage (stores all timestamps)

  • More Redis operations

  • Slightly more complex

Use Cases

2. Fixed Window

Simple and memory-efficient

How It Works

Fixed window algorithm resets counters at fixed intervals:

  • Divides time into fixed windows

  • Resets counter at window boundaries

  • Simple counter increment/decrement

  • Memory efficient

Implementation Details

Uses Redis strings with expiration:

Pros and Cons

Pros:

  • Very memory efficient

  • Simple implementation

  • Fast operations

  • Predictable reset times

Cons:

  • Allows burst traffic at window boundaries

  • Less accurate than sliding window

  • Can allow 2x limit in worst case

Burst Traffic Issue

Use Cases

3. Token Bucket

Allows burst traffic with sustained rate limiting

How It Works

Token bucket algorithm allows controlled bursts:

  • Bucket starts full of tokens

  • Each request consumes one token

  • Tokens refill at a steady rate

  • Allows bursts when bucket is full

Implementation Details

Uses Redis hash to store bucket state:

Pros and Cons

Pros:

  • Allows controlled burst traffic

  • Smooth average rate limiting

  • Good user experience

  • Handles irregular traffic well

Cons:

  • More complex implementation

  • Can allow temporary rate spikes

  • Requires careful tuning

Configuration Examples

Strategy Comparison

Performance Comparison

Strategy
Memory Usage
CPU Usage
Redis Ops
Accuracy

Sliding Window

High

Medium

High

Highest

Fixed Window

Low

Low

Low

Medium

Token Bucket

Medium

Medium

Medium

High

Traffic Pattern Suitability

Steady Traffic

Bursty Traffic

High Volume Traffic

Choosing the Right Strategy

Decision Matrix

Use Sliding Window when:

  • Accuracy is critical

  • Traffic should be evenly distributed

  • Burst prevention is important

  • Memory usage is not a concern

Use Fixed Window when:

  • High performance is required

  • Memory efficiency is important

  • Occasional bursts are acceptable

  • Simple implementation is preferred

Use Token Bucket when:

  • Burst traffic is expected

  • User experience is important

  • Traffic patterns are irregular

  • Some flexibility is needed

Application-Specific Recommendations

REST APIs

IoT Data Ingestion

Public Endpoints

Critical Systems

Advanced Strategy Configuration

Hybrid Approach

Combine multiple strategies for different endpoints:

Dynamic Strategy Selection

Select strategy based on runtime conditions:

Testing Different Strategies

Performance Testing

Accuracy Testing

Next Steps

Last updated