Download this article as a PDF on the Codeflare Mobile App
Rate limiting restricts the number of requests a client can make within a specific time window.
429 Too Many Requests)Throttling controls the rate at which requests are processed, often by slowing them down rather than blocking them outright.
Learn on the Go. Download the Codeflare Mobile App from Google Play Store
| Feature | Rate Limiting | Throttling |
|---|---|---|
| Behavior | Blocks excess requests | Slows them down |
| Response | 429 error | Delayed response |
| Use case | Security & abuse control | Traffic shaping & smoothing |
Understanding these helps you design custom systems.
👉 Problem: A user can send many requests at the boundary
express-rate-limit (Most Common)npm install express-rate-limit const express = require('express');
const rateLimit = require('express-rate-limit');
const app = express();
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP
message: 'Too many requests, try again later',
standardHeaders: true,
legacyHeaders: false,
});
app.use('/api', limiter);
app.get('/api/data', (req, res) => {
res.send('API response');
});
app.listen(3000);
For distributed systems (multiple servers), in-memory limits won’t work.
Use Redis.
npm install rate-limiter-flexible ioredis
const { RateLimiterRedis } = require('rate-limiter-flexible');
const Redis = require('ioredis');
const redisClient = new Redis();
const rateLimiter = new RateLimiterRedis({
storeClient: redisClient,
points: 10, // 10 requests
duration: 1, // per second
});
const express = require('express');
const app = express();
app.use(async (req, res, next) => {
try {
await rateLimiter.consume(req.ip);
next();
} catch {
res.status(429).send('Too Many Requests');
}
});
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
app.use(async (req, res, next) => {
await delay(500); // delay each request
next();
});
class TokenBucket {
constructor(capacity, refillRate) {
this.capacity = capacity;
this.tokens = capacity;
this.refillRate = refillRate;
this.lastRefill = Date.now();
}
refill() {
const now = Date.now();
const diff = (now - this.lastRefill) / 1000;
this.tokens = Math.min(this.capacity, this.tokens + diff * this.refillRate);
this.lastRefill = now;
}
consume() {
this.refill();
if (this.tokens >= 1) {
this.tokens -= 1;
return true;
}
return false;
}
}
app.use('/login', strictLimiter);
app.use('/api', generalLimiter);
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 20 Return helpful errors:
{
"error": "Too many requests",
"retry_after": "60 seconds"
}
X-Forwarded-For)Latest tech news and coding tips.
Database migration is one of the most challenging tasks in software engineering. While both PostgreSQL…
Modern JavaScript isn’t just let, const, arrow functions, and promises anymore. Over the years, the language has…
Software development is one of the most rewarding careers in technology, but it is also…
Printing a document in JavaScript usually means triggering the browser’s print dialog and controlling what…
The display property controls how an element behaves in the layout and how its children are arranged. Access software…
JavaScript is one of the most flexible programming languages ever created. That flexibility is powerful,…