Rate Limiter Middleware
Prerequisites & Dependencies
- Node.js 18+ with Express (or FastAPI, Flask, Go Gin)
npm i express-rate-limit (Token Bucket) or npm i rate-limit-redis (distributed Sliding Window)
- Understanding of API throughput requirements and SLA constraints
Execution Steps
- Determine the rate limit: e.g., 100 requests per 15 minutes per IP, or 10 requests per minute per API key
- Choose the algorithm:
- Token Bucket (fixed window, allows burst up to bucket size)
- Sliding Window (counts requests in a moving time window, more precise)
- Install and configure the middleware with
max, windowMs, standardHeaders: true, legacyHeaders: false
- Apply the middleware globally or per-route:
app.use('/api', limiter) or router.get('/strict', limiter, handler)
- Add retry-after header on limit exceed and log exceeded events for SLA monitoring
- Tune limits based on load testing (e.g.,
k6, artillery) and observed traffic patterns
// Express Token Bucket rate limiter
const rateLimit = require('express-rate-limit');
const globalLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
standardHeaders: true, // Return rate limit info in `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
message: 'Too many requests, please try again later.',
});
app.use('/api', globalLimiter);
app.get('/api/data', (req, res) => res.json({ data: 'hello' }));
1---2name: rate-limiter-middleware3description: Build API rate-limiting middleware using Token Bucket or Sliding Window algorithms.4---56# Rate Limiter Middleware78## Prerequisites & Dependencies9- Node.js 18+ with Express (or FastAPI, Flask, Go Gin)10- `npm i express-rate-limit` (Token Bucket) or `npm i rate-limit-redis` (distributed Sliding Window)11- Understanding of API throughput requirements and SLA constraints1213## Execution Steps141. Determine the rate limit: e.g., 100 requests per 15 minutes per IP, or 10 requests per minute per API key152. Choose the algorithm: 16 - **Token Bucket** (fixed window, allows burst up to bucket size)17 - **Sliding Window** (counts requests in a moving time window, more precise)183. Install and configure the middleware with `max`, `windowMs`, `standardHeaders: true`, `legacyHeaders: false`194. Apply the middleware globally or per-route: `app.use('/api', limiter)` or `router.get('/strict', limiter, handler)`205. Add retry-after header on limit exceed and log exceeded events for SLA monitoring216. Tune limits based on load testing (e.g., `k6`, `artillery`) and observed traffic patterns2223```javascript24// Express Token Bucket rate limiter25const rateLimit = require('express-rate-limit');2627const globalLimiter = rateLimit({28 windowMs: 15 * 60 * 1000, // 15 minutes29 max: 100, // limit each IP to 100 requests per windowMs30 standardHeaders: true, // Return rate limit info in `RateLimit-*` headers31 legacyHeaders: false, // Disable the `X-RateLimit-*` headers32 message: 'Too many requests, please try again later.',33});3435app.use('/api', globalLimiter);36app.get('/api/data', (req, res) => res.json({ data: 'hello' }));37```