Rate Limiting: Algorithms and Implementation for APIs
Explore rate limiting algorithms: Token Bucket, Leaky Bucket, and Sliding Window with practical Node.js and Cloudflare Workers implementations.
Rate limiting protects APIs from abuse, brute-force attacks, and unexpected traffic spikes. This article breaks down popular rate limiting algorithms and demonstrates how to implement them in production.
Why Do We Need Rate Limiting?#
Rate limiting acts like a nightclub bouncer for your backend servers:
- Mitigate DDoS & Brute-Force Attacks: Slow down automated credential stuffing or scraping bots before they reach authentication handlers or databases.
- Ensure Fair Resource Usage: Prevent a single aggressive consumer (“noisy neighbor”) from starving other users of bandwidth or compute capacity.
- Shield Backends from Cascading Failures: Protect downstream databases, queues, and internal microservices from being overwhelmed during traffic spikes.
- Control Third-Party API Costs: Keep operational costs predictable, especially when proxies wrap metered external APIs like OpenAI or Anthropic.
Core Algorithms#
1. Fixed Window Counter#
The simplest approach: count incoming requests within fixed time intervals (e.g., 60-second windows).
class FixedWindowRateLimiter {
private windows = new Map<string, { count: number; resetAt: number }>();
constructor(
private windowMs: number,
private maxRequests: number,
) {}
check(key: string): boolean {
const now = Date.now();
let window = this.windows.get(key);
if (!window || now > window.resetAt) {
window = { count: 0, resetAt: now + this.windowMs };
this.windows.set(key, window);
}
window.count++;
return window.count <= this.maxRequests;
}
}
const limiter = new FixedWindowRateLimiter(60_000, 10);
// 10 requests / minutetypescript[!WARNING] Boundary burst issue: If a user sends 10 requests at
0:59and another 10 requests at1:00, they successfully execute 20 requests within 2 seconds, potentially doubling your intended server load.
2. Sliding Window Log#
Stores an exact timestamp for every request and calculates the number of requests that occurred during the preceding rolling window (e.g., past 60 seconds).
class SlidingWindowLog {
private logs = new Map<string, number[]>();
constructor(
private windowMs: number,
private maxRequests: number,
) {}
check(key: string): boolean {
const now = Date.now();
let log = this.logs.get(key) || [];
const windowStart = now - this.windowMs;
// Discard outdated entries
log = log.filter(t => t > windowStart);
this.logs.set(key, log);
if (log.length >= this.maxRequests) {
return false;
}
log.push(now);
return true;
}
}typescript[!NOTE] Highly accurate with smooth boundary handling, but memory consumption grows linearly with request volume because every request timestamp is saved.
3. Sliding Window Counter (Redis)#
Combines the low memory footprint of fixed windows with the smoothness of sliding logs using Redis Sorted Sets (ZSET):
import { createClient } from 'redis';
const redis = createClient();
async function checkRateLimit(
key: string,
limit: number,
windowSec: number,
): Promise<{ allowed: boolean; remaining: number; resetAt: number }> {
const now = Date.now();
const windowStart = now - windowSec * 1000;
// Execute atomic pipeline transaction
const multi = redis.multi();
multi.zRemRangeByScore(key, 0, windowStart); // Remove expired timestamps
multi.zAdd(key, { score: now, value: `${now}` });
multi.zCard(key); // Count requests in current sliding window
multi.expire(key, windowSec);
const [, , count] = await multi.exec();
const requestCount = count as number;
return {
allowed: requestCount <= limit,
remaining: Math.max(0, limit - requestCount),
resetAt: Math.ceil((windowStart + windowSec * 1000) / 1000),
};
}typescript4. Token Bucket#
Imagine a bucket that holds up to tokens. New tokens are added at a constant refill rate (tokens per second). When a request arrives, it must consume 1 token to pass. If the bucket is empty, the request is rejected.
class TokenBucket {
private buckets = new Map<string, { tokens: number; lastRefill: number }>();
constructor(
private capacity: number, // Maximum bucket capacity
private refillRate: number, // Tokens added per second
) {}
private refill(key: string) {
const bucket = this.buckets.get(key);
if (!bucket) return;
const now = Date.now();
const elapsed = (now - bucket.lastRefill) / 1000;
const newTokens = elapsed * this.refillRate;
bucket.tokens = Math.min(this.capacity, bucket.tokens + newTokens);
bucket.lastRefill = now;
}
tryConsume(key: string): boolean {
if (!this.buckets.has(key)) {
this.buckets.set(key, { tokens: this.capacity, lastRefill: Date.now() });
}
this.refill(key);
const bucket = this.buckets.get(key)!;
if (bucket.tokens >= 1) {
bucket.tokens -= 1;
return true;
}
return false;
}
}typescript[!TIP] Key advantage: Accommodates brief bursts of traffic (up to the bucket capacity) while strictly bounding long-term average throughput. Widely adopted by AWS and Stripe.
5. Leaky Bucket#
Incoming requests enter a FIFO queue (the bucket) and leak out for processing at a strictly constant rate. If incoming requests exceed bucket capacity, excess requests leak over the edge and are immediately dropped or rejected.
Ideal for traffic shaping and smoothing background asynchronous job pipelines.
Implementation — Express Middleware#
A real-world Express server usually requires tiered rate limits depending on the route sensitivity and user tier:
import rateLimit from 'express-rate-limit';
import RedisStore from 'rate-limit-redis';
import { createClient } from 'redis';
const redis = createClient();
// Global limiter — baseline protection for general endpoints
const globalLimiter = rateLimit({
windowMs: 60_000,
max: 100,
standardHeaders: true,
legacyHeaders: false,
message: { error: 'Too many requests' },
});
// Auth limiter — strict policy to combat brute-force attacks
const authLimiter = rateLimit({
windowMs: 15 * 60_000,
max: 5,
skipSuccessfulRequests: false,
keyGenerator: (req) => req.ip,
handler: (req, res) => {
res.status(429).json({
error: 'Too many login attempts. Try again later.',
retryAfter: Math.ceil(req.rateLimit.resetTime / 1000),
});
},
});
// API key limiter — using distributed Redis store
const apiLimiter = rateLimit({
store: new RedisStore({
sendCommand: (...args: string[]) => redis.sendCommand(args),
}),
windowMs: 60_000,
max: (req) => {
// Tiered quotas based on subscription tier
const tier = req.headers['x-api-tier'] || 'free';
const limits = { free: 10, pro: 100, enterprise: 1000 };
return limits[tier as keyof typeof limits] || 10;
},
keyGenerator: (req) => req.headers['x-api-key'] as string,
});
app.use('/api/', globalLimiter);
app.use('/api/auth/login', authLimiter);
app.use('/api/v2', apiLimiter);typescriptResponse Headers#
Always include rate limit metadata in HTTP response headers so clients can intelligently throttle retries using exponential backoff:
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 45
X-RateLimit-Reset: 1718845200
Retry-After: 45httpCloudflare Rate Limiting#
Deploying rate limits directly to the network edge stops malicious traffic before it reaches your origin servers.
Create a rule via Wrangler CLI or Cloudflare Dashboard:
# Wrangler CLI command
wrangler ratelimit create my-limit --limit 100 --period 60bashCloudflare Workers binding implementation:
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const key = request.headers.get('CF-Connecting-IP') || 'unknown';
const { success } = await env.myRateLimiter.limit({ key });
if (!success) {
return new Response('Too Many Requests', { status: 429 });
}
return new Response('OK');
},
};typescriptDistributed Rate Limiting#
When running a cluster of API servers, local in-memory counters fail because requests from the same user hit different instances. A centralized in-memory store like Redis synchronizes state across the cluster:
Atomic Redis Lua script to prevent race conditions:
-- rate_limit.lua
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
redis.call('ZREMRANGEBYSCORE', key, 0, now - window)
local count = redis.call('ZCARD', key)
if count >= limit then
return 0
end
redis.call('ZADD', key, now, now)
redis.call('EXPIRE', key, window)
return limit - count - 1luaBest Practices#
- Always return standard headers: Enables well-behaved API clients to throttle themselves and respect
Retry-After. - Apply endpoint-specific policies: Keep public content open, but apply strict limits to compute-heavy queries and authentication endpoints.
- Use compound keys: Combine IP address, API key, and user ID (
IP + apiKey + userId) to prevent bypassing limits by switching network connections. - Leverage distributed stores: Use Redis with Lua scripts or connection pooling across multiple application instances.
- Implement graceful degradation: Fail open (allow requests) if your Redis cache temporarily becomes unreachable, rather than blocking legitimate users.
- Log violations: Continuously monitor 429 logs to detect coordinated bot attacks and evaluate whether threshold adjustments are needed.
Conclusion#
Rate limiting is not only a defensive security barrier; it is essential resource governance. Fixed window counters are quick to implement, sliding windows provide precision without boundary spikes, and token buckets deliver flexible burst tolerance. For multi-node architectures, use Redis atomic scripts; for global applications, enforce limits at the edge with Cloudflare Workers. Choose the algorithm that matches your traffic characteristics and infrastructure scale.