Skip to main content

API Rate Limit Planner

v1.0.0

Design rate-limit tiers — fixed window, sliding window, token bucket; per-key vs per-IP.

Rate Limit Configuration
# Rate Limiting Configuration

## Parameters
- Limit: 100 requests
- Window: 60 seconds
- Algorithm: sliding-window
- Scope: per IP + API key

## Response Headers
```
X-RateLimit-Limit: 100
X-RateLimit-Remaining: <remaining>
X-RateLimit-Reset: <unix-timestamp>
Retry-After: <seconds> (on 429 only)
```

## 429 Response
```json
{
  "type": "https://api.example.com/problems/rate-limited",
  "title": "Too Many Requests",
  "status": 429,
  "detail": "Rate limit of 100 req/60s exceeded.",
  "retryAfter": 60
}
```

## Implementation (Node.js + Redis)
```ts
import { RateLimiterRedis } from "rate-limiter-flexible";

const rateLimiter = new RateLimiterRedis({
  storeClient: redisClient,
  points: 100,      // requests
  duration: 60,     // seconds
  keyPrefix: "rl",
  // For sliding window behavior:
  blockDuration: 0,
});

export async function rateLimit(key: string) {
  try {
    await rateLimiter.consume(key);
  } catch {
    throw new TooManyRequestsError();
  }
}
```