
Rate limiting is a technique used to control how many requests a user can make to your API within a specific time. It helps protect your API from abuse, ensures fair usage, and improves system stability.
Rate limiting restricts the number of API requests.
Example:
100 requests per minute per userIf exceeded:
429 Too Many RequestsWithout rate limiting:
With rate limiting:
Limit requests in a fixed time window.
Example:
100 requests per minuteSimple but can cause spikes.
Tracks requests over a rolling time window.
Better accuracy:
Last 60 seconds → max 100 requestsUsers get tokens to make requests.
Example:
Bucket size: 100
Refill: 10 tokens/secSmooth traffic handling
Requests are processed at a fixed rate.
Good for:
Flow:
Request → Check limit → Allow / Block → Respondconst rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 100, // limit each IP
message: "Too many requests"
});
app.use('/api/', limiter);Send headers to inform users:
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 80
X-RateLimit-Reset: 60Always use when:
Rate limiting plays a key role in API monetization:
Works perfectly with Stripe billing
{
"error": "Too many requests",
"retry_after": 60
}