What’s New in V3
V3 introduces significant architectural changes that provide much greater flexibility and control over rate limiting in your Medusa application. The main focus of this release is to give developers complete freedom in how they implement and customize rate limiting mechanisms.Key Changes
Breaking Changes:- The
defaultRateLimit
middleware has been removed - Global configuration has been removed
- Introduction of the
RateLimit
class for programmatic rate limiting - Built-in
ipRateLimit
middleware for common IP-based rate limiting - Support for custom identifiers beyond IP addresses
- More granular control over rate limiting logic
Installation
To install the V3 prerelease version:The RateLimit Class
The core of V3 is the newRateLimit
class that gives you programmatic control over rate limiting. This class integrates directly with Medusa’s cache service and allows you to implement custom rate limiting logic.
Basic Usage
src/api/middlewares.ts
Available Methods
TheRateLimit
class provides three main methods:
limit(identifier: string)
Checks and increments the rate limit for a given identifier.
success
: Whether the request is within the rate limitremaining
: Number of requests remaining in the current windowlimit
: The configured limit for the window
getRemaining(identifier: string)
Gets the remaining requests for a specific identifier without incrementing the counter.
reset(identifier: string)
Resets the request count for a specific identifier.
Configuration Options
Option | Type | Default | Description |
---|---|---|---|
limit | Number | 100 | Maximum number of requests in the time window |
window | Number | 900 | Time window in seconds (default: 15 minutes) |
prefix | String | "rl" | Prefix for cache keys |
Built-in IP Rate Limiting
For the common use case of IP-based rate limiting, V3 includes a ready-to-useipRateLimit
middleware like in V2:
src/api/middlewares.ts
ipRateLimit
middleware automatically:
- Extracts the client IP address
- Sets appropriate rate limit headers (
X-RateLimit-Limit
,X-RateLimit-Remaining
) - Returns a 429 status when the limit is exceeded
options
object with the same options as the RateLimit
class (limit
, window
, prefix
).
Advanced Usage Examples
User-Based Rate Limiting
Rate limit based on authenticated user ID:Multiple rate limiters
You can create multiple rate limiters with different options:Migration from V2
Before (V2)
After (V3)
Why the Change?
The V2 approach, while simple, was limited in flexibility. The global configuration system made it difficult to have different rate limiting strategies for different parts of your application. V3 addresses these limitations by:- Removing Global State: No more global configuration that could cause unexpected behavior
- Enabling Custom Identifiers: Rate limit by user ID, API key, or any custom identifier
- Providing Programmatic Control: Direct access to rate limiting logic for complex scenarios
- Maintaining Simplicity: The
ipRateLimit
middleware covers the most common use case with minimal setup