This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
securitycritical PriorityID: rate-limiting-token-bucket

Distributed Rate Limiting (Redis Token Bucket / Sliding Window) Standard

Enterprise DDoS and abuse mitigation via atomic Redis sliding-window log/token bucket algorithms. Enforces IP- and user-tier rate limits, standard IETF RateLimit-* headers, and atomic Lua script execution.

Prohibited Anti-Pattern: AI implements rate limiting with local in-memory JavaScript objects that leak memory and fail in clustered multi-pod deployments.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Distributed Rate Limiting (Redis Token Bucket / Sliding Window) Standard - Enterprise DDoS and abuse mitigation via atomic Redis sliding-window log/token bucket algorithms. Enforces IP- and user-tier rate limits, standard IETF RateLimit-* headers, and atomic Lua script execution.
globs: **/*rate*limit*.ts, **/*throttle*.ts, **/*limiter*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/rate-limiting-token-bucket"
---

# Distributed Rate Limiting (Redis Token Bucket / Sliding Window) Standard

> **Severity**: CRITICAL | **Category**: security
> **Anti-Pattern Warning**: AI implements rate limiting with local in-memory JavaScript objects that leak memory and fail in clustered multi-pod deployments.

## System Constraints
You are an API gateway security engineer. Never write rate limiting logic using local in-memory dictionaries or JavaScript Maps. In production multi-pod environments, rate limiting MUST be backed by a centralized Redis cluster using atomic sliding-window Lua scripts. Always return standard IETF RateLimit-* headers and HTTP 429 with Retry-After.

## Strict Guidelines
- Never use in-memory dictionaries for rate limiting; enforce distributed limits across all replicas using Redis atomic Lua scripts.
- Implement sliding-window rate limiting using sorted sets (ZSET) or token buckets to prevent edge-of-window bursting attacks.
- Return standard IETF RateLimit headers: RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset, and Retry-After upon HTTP 429.
- Differentiate rate limit tiers by authentication status (e.g., 60 req/min for anonymous IPs vs 1000 req/min for authenticated API keys).

## Bad vs Good Implementations
### Atomic Redis Sliding Window via Lua Script vs Broken In-Memory Map

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In a cluster with 5 Kubernetes pods, an attacker can make 5x the allowed requests. Also leaks RAM uncontrollably.
```typescript
// ANTI-PATTERN: Local in-memory Map rate limiter
// 1. FAILS in multi-container / multi-pod environments (each pod has separate counter!)
// 2. LEAKS memory: Uncleaned IP entries grow indefinitely until Node crashes!
const memoryHits = new Map<string, number>();

export function checkRateLimit(ip: string): boolean {
  const current = memoryHits.get(ip) || 0;
  if (current > 100) return false;
  memoryHits.set(ip, current + 1);
  return true;
}
```

#### Production Standard (Correct)
> **Standard**: Atomic Lua script executes inside Redis single-threaded engine, preventing race conditions and keeping count synchronized across all cluster pods.
```typescript
// PRODUCTION STANDARD: Atomic Redis Sliding-Window Rate Limiter
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);

// Atomic sliding-window Lua script (ZSET)
const SLIDING_WINDOW_LUA = `
  local key = KEYS[1]
  local now = tonumber(ARGV[1])
  local window = tonumber(ARGV[2])
  local limit = tonumber(ARGV[3])
  local clearBefore = now - window

  -- Remove expired timestamps outside the rolling window
  redis.call('ZREMRANGEBYSCORE', key, 0, clearBefore)

  local currentCount = redis.call('ZCARD', key)
  if currentCount < limit then
    redis.call('ZADD', key, now, now)
    redis.call('PEXPIRE', key, window)
    return {1, limit - currentCount - 1} -- Allowed, remaining
  else
    return {0, 0} -- Denied
  end
`;

export async function rateLimit(identifier: string, limit = 60, windowMs = 60000) {
  const now = Date.now();
  const result = await redis.eval(
    SLIDING_WINDOW_LUA,
    1,
    `ratelimit:${identifier}`,
    now,
    windowMs,
    limit
  ) as [number, number];

  const allowed = result[0] === 1;
  const remaining = result[1];
  return { allowed, remaining, resetSeconds: Math.ceil(windowMs / 1000) };
}
```

### Standard HTTP 429 Response with IETF Headers

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Clients and SDKs cannot programmatically know when it is safe to retry without standard headers.
```typescript
// ANTI-PATTERN: Arbitrary JSON without standard rate-limit headers
res.status(400).json({ error: "Too many calls" });
```

#### Production Standard (Correct)
> **Standard**: Standard headers allow HTTP clients (like Axios/Fetch with retry plugins) to throttle automatically.
```typescript
// PRODUCTION STANDARD: HTTP 429 with IETF RateLimit Headers
res.set({
  "RateLimit-Limit": limit.toString(),
  "RateLimit-Remaining": remaining.toString(),
  "RateLimit-Reset": resetSeconds.toString(),
  "Retry-After": resetSeconds.toString(),
});
if (!allowed) {
  return res.status(429).json({ error: "Too Many Requests", retryAfterSeconds: resetSeconds });
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Distributed Rate Limiting (Redis Token Bucket / Sliding Window) Standard

Distributed Rate Limiting (Redis Token Bucket / Sliding Window) Standard defines an industrial-grade behavioral standard for security engineering. Enterprise DDoS and abuse mitigation via atomic Redis sliding-window log/token bucket algorithms. Enforces IP- and user-tier rate limits, standard IETF RateLimit-* headers, and atomic Lua script execution. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: AI implements rate limiting with local in-memory JavaScript objects that leak memory and fail in clustered multi-pod deployments. Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftAI implements rate limiting with local in-memory JavaScript objects that leak memory and fail in clustered multi-pod deployments.Critical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Never use in-memory dictionaries for rate limiting

Never use in-memory dictionaries for rate limiting; enforce distributed limits across all replicas using Redis atomic Lua scripts.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Implement sliding-window rate limiting using sorted sets (ZSET) or token buckets to prevent edge-of-window bursting attacks.

Implement sliding-window rate limiting using sorted sets (ZSET) or token buckets to prevent edge-of-window bursting attacks.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Return standard IETF RateLimit headers

Return standard IETF RateLimit headers: RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset, and Retry-After upon HTTP 429.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Differentiate rate limit tiers by authentication status (e.g., 60 req/min for anonymous IPs vs 1000 req/min for authenticated API keys).

Differentiate rate limit tiers by authentication status (e.g., 60 req/min for anonymous IPs vs 1000 req/min for authenticated API keys).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Distributed Rate Limiting (Redis Token Bucket / Sliding Window) Standard critical for AI coding assistants?

Enterprise DDoS and abuse mitigation via atomic Redis sliding-window log/token bucket algorithms. Enforces IP- and user-tier rate limits, standard IETF RateLimit-* headers, and atomic Lua script execution. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (AI implements rate limiting with local in-memory JavaScript objects that leak memory and fail in clustered multi-pod deployments.) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Distributed