Skip to main content

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:
  1. Windows are defined by a start time and period duration
  2. At the start of each window, rate tokens are added (up to capacity)
  3. Requests consume tokens immediately
  4. When the window ends, a new window begins and tokens are added again

Start Time Configuration

The start parameter determines when windows begin:
  • Specified start: All windows align to this timestamp
  • Random start (default): Randomly chosen between 0 and period to distribute load
Without a specified start, each rate limit instance will have a random window start to prevent all clients from flooding requests at the same time (avoiding thundering herd problems).

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

From src/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 from src/shared.ts:
Key points:
  • elapsedWindows: Number of complete windows since last update
  • Tokens added: rate × elapsedWindows, capped at capacity
  • ts updated to the start of the current window
  • retryAfter: 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
Fixed window can allow up to 2× the rate at window boundaries. If this is a concern, consider using token bucket instead.

Avoiding Thundering Herd

When rate limits reset, many clients may retry simultaneously. To prevent this:

1. Use Random Start (Default)

Don’t specify start 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