Notes: This presentation explores a complete production-grade architecture for API rate limiting, fairness, and observability. We’ll go deep into how Redis, FastAPI, Prometheus, Grafana, and Redis Commander come together as a cohesive stack to create an intelligent, self-regulating system. By the end, you’ll understand not just how to prevent overloads but also how to see, measure, and adapt to system behavior in real time.
Notes: APIs often look stable during normal usage — until someone overloads an endpoint. Without intelligent rate control, one client can degrade the experience for everyone else. This architecture enforces fairness and provides real-time visibility into who’s consuming your resources and how effectively you’re throttling requests. The end goal: keep your API reliable, fair, and scalable under any traffic pattern.
Notes:
Notes: Everything runs inside Docker Compose, ensuring reproducibility and easy teardown.
Notes: Redis isn’t just a cache — it’s the enforcer of fairness. It handles every request decision atomically — thanks to its single-threaded nature. Every command executes in total isolation, preventing race conditions. It also supports TTL (Time To Live), allowing automatic key expiry — perfect for rate-limit windows. Redis becomes the decision-making brain behind every “allow” or “block” in the system.
Notes:
FastAPI is asynchronous, and to fully exploit that, we must use redis.asyncio.
If we used a synchronous client, every call to Redis would block the entire event loop — destroying concurrency.
With async Redis, requests yield control while waiting, allowing FastAPI to process hundreds of other connections in parallel.
This is what makes the system scalable and responsive even under heavy load.
Notes: Redis guarantees atomic operations:
INCR or DECR updates happen as a single step.WATCH + MULTI + EXEC ensures consistent multi-key operations without locks.Notes: Rate limits must balance fairness and prevent evasion.
X-API-KEY.Client-IP — solves both.
Each unique combination has its own independent counter.
Anonymous users use anon.Client-IP, providing fair IP-based protection even for unauthenticated traffic.Notes: The simplest algorithm — count requests in a fixed time slice (e.g., 10 seconds).
INCR to increase request count.EXPIRE for automatic reset.
Pros: trivial to implement and very fast.
Cons: boundary bursts — users can send double the allowed requests by hitting both sides of the reset boundary (e.g., 9.9s + 10.1s).
Good for basic protection but not fair under all traffic patterns.Notes: A more accurate and fairer approach. Redis ZSETs store each request timestamp as a scored element. Steps per request:
ZREMRANGEBYSCORE).ZADD).ZCARD).
If the count exceeds the limit → block (429).
It uses more memory but smooths out bursts perfectly — making it ideal for high-traffic APIs.Notes: Models capacity and refill rate rather than counting. Each key has:
capacity (max tokens)refill_rate (tokens/sec)cost (tokens/request)
When a request arrives:Notes:
Prometheus pulls metrics from the FastAPI /metrics endpoint.
Scrapes occur every 5 seconds (configurable).
Metrics include:
request_total: counts requests by endpoint, strategy, and status (200 vs 429).rate_limited_total: counts blocked requests by strategy.request_latency_seconds: a histogram for latency distribution, enabling P50, P90, and P99 percentile calculations.
These reveal both control efficiency and performance overhead.Notes: Grafana visualizes Prometheus data in near-real-time dashboards. Auto-provisioned setup connects to Prometheus out-of-the-box. Panels include:
sum(rate(request_total{status="200"}[1m])) by (endpoint)histogram_quantile(0.99, sum(rate(request_latency_seconds_bucket[5m])) by (endpoint, le)))Notes: A four-layer architecture emerges:
Notes: Two powerful extensions to the core stack:
1. Automation
rate_limited_total).2. Dynamic Tiers
Notes: To cement understanding, try the lab yourself:
docker-compose.yml.Would you like me to: