-
Notifications
You must be signed in to change notification settings - Fork 2
Rate Limiting Algorithms in ThrottleX
ThrottleX is designed with multiple rate limiting algorithms to suit different use cases. This page provides a detailed overview of the algorithms currently implemented in ThrottleX, as well as those planned for future releases.
Concept: Fixed Window Rate Limiting allows a fixed number of requests during a defined time window. This approach is simple but effective in handling sudden spikes in traffic.
How It Works: Every time a request is made, the current window is checked. If the request count exceeds the set limit, subsequent requests are blocked until the next window starts. The window is static, meaning it resets at defined intervals.
Pros:
- Easy to implement and understand.
- Effective for limiting sudden bursts of requests.
Cons:
- Susceptible to the "boundary effect," where many requests made near the end of a window could also make many requests in the next window.
Example:
limiter, err := ratelimiter.NewFixedWindowLimiter(memStore, 10, time.Minute)
In this example, the rate limiter allows 10 requests per minute for each key.
Concept: Sliding Window Rate Limiting provides more granular rate limiting by maintaining a count that "slides" over time rather than resetting abruptly at each window boundary.
How It Works: Each request is recorded with its timestamp. The rate limiter keeps track of requests within a moving window to decide whether the current request should be allowed. It provides a more balanced approach, avoiding spikes that occur with fixed windows.
Pros:
- Avoids the boundary effect seen in fixed window limiting.
- Provides a more even rate-limiting mechanism over time.
Cons:
- Requires more storage and computation as the system needs to keep track of timestamps.
Example:
limiter, err := ratelimiter.NewSlidingWindowLimiter(memStore, 10, time.Minute)
In this example, the rate limiter allows 10 requests per minute, sliding over time.
Concept: The Token Bucket algorithm allows for bursts of activity while enforcing an average rate limit over time. Requests are processed as long as there are tokens in the bucket.
How It Works: Tokens accumulate in the bucket at a steady rate until the bucket reaches its capacity. Each request consumes a token, and if no tokens are available, the request is denied. The token bucket algorithm is useful for scenarios where you want to allow short bursts of activity but control the overall request rate.
Pros:
- Allows bursts while controlling the average request rate.
- Ideal for variable traffic loads.
Cons:
- Complexity in implementation compared to fixed and sliding windows.
Example:
limiter, err := ratelimiter.NewTokenBucketLimiter(memStore, 5, 1.0)
In this example, the rate limiter has a bucket capacity of 5 tokens and refills at 1 token per second.
ThrottleX aims to support a wide variety of rate limiting policies in the future. Here's a glimpse of what we're planning to include:
- Prioritized Rate Limiting: Assign priority to certain users, ensuring that VIP clients always receive preferential access.
- Request Token Policy: Allow clients to present special tokens to bypass or increase rate limits.
- Exponential Backoff Rate Limiting: Increase the wait time exponentially for clients who continuously exceed limits.
- Geographic Rate Limiting: Limit requests based on geographic regions, providing customized rate limits.
- Hierarchical Rate Limiting: Apply rate limits hierarchically across different levels, like user groups.
- Dynamic Rate Limiting: Adjust rate limits dynamically based on server load or other conditions.
- Request Quota Policy: Manage quotas either on a fixed or rolling window basis for long-term control.
- Concurrency Limit: Limit the number of concurrent requests.
- Leaky Bucket Algorithm: Similar to the token bucket, but ensures a steady outflow of requests, preventing sudden spikes.
These planned features will provide a Swiss Army Knife of rate-limiting strategies to fit every use case, from high-priority clients to steady load regulation.
ThrottleX currently provides robust and effective rate limiting mechanisms, with Fixed Window, Sliding Window, and Token Bucket algorithms ready to use. We're committed to making ThrottleX a comprehensive solution for all your rate limiting needs, and we have exciting plans for future policies to make your services even more efficient and fair.
Stay tuned for updates, and contribute to ThrottleX if you want to be part of this journey!