Implements API rate limiting and throttling with token bucket, sliding window, and fixed window algorithms, configuring per-user, per-IP, and per-endpoint limits via Redis-backed counters, API gateway plugins, or middleware, and returning proper HTTP 429 responses with Retry-After headers. Use when setting up request quota management or preventing brute force, credential stuffing, and resource exhaustion attacks against APIs.
Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
Implements API rate limiting and throttling with token bucket, sliding window, and fixed window algorithms, configuring per-user, per-IP, and per-endpoint limits via Redis-backed counters, API gateway plugins, or middleware, and returning proper HTTP 429 responses with Retry-After headers. Use when setting up request quota management or preventing brute force, credential stuffing, and resource exhaustion attacks against APIs.
Step 5: Distributed Rate Limiting for Microservices
# Centralized rate limiting service using Redis Clusterimport redis
from redis.cluster import RedisCluster
classDistributedRateLimiter:
"""Rate limiter for microservice architectures using Redis Cluster."""def__init__(self):
self.redis = RedisCluster(
startup_nodes=[
{"host": "redis-node-1", "port": 6379},
{"host": "redis-node-2", "port": 6379},
{"host": "redis-node-3", "port": 6379},
],
decode_responses=True
)
defcheck_and_increment(self, service_name, user_id, endpoint,
max_requests, window_seconds):
"""Atomic check-and-increment using Redis Lua script."""
key = f"rl:{{{service_name}}}:{user_id}:{endpoint}"# Lua script ensures atomicity across the check and increment
lua_script = """
local key = KEYS[1]
local max_requests = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local window_start = now - window
-- Remove old entries
redis.call('zremrangebyscore', key, '-inf', window_start)
-- Count current entries
local count = redis.call('zcard', key)
if count >= max_requests then
-- Get oldest entry for retry-after calculation
local oldest = redis.call('zrange', key, 0, 0, 'WITHSCORES')
local retry_after = 0
if #oldest > 0 then
retry_after = math.ceil(tonumber(oldest[2]) + window - now)
end
return {0, count, retry_after}
end
-- Add new entry
redis.call('zadd', key, now, now .. ':' .. math.random(100000))
redis.call('expire', key, window + 1)
return {1, count + 1, 0}
"""
result = self.redis.eval(lua_script, 1, key,
max_requests, window_seconds, time.time())
return {
"allowed": bool(result[0]),
"current": int(result[1]),
"retry_after": int(result[2]),
}
Key Concepts
Term
Definition
Sliding Window
Rate limiting algorithm that tracks requests in a rolling time window, providing smoother rate enforcement than fixed windows
Token Bucket
Algorithm where tokens are added at a fixed rate and consumed per request, allowing controlled bursts up to the bucket capacity
Fixed Window
Simplest rate limiting where requests are counted per fixed time window (e.g., per minute), susceptible to burst at window boundaries
429 Too Many Requests
HTTP status code indicating the client has exceeded the rate limit, accompanied by Retry-After header
Retry-After Header
HTTP response header telling the client how many seconds to wait before retrying, essential for well-behaved API clients
Distributed Rate Limiting
Rate limiting across multiple server instances using shared state (Redis, Memcached) to maintain accurate global counters
Tools & Systems
Redis: In-memory data store used for distributed rate limit counters with atomic operations via Lua scripts
Kong Rate Limiting Plugin: API gateway plugin supporting fixed-window and sliding-window rate limiting with Redis backend
express-rate-limit: Express.js middleware for simple rate limiting with Redis, Memcached, or in-memory stores
Flask-Limiter: Flask extension for rate limiting with support for multiple backends and configurable limits per endpoint
Envoy Rate Limit Service: Centralized rate limiting service for Envoy-based service mesh architectures
Common Scenarios
Scenario: Implementing Rate Limiting for a Public API
Context: A company launches a public API with free, premium, and enterprise tiers. The API must protect against abuse while providing fair access to paying customers. The API runs on 6 instances behind an AWS ALB.
Approach:
Deploy Redis Cluster (3 nodes) for distributed rate limit state
Implement sliding window rate limiter using Redis sorted sets with Lua scripts for atomicity