"Not a Pentest" Notice: Rate limiting protects your own APIs from overload. Defensive use only.
Moltbot AI Security · API Rate Limiting
Moltbot API Rate Limiting: Redis Sliding Window & DDoS Protection 2026
Without rate limiting, APIs are vulnerable to DDoS and abuse. Redis-based Sliding Window algorithms are today's state-of-the-art for high-availability systems.
What is API Rate Limiting? Simply Explained
Imagine a ticket counter that only lets 100 customers in per minute. Those who knock too often must wait briefly. Rate limiting works exactly the same: it limits how often a user (or IP) can call your API within a time window. This prevents DDoS attacks, API abuse (e.g., competing scrapers), and model extraction (someone systematically querying your LLM to clone it).
↓ Jump to strategy comparison, Redis Sliding Window, and DDoS Protection Layer
Rate Limiting Strategies
| Strategie | Use Case | Speicher | Genauigkeit |
|---|---|---|---|
| Fixed Window | Einfach, schnell | Minimal | Niedrig |
| Sliding Window | Präzise, fair | Mittel | Hoch |
| Token Bucket | Burst-fähig | Mittel | Mittel |
| Distributed Sliding Window | Multi-Server | Redis | Hoch |
Redis Sliding Window Implementation
// Redis Sliding Window Rate Limiter für Moltbot
const Redis = require('ioredis');
const redis = new Redis(process.env.REDIS_URL);
class SlidingWindowRateLimiter {
constructor(options = {}) {
this.windowSize = options.windowSize || 60000; // 1 minute
this.maxRequests = options.maxRequests || 100;
this.keyPrefix = options.keyPrefix || 'rate_limit:';
}
async isAllowed(key, identifier) {
const now = Date.now();
const windowStart = now - this.windowSize;
const redisKey = this.keyPrefix + key + ':' + identifier;
await redis.zremrangebyscore(redisKey, 0, windowStart);
const currentRequests = await redis.zcard(redisKey);
if (currentRequests >= this.maxRequests) {
return {
allowed: false,
remaining: 0,
resetTime: now + this.windowSize,
totalRequests: currentRequests
};
}
await redis.zadd(redisKey, now, now);
await redis.expire(redisKey, Math.ceil(this.windowSize / 1000));
return {
allowed: true,
remaining: this.maxRequests - currentRequests - 1,
resetTime: now + this.windowSize,
totalRequests: currentRequests + 1
};
}
middleware(options = {}) {
const limiter = new SlidingWindowRateLimiter(options);
return async (req, res, next) => {
const key = options.key || 'api';
const identifier = this.getIdentifier(req);
const result = await limiter.isAllowed(key, identifier);
res.set({
'X-RateLimit-Limit': limiter.maxRequests,
'X-RateLimit-Remaining': result.remaining,
'X-RateLimit-Reset': Math.ceil(result.resetTime / 1000)
});
if (!result.allowed) {
return res.status(429).json({
error: 'Too Many Requests',
retryAfter: Math.ceil((result.resetTime - Date.now()) / 1000)
});
}
next();
};
}
getIdentifier(req) {
const ip = req.ip || req.connection.remoteAddress;
const userId = req.user?.id;
return userId ? 'user:' + userId : 'ip:' + ip;
}
}
const rateLimiter = new SlidingWindowRateLimiter({
windowSize: 60000, // 1 minute
maxRequests: 100,
keyPrefix: 'moltbot:'
});
app.use('/api', rateLimiter.middleware({
windowSize: 60000,
maxRequests: 100,
key: 'general'
}));DDoS Protection Layer
// DDoS Protection für Moltbot
class DDoSProtection {
constructor(redisClient) {
this.redis = redisClient;
this.thresholds = {
ip: { requests: 1000, window: 60000 }, // 1000 req/min per IP
global: { requests: 10000, window: 60000 }, // 10k req/min global
endpoint: { requests: 500, window: 60000 } // 500 req/min per endpoint
};
}
async checkRequest(req) {
const ip = req.ip || req.connection.remoteAddress;
const endpoint = req.path;
const now = Date.now();
const ipKey = 'ddos:ip:' + ip;
const ipCount = await this.redis.incr(ipKey);
if (ipCount === 1) await this.redis.expire(ipKey, 60);
if (ipCount > this.thresholds.ip.requests) {
await this.blockIP(ip, 3600); // Block for 1 hour
return { blocked: true, reason: 'IP rate limit exceeded' };
}
const globalKey = 'ddos:global';
const globalCount = await this.redis.incr(globalKey);
if (globalCount === 1) await this.redis.expire(globalKey, 60);
if (globalCount > this.thresholds.global.requests) {
return { blocked: true, reason: 'Global rate limit exceeded' };
}
const endpointKey = 'ddos:endpoint:' + endpoint;
const endpointCount = await this.redis.incr(endpointKey);
if (endpointCount === 1) await this.redis.expire(endpointKey, 60);
if (endpointCount > this.thresholds.endpoint.requests) {
return { blocked: true, reason: 'Endpoint rate limit exceeded' };
}
return { blocked: false };
}
async blockIP(ip, duration) {
const blockKey = 'blocked:ip:' + ip;
await this.redis.setex(blockKey, duration, '1');
await this.redis.lpush('ddos:blocks', JSON.stringify({
ip,
timestamp: Date.now(),
duration,
reason: 'Rate limit exceeded'
}));
}
async isIPBlocked(ip) {
const blockKey = 'blocked:ip:' + ip;
return await this.redis.exists(blockKey);
}
}
const ddosProtection = new DDoSProtection(redis);
app.use(async (req, res, next) => {
if (await ddosProtection.isIPBlocked(req.ip)) {
return res.status(403).json({ error: 'IP blocked' });
}
const result = await ddosProtection.checkRequest(req);
if (result.blocked) {
return res.status(429).json({
error: 'Rate limit exceeded',
reason: result.reason
});
}
next();
});CG
ClawKing Security Team
✓ VerifiedSecurity Research & Engineering · API Security Specialists
📅 Published: 27.04.2026🔄 Last reviewed: 27.04.2026
This guide is based on practical experience with Redis-based rate limiting in high-load production environments. The code examples are production-ready and tested in Moltbot deployments.
🔒 Verified by ClawKing Security Team·All information fact-checked and peer-reviewed