Manage Multiple Limiters (Recommended)
Learn the recommended pattern for defining and applying different rate limits to various API endpoints, user tiers, or specific actions within your application using oss-ratelimit.
Client & Registry Management
Managing multiple rate limiters (e.g., for different API endpoints or user tiers) and efficiently handling Redis connections is crucial. oss-ratelimit provides a powerful registry system for this.
1. Initialize the Registry (initRateLimit)
Create a registry instance, optionally providing default Redis options. Define the names your limiters will use with a type union for type safety.
2. Initialize Limiters (Optional but Recommended)
You can eagerly initialize all defined limiters on application startup using initializeLimiters. This ensures Redis connections are established and configurations are validated early.
Alternatively, limiters will be initialized automatically (and lazily) the first time registry.register(name) or registry.get(name) is called for a specific name if not already initialized.
3. Accessing Limiters
Use registry.get(name) to retrieve an initialized limiter instance.
Reliably getting the real client IP behind proxies requires checking specific headers. Use a dedicated utility function for this.
You can also use the helper functions getInitializedLimiter or createLimiterAccessor for convenience:
4. Redis Client Reuse
The registry automatically manages and reuses Redis client connections based on the configuration (RedisOptions or envRedisKey). This prevents creating excessive connections to your Redis server. You can inspect which client a limiter is using via the limiterRegister event or programmatically if needed (though usually not required).
Advanced Usage
Blocking (block)
Wait until a request is allowed, up to a configurable maximum time.
Resetting Limits (reset)
Manually clear the rate limit count for a specific identifier. Useful for testing or specific application logic.
Checking Status (getStats, check)
Query the current state of the rate limit for an identifier without consuming a request token/count.
Configuration Options
Configure the Ratelimit instance or registry registration:
| Prop | Type | Default |
|---|---|---|
silent? | boolean | false |
failOpen? | boolean | false |
ephemeralCacheTTL? | number | 60000 |
ephemeralCache? | boolean | true |
timeout? | number | 1000 |
analytics? | boolean | false |
prefix? | string | oss-ratelimit |
limiter? | FixedWindowOptions | SlidingWindowOptions | TokenBucketOptions | - |
redis? | RedisClientType | RedisOptions | { envRedisKey: string } | - |
Redis Options
Passed directly to redis.createClient or used by getRedisClient:
| Prop | Type | Default |
|---|---|---|
reconnectStrategy? | | - |
connectTimeout? | number | 5000 |
tls? | boolean | - |
database? | number | - |
password? | string | - |
username? | string | - |
port? | number | - |
host? | string | - |
url? | string | - |
Concepts & Algorithms
Understand the core concepts behind rate limiting (like token bucket, sliding window) and learn about the specific algorithms implemented within oss-ratelimit.
Nextjs Integration with Oss Ratelimit Redis
A step-by-step guide on how to seamlessly integrate oss-ratelimit using Redis as a storage backend within your Next.js application, including middleware examples.