Configuration
Table of Contents
- Quick Start
- Core Options
- Algorithm
- Store
- Key Generator
- Middleware Behavior
- Route-Level Configuration
- Dynamic Limits
- Scenario Presets
- Redis Configuration
- Related Documents
Quick Start
The two required ideas are the time window and the maximum number of allowed requests. Other options refine the algorithm, storage backend, key generation, middleware behavior, and route overrides.
Core Options
Algorithm
1. Sliding Window
Use sliding window when you need the most precise rolling-window behavior.
Recommended for:
- Login and authentication flows
- Payment or sensitive actions
- Per-user API limits where fairness matters
2. Fixed Window
Use fixed window when a coarse window is acceptable and you want a simple hot path.
Fixed-window counters reset at window boundaries, so requests can cluster around the boundary.
3. Token Bucket
Use token bucket when controlled bursts are acceptable and capacity refills over time.
Compared with sliding window:
4. Leaky Bucket
Use leaky bucket when the goal is to smooth traffic to a backend.
Leaky bucket allows requests while the bucket has capacity and gradually drains state over time.
Store
Memory
Use Memory for single-process services, tests, local development, and low-complexity deployments.
Redis Connection String
The library creates and owns the Redis client in this form. Short-lived scripts and services should call await limiter.close().
RedisStore Instance
The caller owns the Redis client by default. Set ownsClient: true if the store should close it.
CacheHubStore Atomic Backend
Use CacheHubStore when you want cache-hub atomic state primitives while keeping flex-rate-limit as the algorithm and middleware layer.
Key Generator
The key generator controls the isolation boundary of a quota.
Common key strategies:
Middleware Behavior
skip
Use skip for routes that should not be counted, such as health checks. Do not use skip to make allowlisted IPs bypass limits unless that is explicitly intended.
Custom Handler
Response Outcome Rollback
skipSuccessfulRequests and skipFailedRequests use rollback metadata internally. Public check() results do not expose that metadata unless trackRollback: true is requested.
Route-Level Configuration
Route-level overrides are useful when the same application needs different limits for login, query, admin, and write operations.
Dynamic Limits
max can be a function:
Use dynamic limits for plan-based quotas, tenant-specific limits, or internal service accounts.
Scenario Presets
Redis Configuration
For cluster and sentinel examples, see Storage Backends.