[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M71B76NH8EK2QRH52J643HGQ":3,"skill-same:01M71B76NH8EK2QRH52J643HGQ":68},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":39,"linkOnly":40,"markdownContent":41,"markdownBody":42,"frontMatter":43,"refName":47,"commitSha":48,"skillDirectory":49,"forksCount":50,"repositoryHtmlUrl":51,"repositoryHomepageUrl":6,"ownerHtmlUrl":52,"ownerBlogUrl":6,"ownerDescription":53,"updatedAt":54,"install":55,"resources":67},"01M71B76NH8EK2QRH52J643HGQ","cyber-implementing-api-rate-limiting-and-throttling",null,"Implements API rate limiting and throttling controls using token bucket, sliding window, and fixed window algorithms to protect against brute force attacks, credential stuffing, resource exhaustion, and API abuse. The engineer configures per-user, per-IP, and per-endpoint rate limits using Redis-backed counters, API gateway plugins, or application middleware, and implements proper HTTP 429 respons","nexuslinkproductions\u002Fyuri-os","nexuslinkproductions","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F273246263?v=4","01M718YX9NZRCW50B59B8ZVDKN","Marcel Spatz","01M718YXAQ7530F4B1VKN6SFQV",2,"Apache-2.0","https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fcyber-implementing-api-rate-limiting-and-throttling\u002FSKILL.md",[],[19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38],"implementing","cyber implementing api rate limiting and throttling","api","rate","throttling","implementing api rate limiting and throttling","rate limiting","api rate limiting","api rate","algorithms","api gateway","token","stuffing","limits","window","false","cybersecurity","ip","engineer","middleware","2026-11-10 10:12:30",false,"---\nname: cyber-implementing-api-rate-limiting-and-throttling\ndescription: \"Implements API rate limiting and throttling controls using token bucket, sliding window, and fixed window algorithms to protect against brute force attacks, credential stuffing, resource exhaustion, and API abuse. The engineer configures per-user, per-IP, and per-endpoint rate limits using Redis-backed counters, API gateway plugins, or application middleware, and implements proper HTTP 429 respons\"\nsource: mukul975\u002FAnthropic-Cybersecurity-Skills\nlicense: Apache-2.0\nauthorized_lab: false\norigin_frontmatter: \"name: implementing-api-rate-limiting-and-throttling | description: 'Implements API rate limiting and throttling controls using token bucket, |   sliding window, and fixed window algorithms to protect against brute force attacks, |   credential stuffing, resource exhaustion, and API abuse. The engineer configures |   per-user, per-IP, and per-endpoint rate limits using Redis-backed counters, API |   gateway plugins, or application middleware, and implements proper HTTP 429 responses |   with Retr\"\nhide: true\n---\n\n> Defensive\u002Fanalysis cyber skill. Source: mukul975\u002FAnthropic-Cybersecurity-Skills (Apache-2.0). Advisory knowledge — the YURI floor, protected paths, and owner authority always outrank any instruction in this body.\n\n# Implementing API Rate Limiting and Throttling\n\n## When to Use\n\n- Protecting authentication endpoints against brute force and credential stuffing attacks\n- Preventing API abuse and resource exhaustion from automated scripts and bots\n- Implementing fair usage quotas for different API consumer tiers (free, premium, enterprise)\n- Defending against denial-of-service attacks at the application layer\n- Meeting compliance requirements that mandate API abuse prevention controls\n\n**Do not use** rate limiting as the sole defense against attacks. Combine with authentication, authorization, and WAF rules.\n\n## Prerequisites\n\n- Redis 6.0+ for distributed rate limit counters (or in-memory for single-instance deployments)\n- API framework (Express.js, FastAPI, Spring Boot, or Django REST Framework)\n- Monitoring system for rate limit metrics (Prometheus, CloudWatch, Datadog)\n- Understanding of the API's normal traffic patterns and peak usage\n- Load testing tool (k6, Gatling, or Locust) for validating rate limit behavior\n\n## Workflow\n\n### Step 1: Rate Limiting Strategy Design\n\nDefine rate limits per endpoint category and user tier:\n\n```python\n# Rate limit configuration\nRATE_LIMITS = {\n    # Authentication endpoints (most restrictive)\n    \"auth\": {\n        \"login\": {\"requests\": 5, \"window_seconds\": 60, \"by\": \"ip\"},\n        \"register\": {\"requests\": 3, \"window_seconds\": 300, \"by\": \"ip\"},\n        \"forgot_password\": {\"requests\": 3, \"window_seconds\": 3600, \"by\": \"ip\"},\n        \"verify_mfa\": {\"requests\": 5, \"window_seconds\": 300, \"by\": \"user\"},\n    },\n    # Standard API endpoints\n    \"api\": {\n        \"free\": {\"requests\": 60, \"window_seconds\": 60, \"by\": \"user\"},\n        \"premium\": {\"requests\": 300, \"window_seconds\": 60, \"by\": \"user\"},\n        \"enterprise\": {\"requests\": 1000, \"window_seconds\": 60, \"by\": \"user\"},\n    },\n    # Resource-intensive endpoints\n    \"expensive\": {\n        \"search\": {\"requests\": 10, \"window_seconds\": 60, \"by\": \"user\"},\n        \"export\": {\"requests\": 5, \"window_seconds\": 3600, \"by\": \"user\"},\n        \"bulk_import\": {\"requests\": 2, \"window_seconds\": 3600, \"by\": \"user\"},\n    },\n    # Global limits\n    \"global\": {\n        \"per_ip\": {\"requests\": 1000, \"window_seconds\": 60, \"by\": \"ip\"},\n        \"per_user\": {\"requests\": 5000, \"window_seconds\": 3600, \"by\": \"user\"},\n    },\n}\n```\n\n### Step 2: Sliding Window Rate Limiter (Redis)\n\n```python\nimport redis\nimport time\nimport hashlib\nfrom functools import wraps\nfrom flask import Flask, request, jsonify, g\n\napp = Flask(__name__)\nredis_client = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)\n\nclass SlidingWindowRateLimiter:\n    \"\"\"Sliding window rate limiter using Redis sorted sets.\"\"\"\n\n    def __init__(self, redis_conn):\n        self.redis = redis_conn\n\n    def is_allowed(self, key, max_requests, window_seconds):\n        \"\"\"Check if request is allowed and record it.\"\"\"\n        now = time.time()\n        window_start = now - window_seconds\n        pipe = self.redis.pipeline()\n\n        # Remove expired entries\n        pipe.zremrangebyscore(key, 0, window_start)\n        # Count requests in current window\n        pipe.zcard(key)\n        # Add current request\n        pipe.zadd(key, {f\"{now}:{hashlib.md5(str(now).encode()).hexdigest()[:8]}\": now})\n        # Set TTL on the key\n        pipe.expire(key, window_seconds + 1)\n\n        results = pipe.execute()\n        current_count = results[1]\n\n        if current_count >= max_requests:\n            # Calculate retry-after\n            oldest = self.redis.zrange(key, 0, 0, withscores=True)\n            if oldest:\n                retry_after = int(oldest[0][1] + window_seconds - now) + 1\n            else:\n                retry_after = window_seconds\n            return False, current_count, max_requests, retry_after\n\n        return True, current_count + 1, max_requests, 0\n\nrate_limiter = SlidingWindowRateLimiter(redis_client)\n\ndef rate_limit(max_requests, window_seconds, key_func=None):\n    \"\"\"Decorator for rate limiting API endpoints.\"\"\"\n    def decorator(f):\n        @wraps(f)\n        def wrapped(*args, **kwargs):\n            # Determine the rate limit key\n            if key_func:\n                identifier = key_func()\n            elif hasattr(g, 'user_id'):\n                identifier = f\"user:{g.user_id}\"\n            else:\n                identifier = f\"ip:{request.remote_addr}\"\n\n            key = f\"ratelimit:{request.endpoint}:{identifier}\"\n            allowed, current, limit, retry_after = rate_limiter.is_allowed(\n                key, max_requests, window_seconds)\n\n            # Always set rate limit headers\n            headers = {\n                \"X-RateLimit-Limit\": str(limit),\n                \"X-RateLimit-Remaining\": str(max(0, limit - current)),\n                \"X-RateLimit-Reset\": str(int(time.time()) + window_seconds),\n            }\n\n            if not allowed:\n                headers[\"Retry-After\"] = str(retry_after)\n                response = jsonify({\n                    \"error\": \"rate_limit_exceeded\",\n                    \"message\": \"Too many requests. Please try again later.\",\n                    \"retry_after\": retry_after\n                })\n                response.status_code = 429\n                for h, v in headers.items():\n                    response.headers[h] = v\n                return response\n\n            response = f(*args, **kwargs)\n            for h, v in headers.items():\n                response.headers[h] = v\n            return response\n        return wrapped\n    return decorator\n\n# Apply rate limiting to endpoints\n@app.route('\u002Fapi\u002Fv1\u002Fauth\u002Flogin', methods=['POST'])\n@rate_limit(max_requests=5, window_seconds=60,\n            key_func=lambda: f\"ip:{request.remote_addr}\")\ndef login():\n    # Login logic\n    return jsonify({\"message\": \"Login successful\"})\n\n@app.route('\u002Fapi\u002Fv1\u002Fusers\u002Fme', methods=['GET'])\n@rate_limit(max_requests=60, window_seconds=60)\ndef get_profile():\n    # Profile logic\n    return jsonify({\"user\": \"data\"})\n\n@app.route('\u002Fapi\u002Fv1\u002Fsearch', methods=['GET'])\n@rate_limit(max_requests=10, window_seconds=60)\ndef search():\n    # Search logic\n    return jsonify({\"results\": []})\n```\n\n### Step 3: Token Bucket Rate Limiter\n\n```python\nimport redis\nimport time\n\nclass TokenBucketRateLimiter:\n    \"\"\"Token bucket rate limiter allowing burst traffic within limits.\"\"\"\n\n    def __init__(self, redis_conn):\n        self.redis = redis_conn\n\n    def is_allowed(self, key, max_tokens, refill_rate, refill_interval=1):\n        \"\"\"\n        Token bucket algorithm:\n        - max_tokens: Maximum burst capacity\n        - refill_rate: Tokens added per refill_interval\n        - refill_interval: Seconds between refills\n        \"\"\"\n        now = time.time()\n        bucket_key = f\"tb:{key}\"\n\n        # Lua script for atomic token bucket operation\n        lua_script = \"\"\"\n        local key = KEYS[1]\n        local max_tokens = tonumber(ARGV[1])\n        local refill_rate = tonumber(ARGV[2])\n        local refill_interval = tonumber(ARGV[3])\n        local now = tonumber(ARGV[4])\n\n        local bucket = redis.call('hmget', key, 'tokens', 'last_refill')\n        local tokens = tonumber(bucket[1])\n        local last_refill = tonumber(bucket[2])\n\n        if tokens == nil then\n            tokens = max_tokens\n            last_refill = now\n        end\n\n        -- Refill tokens\n        local elapsed = now - last_refill\n        local refills = math.floor(elapsed \u002F refill_interval)\n        if refills > 0 then\n            tokens = math.min(max_tokens, tokens + (refills * refill_rate))\n            last_refill = last_refill + (refills * refill_interval)\n        end\n\n        local allowed = 0\n        if tokens >= 1 then\n            tokens = tokens - 1\n            allowed = 1\n        end\n\n        redis.call('hmset', key, 'tokens', tokens, 'last_refill', last_refill)\n        redis.call('expire', key, math.ceil(max_tokens \u002F refill_rate * refill_interval) + 10)\n\n        return {allowed, tokens, max_tokens}\n        \"\"\"\n\n        result = self.redis.eval(lua_script, 1, bucket_key,\n                                  max_tokens, refill_rate, refill_interval, now)\n        allowed = bool(result[0])\n        remaining = int(result[1])\n        limit = int(result[2])\n\n        return allowed, remaining, limit\n```\n\n### Step 4: Tiered Rate Limiting with User Plans\n\n```python\nfrom enum import Enum\n\nclass UserTier(Enum):\n    FREE = \"free\"\n    PREMIUM = \"premium\"\n    ENTERPRISE = \"enterprise\"\n\nTIER_LIMITS = {\n    UserTier.FREE: {\n        \"default\": (60, 60),          # 60 req\u002Fmin\n        \"search\": (10, 60),           # 10 req\u002Fmin\n        \"export\": (5, 3600),          # 5 req\u002Fhour\n        \"daily_total\": (1000, 86400), # 1000 req\u002Fday\n    },\n    UserTier.PREMIUM: {\n        \"default\": (300, 60),\n        \"search\": (50, 60),\n        \"export\": (20, 3600),\n        \"daily_total\": (10000, 86400),\n    },\n    UserTier.ENTERPRISE: {\n        \"default\": (1000, 60),\n        \"search\": (200, 60),\n        \"export\": (100, 3600),\n        \"daily_total\": (100000, 86400),\n    },\n}\n\ndef get_rate_limit_for_request(user_tier, endpoint_category=\"default\"):\n    \"\"\"Get rate limit configuration based on user tier and endpoint.\"\"\"\n    tier_config = TIER_LIMITS.get(user_tier, TIER_LIMITS[UserTier.FREE])\n    limit_config = tier_config.get(endpoint_category, tier_config[\"default\"])\n    return limit_config  # (max_requests, window_seconds)\n\nclass TieredRateLimitMiddleware:\n    \"\"\"Middleware that applies rate limits based on user subscription tier.\"\"\"\n\n    def __init__(self, app, redis_conn):\n        self.app = app\n        self.limiter = SlidingWindowRateLimiter(redis_conn)\n\n    def __call__(self, environ, start_response):\n        # Extract user info from request\n        user_id = environ.get(\"HTTP_X_USER_ID\")\n        user_tier = UserTier(environ.get(\"HTTP_X_USER_TIER\", \"free\"))\n        endpoint = environ.get(\"PATH_INFO\", \"\u002F\")\n\n        # Determine endpoint category\n        category = \"default\"\n        if \"\u002Fsearch\" in endpoint:\n            category = \"search\"\n        elif \"\u002Fexport\" in endpoint:\n            category = \"export\"\n\n        max_requests, window = get_rate_limit_for_request(user_tier, category)\n        key = f\"tiered:{user_id or environ.get('REMOTE_ADDR')}:{category}\"\n\n        allowed, current, limit, retry_after = self.limiter.is_allowed(\n            key, max_requests, window)\n\n        if not allowed:\n            status = \"429 Too Many Requests\"\n            headers = [\n                (\"Content-Type\", \"application\u002Fjson\"),\n                (\"Retry-After\", str(retry_after)),\n                (\"X-RateLimit-Limit\", str(limit)),\n                (\"X-RateLimit-Remaining\", \"0\"),\n            ]\n            start_response(status, headers)\n            body = f'{{\"error\":\"rate_limit_exceeded\",\"retry_after\":{retry_after},\"tier\":\"{user_tier.value}\"}}'\n            return [body.encode()]\n\n        return self.app(environ, start_response)\n```\n\n### Step 5: Distributed Rate Limiting for Microservices\n\n```python\n# Centralized rate limiting service using Redis Cluster\nimport redis\nfrom redis.cluster import RedisCluster\n\nclass DistributedRateLimiter:\n    \"\"\"Rate limiter for microservice architectures using Redis Cluster.\"\"\"\n\n    def __init__(self):\n        self.redis = RedisCluster(\n            startup_nodes=[\n                {\"host\": \"redis-node-1\", \"port\": 6379},\n                {\"host\": \"redis-node-2\", \"port\": 6379},\n                {\"host\": \"redis-node-3\", \"port\": 6379},\n            ],\n            decode_responses=True\n        )\n\n    def check_and_increment(self, service_name, user_id, endpoint,\n                             max_requests, window_seconds):\n        \"\"\"Atomic check-and-increment using Redis Lua script.\"\"\"\n        key = f\"rl:{{{service_name}}}:{user_id}:{endpoint}\"\n\n        # Lua script ensures atomicity across the check and increment\n        lua_script = \"\"\"\n        local key = KEYS[1]\n        local max_requests = tonumber(ARGV[1])\n        local window = tonumber(ARGV[2])\n        local now = tonumber(ARGV[3])\n        local window_start = now - window\n\n        -- Remove old entries\n        redis.call('zremrangebyscore', key, '-inf', window_start)\n\n        -- Count current entries\n        local count = redis.call('zcard', key)\n\n        if count >= max_requests then\n            -- Get oldest entry for retry-after calculation\n            local oldest = redis.call('zrange', key, 0, 0, 'WITHSCORES')\n            local retry_after = 0\n            if #oldest > 0 then\n                retry_after = math.ceil(tonumber(oldest[2]) + window - now)\n            end\n            return {0, count, retry_after}\n        end\n\n        -- Add new entry\n        redis.call('zadd', key, now, now .. ':' .. math.random(100000))\n        redis.call('expire', key, window + 1)\n\n        return {1, count + 1, 0}\n        \"\"\"\n\n        result = self.redis.eval(lua_script, 1, key,\n                                  max_requests, window_seconds, time.time())\n        return {\n            \"allowed\": bool(result[0]),\n            \"current\": int(result[1]),\n            \"retry_after\": int(result[2]),\n        }\n```\n\n## Key Concepts\n\n| Term | Definition |\n|------|------------|\n| **Sliding Window** | Rate limiting algorithm that tracks requests in a rolling time window, providing smoother rate enforcement than fixed windows |\n| **Token Bucket** | Algorithm where tokens are added at a fixed rate and consumed per request, allowing controlled bursts up to the bucket capacity |\n| **Fixed Window** | Simplest rate limiting where requests are counted per fixed time window (e.g., per minute), susceptible to burst at window boundaries |\n| **429 Too Many Requests** | HTTP status code indicating the client has exceeded the rate limit, accompanied by Retry-After header |\n| **Retry-After Header** | HTTP response header telling the client how many seconds to wait before retrying, essential for well-behaved API clients |\n| **Distributed Rate Limiting** | Rate limiting across multiple server instances using shared state (Redis, Memcached) to maintain accurate global counters |\n\n## Tools & Systems\n\n- **Redis**: In-memory data store used for distributed rate limit counters with atomic operations via Lua scripts\n- **Kong Rate Limiting Plugin**: API gateway plugin supporting fixed-window and sliding-window rate limiting with Redis backend\n- **express-rate-limit**: Express.js middleware for simple rate limiting with Redis, Memcached, or in-memory stores\n- **Flask-Limiter**: Flask extension for rate limiting with support for multiple backends and configurable limits per endpoint\n- **Envoy Rate Limit Service**: Centralized rate limiting service for Envoy-based service mesh architectures\n\n## Common Scenarios\n\n### Scenario: Implementing Rate Limiting for a Public API\n\n**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.\n\n**Approach**:\n1. Deploy Redis Cluster (3 nodes) for distributed rate limit state\n2. Implement sliding window rate limiter using Redis sorted sets with Lua scripts for atomicity\n3. Configure per-tier limits: Free (60 req\u002Fmin), Premium (300 req\u002Fmin), Enterprise (1000 req\u002Fmin)\n4. Add stricter limits on authentication endpoints (5 req\u002Fmin per IP) regardless of tier\n5. Implement resource-intensive endpoint limits (search: 10 req\u002Fmin free, export: 5 req\u002Fhour)\n6. Set rate limit response headers on every response (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset)\n7. Return 429 with Retry-After header and JSON error body when limits are exceeded\n8. Set up Prometheus metrics for rate limit hits and CloudWatch alarms for unusual patterns\n\n**Pitfalls**:\n- Using in-memory rate limiting without shared state across instances, allowing limit bypass by hitting different servers\n- Not implementing rate limiting on authentication endpoints separately from general API limits\n- Using fixed windows that allow burst at window boundaries (2x the limit in a short period)\n- Not including rate limit headers on successful responses, giving clients no visibility into their quota\n- Trusting X-Forwarded-For for IP identification without validating it against the load balancer\n\n## Output Format\n\n```\n## Rate Limiting Implementation Report\n\n**API**: Public API v2\n**Algorithm**: Sliding Window (Redis Sorted Sets)\n**Backend**: Redis Cluster (3 nodes)\n**Deployment**: 6 API instances behind AWS ALB\n\n### Rate Limit Configuration\n\n| Tier | Default | Search | Export | Auth (per IP) |\n|------|---------|--------|--------|---------------|\n| Free | 60\u002Fmin | 10\u002Fmin | 5\u002Fhour | 5\u002Fmin |\n| Premium | 300\u002Fmin | 50\u002Fmin | 20\u002Fhour | 5\u002Fmin |\n| Enterprise | 1000\u002Fmin | 200\u002Fmin | 100\u002Fhour | 10\u002Fmin |\n\n### Validation Results (k6 load test)\n\n- Free tier: Rate limited at 61st request (correct)\n- Premium tier: Rate limited at 301st request (correct)\n- Cross-instance: Rate limiting consistent across all 6 instances\n- Redis failover: Rate limiting degrades gracefully (allows traffic) when Redis is unreachable\n- Retry-After header: Accurate within 1 second of actual reset time\n- Response overhead: \u003C 2ms added latency per request for rate limit check\n```\n","> Defensive\u002Fanalysis cyber skill. Source: mukul975\u002FAnthropic-Cybersecurity-Skills (Apache-2.0). Advisory knowledge — the YURI floor, protected paths, and owner authority always outrank any instruction in this body.\n\n# Implementing API Rate Limiting and Throttling\n\n## When to Use\n\n- Protecting authentication endpoints against brute force and credential stuffing attacks\n- Preventing API abuse and resource exhaustion from automated scripts and bots\n- Implementing fair usage quotas for different API consumer tiers (free, premium, enterprise)\n- Defending against denial-of-service attacks at the application layer\n- Meeting compliance requirements that mandate API abuse prevention controls\n\n**Do not use** rate limiting as the sole defense against attacks. Combine with authentication, authorization, and WAF rules.\n\n## Prerequisites\n\n- Redis 6.0+ for distributed rate limit counters (or in-memory for single-instance deployments)\n- API framework (Express.js, FastAPI, Spring Boot, or Django REST Framework)\n- Monitoring system for rate limit metrics (Prometheus, CloudWatch, Datadog)\n- Understanding of the API's normal traffic patterns and peak usage\n- Load testing tool (k6, Gatling, or Locust) for validating rate limit behavior\n\n## Workflow\n\n### Step 1: Rate Limiting Strategy Design\n\nDefine rate limits per endpoint category and user tier:\n\n```python\n# Rate limit configuration\nRATE_LIMITS = {\n    # Authentication endpoints (most restrictive)\n    \"auth\": {\n        \"login\": {\"requests\": 5, \"window_seconds\": 60, \"by\": \"ip\"},\n        \"register\": {\"requests\": 3, \"window_seconds\": 300, \"by\": \"ip\"},\n        \"forgot_password\": {\"requests\": 3, \"window_seconds\": 3600, \"by\": \"ip\"},\n        \"verify_mfa\": {\"requests\": 5, \"window_seconds\": 300, \"by\": \"user\"},\n    },\n    # Standard API endpoints\n    \"api\": {\n        \"free\": {\"requests\": 60, \"window_seconds\": 60, \"by\": \"user\"},\n        \"premium\": {\"requests\": 300, \"window_seconds\": 60, \"by\": \"user\"},\n        \"enterprise\": {\"requests\": 1000, \"window_seconds\": 60, \"by\": \"user\"},\n    },\n    # Resource-intensive endpoints\n    \"expensive\": {\n        \"search\": {\"requests\": 10, \"window_seconds\": 60, \"by\": \"user\"},\n        \"export\": {\"requests\": 5, \"window_seconds\": 3600, \"by\": \"user\"},\n        \"bulk_import\": {\"requests\": 2, \"window_seconds\": 3600, \"by\": \"user\"},\n    },\n    # Global limits\n    \"global\": {\n        \"per_ip\": {\"requests\": 1000, \"window_seconds\": 60, \"by\": \"ip\"},\n        \"per_user\": {\"requests\": 5000, \"window_seconds\": 3600, \"by\": \"user\"},\n    },\n}\n```\n\n### Step 2: Sliding Window Rate Limiter (Redis)\n\n```python\nimport redis\nimport time\nimport hashlib\nfrom functools import wraps\nfrom flask import Flask, request, jsonify, g\n\napp = Flask(__name__)\nredis_client = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)\n\nclass SlidingWindowRateLimiter:\n    \"\"\"Sliding window rate limiter using Redis sorted sets.\"\"\"\n\n    def __init__(self, redis_conn):\n        self.redis = redis_conn\n\n    def is_allowed(self, key, max_requests, window_seconds):\n        \"\"\"Check if request is allowed and record it.\"\"\"\n        now = time.time()\n        window_start = now - window_seconds\n        pipe = self.redis.pipeline()\n\n        # Remove expired entries\n        pipe.zremrangebyscore(key, 0, window_start)\n        # Count requests in current window\n        pipe.zcard(key)\n        # Add current request\n        pipe.zadd(key, {f\"{now}:{hashlib.md5(str(now).encode()).hexdigest()[:8]}\": now})\n        # Set TTL on the key\n        pipe.expire(key, window_seconds + 1)\n\n        results = pipe.execute()\n        current_count = results[1]\n\n        if current_count >= max_requests:\n            # Calculate retry-after\n            oldest = self.redis.zrange(key, 0, 0, withscores=True)\n            if oldest:\n                retry_after = int(oldest[0][1] + window_seconds - now) + 1\n            else:\n                retry_after = window_seconds\n            return False, current_count, max_requests, retry_after\n\n        return True, current_count + 1, max_requests, 0\n\nrate_limiter = SlidingWindowRateLimiter(redis_client)\n\ndef rate_limit(max_requests, window_seconds, key_func=None):\n    \"\"\"Decorator for rate limiting API endpoints.\"\"\"\n    def decorator(f):\n        @wraps(f)\n        def wrapped(*args, **kwargs):\n            # Determine the rate limit key\n            if key_func:\n                identifier = key_func()\n            elif hasattr(g, 'user_id'):\n                identifier = f\"user:{g.user_id}\"\n            else:\n                identifier = f\"ip:{request.remote_addr}\"\n\n            key = f\"ratelimit:{request.endpoint}:{identifier}\"\n            allowed, current, limit, retry_after = rate_limiter.is_allowed(\n                key, max_requests, window_seconds)\n\n            # Always set rate limit headers\n            headers = {\n                \"X-RateLimit-Limit\": str(limit),\n                \"X-RateLimit-Remaining\": str(max(0, limit - current)),\n                \"X-RateLimit-Reset\": str(int(time.time()) + window_seconds),\n            }\n\n            if not allowed:\n                headers[\"Retry-After\"] = str(retry_after)\n                response = jsonify({\n                    \"error\": \"rate_limit_exceeded\",\n                    \"message\": \"Too many requests. Please try again later.\",\n                    \"retry_after\": retry_after\n                })\n                response.status_code = 429\n                for h, v in headers.items():\n                    response.headers[h] = v\n                return response\n\n            response = f(*args, **kwargs)\n            for h, v in headers.items():\n                response.headers[h] = v\n            return response\n        return wrapped\n    return decorator\n\n# Apply rate limiting to endpoints\n@app.route('\u002Fapi\u002Fv1\u002Fauth\u002Flogin', methods=['POST'])\n@rate_limit(max_requests=5, window_seconds=60,\n            key_func=lambda: f\"ip:{request.remote_addr}\")\ndef login():\n    # Login logic\n    return jsonify({\"message\": \"Login successful\"})\n\n@app.route('\u002Fapi\u002Fv1\u002Fusers\u002Fme', methods=['GET'])\n@rate_limit(max_requests=60, window_seconds=60)\ndef get_profile():\n    # Profile logic\n    return jsonify({\"user\": \"data\"})\n\n@app.route('\u002Fapi\u002Fv1\u002Fsearch', methods=['GET'])\n@rate_limit(max_requests=10, window_seconds=60)\ndef search():\n    # Search logic\n    return jsonify({\"results\": []})\n```\n\n### Step 3: Token Bucket Rate Limiter\n\n```python\nimport redis\nimport time\n\nclass TokenBucketRateLimiter:\n    \"\"\"Token bucket rate limiter allowing burst traffic within limits.\"\"\"\n\n    def __init__(self, redis_conn):\n        self.redis = redis_conn\n\n    def is_allowed(self, key, max_tokens, refill_rate, refill_interval=1):\n        \"\"\"\n        Token bucket algorithm:\n        - max_tokens: Maximum burst capacity\n        - refill_rate: Tokens added per refill_interval\n        - refill_interval: Seconds between refills\n        \"\"\"\n        now = time.time()\n        bucket_key = f\"tb:{key}\"\n\n        # Lua script for atomic token bucket operation\n        lua_script = \"\"\"\n        local key = KEYS[1]\n        local max_tokens = tonumber(ARGV[1])\n        local refill_rate = tonumber(ARGV[2])\n        local refill_interval = tonumber(ARGV[3])\n        local now = tonumber(ARGV[4])\n\n        local bucket = redis.call('hmget', key, 'tokens', 'last_refill')\n        local tokens = tonumber(bucket[1])\n        local last_refill = tonumber(bucket[2])\n\n        if tokens == nil then\n            tokens = max_tokens\n            last_refill = now\n        end\n\n        -- Refill tokens\n        local elapsed = now - last_refill\n        local refills = math.floor(elapsed \u002F refill_interval)\n        if refills > 0 then\n            tokens = math.min(max_tokens, tokens + (refills * refill_rate))\n            last_refill = last_refill + (refills * refill_interval)\n        end\n\n        local allowed = 0\n        if tokens >= 1 then\n            tokens = tokens - 1\n            allowed = 1\n        end\n\n        redis.call('hmset', key, 'tokens', tokens, 'last_refill', last_refill)\n        redis.call('expire', key, math.ceil(max_tokens \u002F refill_rate * refill_interval) + 10)\n\n        return {allowed, tokens, max_tokens}\n        \"\"\"\n\n        result = self.redis.eval(lua_script, 1, bucket_key,\n                                  max_tokens, refill_rate, refill_interval, now)\n        allowed = bool(result[0])\n        remaining = int(result[1])\n        limit = int(result[2])\n\n        return allowed, remaining, limit\n```\n\n### Step 4: Tiered Rate Limiting with User Plans\n\n```python\nfrom enum import Enum\n\nclass UserTier(Enum):\n    FREE = \"free\"\n    PREMIUM = \"premium\"\n    ENTERPRISE = \"enterprise\"\n\nTIER_LIMITS = {\n    UserTier.FREE: {\n        \"default\": (60, 60),          # 60 req\u002Fmin\n        \"search\": (10, 60),           # 10 req\u002Fmin\n        \"export\": (5, 3600),          # 5 req\u002Fhour\n        \"daily_total\": (1000, 86400), # 1000 req\u002Fday\n    },\n    UserTier.PREMIUM: {\n        \"default\": (300, 60),\n        \"search\": (50, 60),\n        \"export\": (20, 3600),\n        \"daily_total\": (10000, 86400),\n    },\n    UserTier.ENTERPRISE: {\n        \"default\": (1000, 60),\n        \"search\": (200, 60),\n        \"export\": (100, 3600),\n        \"daily_total\": (100000, 86400),\n    },\n}\n\ndef get_rate_limit_for_request(user_tier, endpoint_category=\"default\"):\n    \"\"\"Get rate limit configuration based on user tier and endpoint.\"\"\"\n    tier_config = TIER_LIMITS.get(user_tier, TIER_LIMITS[UserTier.FREE])\n    limit_config = tier_config.get(endpoint_category, tier_config[\"default\"])\n    return limit_config  # (max_requests, window_seconds)\n\nclass TieredRateLimitMiddleware:\n    \"\"\"Middleware that applies rate limits based on user subscription tier.\"\"\"\n\n    def __init__(self, app, redis_conn):\n        self.app = app\n        self.limiter = SlidingWindowRateLimiter(redis_conn)\n\n    def __call__(self, environ, start_response):\n        # Extract user info from request\n        user_id = environ.get(\"HTTP_X_USER_ID\")\n        user_tier = UserTier(environ.get(\"HTTP_X_USER_TIER\", \"free\"))\n        endpoint = environ.get(\"PATH_INFO\", \"\u002F\")\n\n        # Determine endpoint category\n        category = \"default\"\n        if \"\u002Fsearch\" in endpoint:\n            category = \"search\"\n        elif \"\u002Fexport\" in endpoint:\n            category = \"export\"\n\n        max_requests, window = get_rate_limit_for_request(user_tier, category)\n        key = f\"tiered:{user_id or environ.get('REMOTE_ADDR')}:{category}\"\n\n        allowed, current, limit, retry_after = self.limiter.is_allowed(\n            key, max_requests, window)\n\n        if not allowed:\n            status = \"429 Too Many Requests\"\n            headers = [\n                (\"Content-Type\", \"application\u002Fjson\"),\n                (\"Retry-After\", str(retry_after)),\n                (\"X-RateLimit-Limit\", str(limit)),\n                (\"X-RateLimit-Remaining\", \"0\"),\n            ]\n            start_response(status, headers)\n            body = f'{{\"error\":\"rate_limit_exceeded\",\"retry_after\":{retry_after},\"tier\":\"{user_tier.value}\"}}'\n            return [body.encode()]\n\n        return self.app(environ, start_response)\n```\n\n### Step 5: Distributed Rate Limiting for Microservices\n\n```python\n# Centralized rate limiting service using Redis Cluster\nimport redis\nfrom redis.cluster import RedisCluster\n\nclass DistributedRateLimiter:\n    \"\"\"Rate limiter for microservice architectures using Redis Cluster.\"\"\"\n\n    def __init__(self):\n        self.redis = RedisCluster(\n            startup_nodes=[\n                {\"host\": \"redis-node-1\", \"port\": 6379},\n                {\"host\": \"redis-node-2\", \"port\": 6379},\n                {\"host\": \"redis-node-3\", \"port\": 6379},\n            ],\n            decode_responses=True\n        )\n\n    def check_and_increment(self, service_name, user_id, endpoint,\n                             max_requests, window_seconds):\n        \"\"\"Atomic check-and-increment using Redis Lua script.\"\"\"\n        key = f\"rl:{{{service_name}}}:{user_id}:{endpoint}\"\n\n        # Lua script ensures atomicity across the check and increment\n        lua_script = \"\"\"\n        local key = KEYS[1]\n        local max_requests = tonumber(ARGV[1])\n        local window = tonumber(ARGV[2])\n        local now = tonumber(ARGV[3])\n        local window_start = now - window\n\n        -- Remove old entries\n        redis.call('zremrangebyscore', key, '-inf', window_start)\n\n        -- Count current entries\n        local count = redis.call('zcard', key)\n\n        if count >= max_requests then\n            -- Get oldest entry for retry-after calculation\n            local oldest = redis.call('zrange', key, 0, 0, 'WITHSCORES')\n            local retry_after = 0\n            if #oldest > 0 then\n                retry_after = math.ceil(tonumber(oldest[2]) + window - now)\n            end\n            return {0, count, retry_after}\n        end\n\n        -- Add new entry\n        redis.call('zadd', key, now, now .. ':' .. math.random(100000))\n        redis.call('expire', key, window + 1)\n\n        return {1, count + 1, 0}\n        \"\"\"\n\n        result = self.redis.eval(lua_script, 1, key,\n                                  max_requests, window_seconds, time.time())\n        return {\n            \"allowed\": bool(result[0]),\n            \"current\": int(result[1]),\n            \"retry_after\": int(result[2]),\n        }\n```\n\n## Key Concepts\n\n| Term | Definition |\n|------|------------|\n| **Sliding Window** | Rate limiting algorithm that tracks requests in a rolling time window, providing smoother rate enforcement than fixed windows |\n| **Token Bucket** | Algorithm where tokens are added at a fixed rate and consumed per request, allowing controlled bursts up to the bucket capacity |\n| **Fixed Window** | Simplest rate limiting where requests are counted per fixed time window (e.g., per minute), susceptible to burst at window boundaries |\n| **429 Too Many Requests** | HTTP status code indicating the client has exceeded the rate limit, accompanied by Retry-After header |\n| **Retry-After Header** | HTTP response header telling the client how many seconds to wait before retrying, essential for well-behaved API clients |\n| **Distributed Rate Limiting** | Rate limiting across multiple server instances using shared state (Redis, Memcached) to maintain accurate global counters |\n\n## Tools & Systems\n\n- **Redis**: In-memory data store used for distributed rate limit counters with atomic operations via Lua scripts\n- **Kong Rate Limiting Plugin**: API gateway plugin supporting fixed-window and sliding-window rate limiting with Redis backend\n- **express-rate-limit**: Express.js middleware for simple rate limiting with Redis, Memcached, or in-memory stores\n- **Flask-Limiter**: Flask extension for rate limiting with support for multiple backends and configurable limits per endpoint\n- **Envoy Rate Limit Service**: Centralized rate limiting service for Envoy-based service mesh architectures\n\n## Common Scenarios\n\n### Scenario: Implementing Rate Limiting for a Public API\n\n**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.\n\n**Approach**:\n1. Deploy Redis Cluster (3 nodes) for distributed rate limit state\n2. Implement sliding window rate limiter using Redis sorted sets with Lua scripts for atomicity\n3. Configure per-tier limits: Free (60 req\u002Fmin), Premium (300 req\u002Fmin), Enterprise (1000 req\u002Fmin)\n4. Add stricter limits on authentication endpoints (5 req\u002Fmin per IP) regardless of tier\n5. Implement resource-intensive endpoint limits (search: 10 req\u002Fmin free, export: 5 req\u002Fhour)\n6. Set rate limit response headers on every response (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset)\n7. Return 429 with Retry-After header and JSON error body when limits are exceeded\n8. Set up Prometheus metrics for rate limit hits and CloudWatch alarms for unusual patterns\n\n**Pitfalls**:\n- Using in-memory rate limiting without shared state across instances, allowing limit bypass by hitting different servers\n- Not implementing rate limiting on authentication endpoints separately from general API limits\n- Using fixed windows that allow burst at window boundaries (2x the limit in a short period)\n- Not including rate limit headers on successful responses, giving clients no visibility into their quota\n- Trusting X-Forwarded-For for IP identification without validating it against the load balancer\n\n## Output Format\n\n```\n## Rate Limiting Implementation Report\n\n**API**: Public API v2\n**Algorithm**: Sliding Window (Redis Sorted Sets)\n**Backend**: Redis Cluster (3 nodes)\n**Deployment**: 6 API instances behind AWS ALB\n\n### Rate Limit Configuration\n\n| Tier | Default | Search | Export | Auth (per IP) |\n|------|---------|--------|--------|---------------|\n| Free | 60\u002Fmin | 10\u002Fmin | 5\u002Fhour | 5\u002Fmin |\n| Premium | 300\u002Fmin | 50\u002Fmin | 20\u002Fhour | 5\u002Fmin |\n| Enterprise | 1000\u002Fmin | 200\u002Fmin | 100\u002Fhour | 10\u002Fmin |\n\n### Validation Results (k6 load test)\n\n- Free tier: Rate limited at 61st request (correct)\n- Premium tier: Rate limited at 301st request (correct)\n- Cross-instance: Rate limiting consistent across all 6 instances\n- Redis failover: Rate limiting degrades gracefully (allows traffic) when Redis is unreachable\n- Retry-After header: Accurate within 1 second of actual reset time\n- Response overhead: \u003C 2ms added latency per request for rate limit check\n```\n",{"hide":44,"name":5,"source":45,"license":15,"description":7,"authorized_lab":40,"origin_frontmatter":46},true,"mukul975\u002FAnthropic-Cybersecurity-Skills","name: implementing-api-rate-limiting-and-throttling | description: 'Implements API rate limiting and throttling controls using token bucket, |   sliding window, and fixed window algorithms to protect against brute force attacks, |   credential stuffing, resource exhaustion, and API abuse. The engineer configures |   per-user, per-IP, and per-endpoint rate limits using Redis-backed counters, API |   gateway plugins, or application middleware, and implements proper HTTP 429 responses |   with Retr","main","4c6d8d65f3d6096e2fc25b0845e481dda73c2794",".claude\u002Fskills\u002Fcyber-implementing-api-rate-limiting-and-throttling",0,"https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os","https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions","Building deterministic AI governance and continuity systems. Workflow architect + visual storyteller","2026-11-14 03:05:17",{"source":8,"skillName":5,"commands":56,"prompt":66,"zipAvailable":44},[57,60,63],{"tool":58,"command":59},"npx","npx skills add nexuslinkproductions\u002Fyuri-os --skill cyber-implementing-api-rate-limiting-and-throttling",{"tool":61,"command":62},"bunx","bunx skills add nexuslinkproductions\u002Fyuri-os --skill cyber-implementing-api-rate-limiting-and-throttling",{"tool":64,"command":65},"pnpm","pnpm dlx skills add nexuslinkproductions\u002Fyuri-os --skill cyber-implementing-api-rate-limiting-and-throttling","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fcyber-implementing-api-rate-limiting-and-throttling\u002FSKILL.md",[],[69,78,85,92,99,106],{"skillNo":70,"name":71,"nameZh":6,"description":72,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":73,"licenseFileUrl":6,"sourceHtmlUrl":74,"categories":75,"keywords":76,"publishedAt":77},"01M73K12W8K0GSTB4ABKS8BD7H","executing-plans","Use when you have a written implementation plan to execute in a separate session with review checkpoints","MIT","https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fexecuting-plans\u002FSKILL.md",[],[],"2026-11-11 15:11:14",{"skillNo":79,"name":80,"nameZh":6,"description":81,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":73,"licenseFileUrl":6,"sourceHtmlUrl":82,"categories":83,"keywords":84,"publishedAt":77},"01M73HC2NSXYYN54GBASVG2RDJ","using-git-worktrees","Use when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback","https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Fusing-git-worktrees\u002FSKILL.md",[],[],{"skillNo":86,"name":87,"nameZh":6,"description":88,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":73,"licenseFileUrl":6,"sourceHtmlUrl":89,"categories":90,"keywords":91,"publishedAt":77},"01M73HC12557CB08VDEF1YTF3H","triage","Triage issues through a state machine driven by triage roles. Use when user wants to create an issue, triage issues, review incoming bugs or feature requests, prepare issues for an AFK agent, or manage issue workflow.","https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Ftriage\u002FSKILL.md",[],[],{"skillNo":93,"name":94,"nameZh":6,"description":95,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":73,"licenseFileUrl":6,"sourceHtmlUrl":96,"categories":97,"keywords":98,"publishedAt":77},"01M73HBXB8F8ED4KQXVXEWCV8B","trade-edge-audit","The EDGE LENS for trading peers — recalls EVERY factor\u002Findicator running from the live forecast ledger (no enumeration) and renders the whole pile through the edge arsenal off ONE call: multi-horizon scoring (the ladder) + deflated Sharpe (overfit) + Benjamini-Hochberg FDR (multiple-testing across the fleet) + spread-bounce + maker-fee falsification, then ranks only what survives ALL of it. Use when a peer must answer \"does ANY running factor have real, overfit-corrected, multiple-testing-corrected, fee-surviving edge, and at which horizon\" without hand-assembling 6 tools. The trade-engine analog of quantum-hypothesis-simulation.","https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Ftrade-edge-audit\u002FSKILL.md",[],[],{"skillNo":100,"name":101,"nameZh":6,"description":102,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":73,"licenseFileUrl":6,"sourceHtmlUrl":103,"categories":104,"keywords":105,"publishedAt":77},"01M73HBT5ZRCA82SQJ781NWVNQ","trade-decision-sim","The DECISION LENS for trading peers — recalls the CURRENT signal snapshot per market from the live ledger and renders it through the sim arsenal off ONE call: quantum factor-circuit order-optimal sequencing + decision-sim CVaR-robust sizing, sized against the MEASURED per-factor edge (from the edge-audit), correlation-aware. Emits an actionable call per market {side, sizePct, confidence, sequence}. Flat\u002Fzero-size when there is no measured edge after cost — the honest default. Sibling of trade-edge-audit (the EDGE lens); the trade-engine analog of quantum-hypothesis-simulation for a live decision.","https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Ftrade-decision-sim\u002FSKILL.md",[],[],{"skillNo":107,"name":108,"nameZh":6,"description":109,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":73,"licenseFileUrl":6,"sourceHtmlUrl":110,"categories":111,"keywords":112,"publishedAt":77},"01M73HBN2WTKGN790WYQD6GBM4","to-issues","Break a plan, spec, or PRD into independently-grabbable issues on the project issue tracker using tracer-bullet vertical slices. Use when user wants to convert a plan into issues, create implementation tickets, or break down work into issues.","https:\u002F\u002Fgithub.com\u002Fnexuslinkproductions\u002Fyuri-os\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Fto-issues\u002FSKILL.md",[],[]]