Available Strategies
The Convex Rate Limiter supports two proven rate limiting algorithms:- Token Bucket - Continuously adds tokens over time, allowing smooth rate limiting with burst capacity
- Fixed Window - Grants tokens in bulk at fixed intervals, ideal for scheduled resets
Token Bucket vs Fixed Window
When to Use Token Bucket
The token bucket approach provides guarantees for overall consumption via therate per period at which tokens are added, while also allowing unused tokens to accumulate (like “rollover” minutes) up to some capacity value.
Best for:
- Smooth, continuous rate limiting
- LLM API rate limits (tokens are added continuously)
- User messaging (allow bursts if they haven’t been active)
- Any scenario where you want gradual token replenishment
When to Use Fixed Window
The fixed window approach grants tokens all at once, everyperiod milliseconds. It similarly allows accumulating “rollover” tokens up to a capacity (defaults to the rate).
Best for:
- Scheduled resets (e.g., daily quotas)
- Aligning with external API windows
- When you want predictable reset times
- Burst allowance at specific intervals
For fixed window, you can specify a custom
start time if you want the period to reset at a specific time of day. By default it will be random to help space out requests that are retrying.Key Differences
| Feature | Token Bucket | Fixed Window |
|---|---|---|
| Token addition | Continuous (calculated per millisecond) | Bulk (at window boundaries) |
| Rate smoothing | Excellent - perfectly smooth | Moderate - can have bursts at boundaries |
| Predictable resets | No - tokens always adding | Yes - resets at fixed intervals |
| Best use case | Smooth traffic, LLM APIs | Scheduled quotas, daily limits |
start parameter | Not applicable (always null) | Optional timestamp for window alignment |
Configuration Options
Both strategies share common configuration parameters:Required Parameters
Optional Parameters
capacity
The maximum number of tokens that can accumulate. Defaults to rate.
Higher capacity allows more burst traffic but maintains the same long-term rate limit.
maxReserved
The maximum number of tokens that can be reserved ahead of time when using the reserve feature.
shards
Number of shards to use for handling high throughput. See Scaling with Shards for details.
start (Fixed Window Only)
Timestamp in UTC milliseconds for when the first window starts. All subsequent windows are calculated from this point.
If
start is not provided for fixed window, it will be a random number between 0 and period to help distribute load from retrying clients.Type Definitions
The rate limit configurations are defined using Convex validators:src/shared.ts
Choosing the Right Strategy
Use this decision tree:-
Do you need tokens to be added continuously?
- Yes → Token Bucket
- No → Continue to #2
-
Do you need predictable reset times?
- Yes → Fixed Window
- No → Token Bucket (more flexible)
-
Are you rate limiting an external API?
- LLM/streaming APIs → Token Bucket
- APIs with daily/hourly quotas → Fixed Window
-
Do you want the smoothest possible rate limiting?
- Yes → Token Bucket
- Don’t care → Either works
Next Steps
Token Bucket Details
Learn how token bucket works in detail
Fixed Window Details
Learn how fixed window works in detail