Skip to content

Latest commit

 

History

History
81 lines (61 loc) · 3.14 KB

feature-doc-example.md

File metadata and controls

81 lines (61 loc) · 3.14 KB
title Rate Limiting
type feature
date 2026-06-20
author Jane Doe + Claude (Sonnet 5)
status active
confidence verified
tags
api
rate-limiting
redis
related
docs/sessions/20260612-rate-limiting-rollout.md
docs/sessions/20260620-rate-limiting-overrides.md

What This Is

Per-API-key rate limiting for the public API, backed by a Redis token bucket. Protects the API from abusive clients without needing per-endpoint logic.

Architecture

flowchart LR
    A[Client Request] --> B[RateLimitMiddleware]
    B --> C{Redis reachable?}
    C -->|Yes| D[Check token bucket]
    C -->|No| E[Fail open, log warning]
    D -->|Under limit| F[Handler]
    D -->|Over limit| G[429 Too Many Requests]
    E --> F
%% Generated 2026-06-20 from: src/middleware/rate_limit.py
Loading

Key Components

RateLimitMiddleware

  • Location: src/middleware/rate_limit.py:12
  • Purpose: Intercepts every request, checks the caller's token bucket in Redis before passing through to the handler.
  • Key details: Fails open (lets the request through) if Redis is unreachable, rather than 500ing — see Known Issues for why this was changed.

Token Bucket Config

  • Location: src/middleware/rate_limit.py:45 (DEFAULT_BUCKET)
  • Purpose: Defines the default requests-per-minute and burst size applied to every key.
  • Key details: Per-key overrides live in Redis under ratelimit:override:{key}, checked before falling back to DEFAULT_BUCKET.

API / Endpoints

Method Path Purpose Auth
GET /internal/ratelimit/status/{key} Current bucket state for a key Internal only
PUT /internal/ratelimit/override/{key} Set a custom limit for a key Internal only

Configuration

Variable Purpose Default Required
RATE_LIMIT_ENABLED Kill switch for the whole feature true No
RATE_LIMIT_REDIS_URL Redis instance backing the token buckets redis://localhost:6379/2 No

How to Use

Set a per-customer override: PUT /internal/ratelimit/override/{key} with {"requests_per_minute": 500, "burst": 50}.

Troubleshooting

Symptom Likely Cause Fix
All requests suddenly unlimited Redis unreachable, middleware failed open Check Redis health; this is deliberate degraded behavior, not a bug — see Known Issues
A specific customer still rate-limited after override Override key mismatch Confirm the key matches exactly what RateLimitMiddleware reads at src/middleware/rate_limit.py:12 — it's the API key, not the customer ID

Known Issues

  • Fails open when Redis is down, meaning rate limiting is fully disabled during a Redis outage. This is intentional (⚠️ UNVERIFIED: assumed acceptable tradeoff — not confirmed with product) but means an outage window has zero protection.

Changelog

2026-06-20

  • Added per-key override endpoints (PUT /internal/ratelimit/override/{key})
  • Documented the fail-open behavior and its tradeoff explicitly in Known Issues

2026-06-12

  • Initial feature doc
  • Redis token bucket middleware live for all public API routes