Strategies
Sliding Window
Smooth rate limiting with per-request timestamp tracking.
The Sliding Window strategy tracks individual request timestamps and counts requests within a rolling time window. This eliminates the boundary burst problem of Fixed Window.
How It Works
Time ──────────────────────────────────────────────>
←────── window (60s) ──────→
• • • • • • • • • • (10 requests)
↑ ↑
oldest nowInstead of resetting at fixed intervals, the window "slides" with time. Only requests within the last windowMs milliseconds are counted.
Configuration
Options for the Sliding Window strategy:
Prop
Type
Usage
import { slidingWindow } from '@ratelock/local'
const limiter = await slidingWindow({
limit: 100,
windowMs: 60_000,
})
const result = await limiter.check('user:123')
// { allowed: true, remaining: 99, reset: 1716000060000, windowStart: 1716000000000, windowEnd: 1716000060000 }Result Fields
The check result returned by the Sliding Window limiter:
Prop
Type
Pros and Cons
Pros:
- No boundary burst problem
- More accurate rate limiting
- Smooth request distribution
Cons:
- Higher memory usage (stores individual timestamps)
- Slightly slower than Fixed Window
- Requires cleanup of expired entries
When to Use
- When you need precise rate limiting
- When users exploit Fixed Window boundaries
- APIs with strict rate limit requirements
- When accuracy matters more than memory usage
How is this guide?
Last updated on