Overview
The fixed window algorithm limits requests by granting a fixed number of tokens at the start of each time window. Tokens accumulate up to a maximum capacity and are consumed by requests.A fixed window rate limit grants tokens in bulk at the start of each fixed window of time. The
rate determines how many tokens are granted, the period defines the window duration, and capacity sets the maximum tokens that can accumulate.How It Works
Token Grants at Window Boundaries
Tokens are granted all at once when windows reset:- Windows are defined by a
starttime andperiodduration - At the start of each window,
ratetokens are added (up tocapacity) - Requests consume tokens immediately
- When the window ends, a new window begins and tokens are added again
Start Time Configuration
Thestart parameter determines when windows begin:
- Specified start: All windows align to this timestamp
- Random start (default): Randomly chosen between 0 and
periodto distribute load
Visual Explanation
Here’s how tokens are granted over time:Tokens are granted in bulk at window boundaries. Between windows, tokens can only decrease (be consumed), never increase.
Configuration
Type Definition
Fromsrc/shared.ts:
Parameters
rate (required)
The number of tokens granted at the start of each window.
period (required)
The window duration in milliseconds. Use the provided constants:
capacity (optional)
Maximum tokens that can accumulate. Defaults to rate.
start (optional)
Timestamp in UTC milliseconds for when windows start. If not provided, a random time is chosen.
maxReserved (optional)
Maximum tokens that can be reserved into the future.
shards (optional)
Number of shards for high-throughput scenarios. See Scaling with Shards.
Real Code Examples
Free Trial Signups
Daily API Quota
High-Throughput LLM Requests
Per-User Hourly Limits
Implementation Details
The fixed window calculation fromsrc/shared.ts:
elapsedWindows: Number of complete windows since last update- Tokens added:
rate × elapsedWindows, capped at capacity tsupdated to the start of the current windowretryAfter: Time until enough windows pass to have sufficient tokens
Use Cases
Scheduled Resets
When you want quotas to reset at specific times:Burst Allowance
Allow users to consume their entire quota in a burst:External API Alignment
Match external API rate limit windows:Global Singleton Limits
Limit total system-wide actions:Advantages
- Predictable resets: Users know exactly when their quota refreshes
- Simple to understand: “100 per hour” means exactly that
- Burst-friendly: Users can consume their entire quota immediately
- Aligned windows: Multiple users share the same window boundaries
Limitations
- Boundary bursts: Users can consume 2× rate at window boundaries
- Example: Use 100 at 11:59 AM, then 100 more at 12:00 PM
- Less smooth: Token availability changes in steps, not continuously
- Thundering herd: Without random start, all users retry at the same time
Avoiding Thundering Herd
When rate limits reset, many clients may retry simultaneously. To prevent this:1. Use Random Start (Default)
Don’t specifystart to get automatic randomization:
2. Add Jitter to Retries
3. Use Reservations
Reserve capacity ahead of time to avoid retry storms:Next Steps
Token Bucket
Learn about the alternative token bucket strategy
Basic Usage
Start using fixed window rate limiting
Scaling with Shards
Handle high throughput scenarios
Reservations
Reserve capacity to avoid retry storms