distributed-rate-limiting
Skill2 starsChanged 28 days agoApache-2.0View on GitHub
Architects, implements, and tunes distributed rate limiting using Redis sliding window logs, token buckets, tiered API quotas, RFC 6585 headers, and circuit breakers.
- Reads credentials
In this repository108 other files it gives its agents.
- AGENTS.mdAGENTS.md
- everything-gemini-maintainerSkill
- adversarial-fuzz-generatorSkill
- agent-evalsSkill
- ai-first-engineeringSkill
- alloydb-omni-vectorSkill
- apache-kafka-streamsSkill
- api-craftsmanSkill
- architecture-designerSkill
- argon-3d-game-engineSkill
- argon-3d-scene-architectSkill
- argon-procedural-asset-generatorSkill
- argon-visual-critique-loopSkill
- argo-workflowsSkill
- autonomous-incident-healerSkill
- axum-web-serviceSkill
- bigquery-vector-searchSkill
- browser-qa-testerSkill
- chromadb-vector-storeSkill
- cilium-ebpf-networkingSkill
- clickhouse-analyticsSkill
- cloud-deploy-canarySkill
- cloudflare-r2-object-storageSkill
- cloudflare-workersSkill
- cloud-run-agentsSkill
- context-cachingSkill
- context-distillation-compressorSkill
- context-warmerSkill
- continuous-learningSkill
- cost-oracle-finopsSkill
- cross-repo-swarm-syncSkill
- cve-remediation-orchestratorSkill
- database-architectSkill
- dataproc-spark-analyticsSkill
- dbt-transformationSkill
- debezium-cdcSkill
- devops-pipelineSkill
- docker-compose-orchestratorSkill
- duckdb-local-olapSkill
- dynamic-thinking-allocationSkill
- ebpf-microvm-sandboxSkill
- ebpf-sandbox-enforcerSkill
- elephant-goldfish-modelSkill
- envoy-ai-gatewaySkill
- ephemeral-swarm-orchestratorSkill
- fastapi-productionSkill
- fullstack-nextjsSkill
- game-audio-synthesizerSkill
- game-studioSkill
- gemini-function-callingSkill
- gemini-groundingSkill
- gemini-live-audio-transcriberSkill
- gemini-live-multimodalSkill
- gemini-live-screen-visionSkill
- gemini-multimodalSkill
- genetic-prompt-cross-pollinatorSkill
- git-craftsmanSkill
- github-opsSkill
- google-axSkill
- google-flow-orchestratorSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 135
Atomic Scripts: Never implement rate limiters with separated GET and INCR commands.
- Very specific: line 138
Header Standards: Always emit Retry-After, X-RateLimit-Limit, and X-RateLimit-Remaining to allow compliant clients to back off …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Distributed Rate Limiting & API Traffic Shaping
This skill guides the design, implementation, and tuning of distributed rate limiting systems across microservice clusters using Redis, token buckets, and sliding window algorithms.
⚡ Algorithm Comparison Matrix
| Algorithm | Precision | Memory Usage | Burst Handling | Best For |
Sliding Window Log | 100% Exact | High ($O(N)$ requests) | Excellent | Low-to-medium traffic sensitive endpoints (auth, payments). |
Sliding Window Counter | ~99% Exact | Fixed ($O(1)$) | Smooth | High-scale public APIs and ingress gateways. |
Token Bucket | High | Very Low ($O(1)$) | Controlled bursts | Standard microservice backpressuring and rate smoothing. |
Fixed Window | Poor (2x burst at boundary) | Lowest | Prone to spikes | Simple non-critical background jobs only. |
🔒 Atomic Sliding Window Rate Limiter (Redis Lua)
Atomic execution in Redis ensures no race conditions across distributed API instances:
-- sliding_window.lua
-- KEYS[1]: Rate limit key (e.g., "ratelimit:tenant_123:orders")
-- ARGV[1]: Current UNIX timestamp in milliseconds
-- ARGV[2]: Window size in milliseconds (e.g., 60000 for 1 minute)
-- ARGV[3]: Maximum allowed requests in window (e.g., 100)
local key = KEYS[1]
local now = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local limit = tonumber(ARGV[3])
local clearBefore = now - window
-- 1. Remove expired request timestamps outside the window
redis.call('ZREMRANGEBYSCORE', key, 0, clearBefore)
-- 2. Count requests currently in the sliding window
local currentRequests = redis.call('ZCARD', key)
if currentRequests < limit then
-- 3. Register current request with unique timestamp as score and member
redis.call('ZADD', key, now, now .. '-' .. math.random(1000, 9999))
redis.call('PEXPIRE', key, window)
return { 1, limit - currentRequests - 1, 0 } -- [Allowed: 1, Remaining, RetryAfter: 0]
else
-- 4. Calculate time until oldest entry expires
local oldest = redis.call('ZRANGE', key, 0, 0, 'WITHSCORES')
local retryAfter = math.ceil((tonumber(oldest[2]) + window - now) / 1000)
return { 0, 0, math.max(1, retryAfter) } -- [Allowed: 0, Remaining: 0, RetryAfter]
end🌐 Express / Fastify / Node.js Middleware Implementation
Wrap Redis Lua execution into production middleware with RFC 6585 compliance:
import { Request, Response, NextFunction } from 'express';
import Redis from 'ioredis';
const redis = new Redis(process.env.REDIS_URL || 'redis://localhost:6379');
export function createRateLimiter(options: { windowMs: number; max: number; keyGenerator?: (req: Request) => string }) {
return async (req: Request, res: Response, next: NextFunction) => {
const key = options.keyGenerator ? options.keyGenerator(req) : `ratelimit:${req.ip}`;
const now = Date.now();
try {
const [allowed, remaining, retryAfter] = await redis.eval(
SLIDING_WINDOW_LUA_SCRIPT,
1,
key,
now.toString(),
options.windowMs.toString(),
options.max.toString()
) as [number, number, number];
// Set standard RFC 6585 RateLimit response headers
res.setHeader('X-RateLimit-Limit', options.max);
res.setHeader('X-RateLimit-Remaining', remaining);
res.setHeader('X-RateLimit-Reset', Math.ceil((now + options.windowMs) / 1000));
if (allowed === 1) {
return next();
}
res.setHeader('Retry-After', retryAfter);
return res.status(429).json({
error: 'Too Many Requests',
message: `Rate limit exceeded. Please retry after ${retryAfter} seconds.`,
retryAfter,
});
} catch (err) {
// Circuit Breaker: Fail-Open for high availability if Redis degrades
console.error('RateLimiter Redis failure, failing open:', err);
return next();
}
};
}🎯 Tiered Multi-Dimensional Quotas
Configure tiered rate limits based on authenticated API keys and endpoint criticality:
export function getTierLimits(tier: 'free' | 'pro' | 'enterprise') {
switch (tier) {
case 'enterprise':
return { windowMs: 60_000, max: 10_000 };
case 'pro':
return { windowMs: 60_000, max: 1_200 };
case 'free':
default:
return { windowMs: 60_000, max: 60 };
}
}📋 Architectural Rules & Best Practices
- Atomic Scripts: Never implement rate limiters with separated
GETandINCRcommands; always execute atomic Lua scripts or Redis Transactions (MULTI/EXEC) to prevent race conditions under load. - Fail-Open Strategy: For user-facing critical flows, catch Redis connection timeouts and fail-open with alerting rather than causing total service outages.
- Multi-Dimensional Keys: Combine client identity with endpoint routes (e.g.
tenant:123:POST:/api/v1/checkout) to prevent denial of service on high-cost endpoints. - Header Standards: Always emit
Retry-After,X-RateLimit-Limit, andX-RateLimit-Remainingto allow compliant clients to back off exponentially.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

