rgoussu@goussu: ~/library/system-design/exercises
~/library/system-design/exercises cat load-balancer-and-rate-limiter.md

Build a load balancer & rate limiter

# Build an L7 load balancer and a rate limiter as separate projects, then compose them into a small API edge — health checks, burst-tested algorithms, distributed counters, and a measured p99.

Exercisesaved 2026-08-08source #exercise#load-balancing#rate-limiting#networking#http#resilience

Goal

Build the two guardrails of an API edge — a reverse-proxying load balancer and a rate limiter (both are John Crickett Coding Challenges) — then compose them into one small edge tier. Proves the boxes at the border of every architecture diagram are things you can build, break, and measure, not just draw.

Subject: full brief & instructions

Practices

Milestones

  1. Reverse proxy — forward HTTP to a single backend, relaying headers and status faithfully; diff responses direct vs. proxied to prove transparency.
  2. Round robin — a static backend list with rotation; a dead backend produces a 502, not a crash.
  3. Health checks — periodic checks eject an unhealthy backend and re-admit it on recovery; kill a backend under live traffic and watch requests reroute.
  4. Rate limiter, in process — token bucket and fixed window behind one interface; unit-test the edge-of-window double burst fixed window allows and token bucket doesn't.
  5. Sliding window — add sliding window log and counter; chart what each algorithm lets through under the same bursty client.
  6. Compose the edge — per-API-key limiting inside the balancer: 429 + Retry-After + rate-limit headers, so a well-behaved client can back off.
  7. Distributed counters — run two balancer instances sharing Redis-backed counters (atomic via Lua); measure accuracy vs. latency against per-node local buckets.
  8. Load test — drive it with wrk2 through the edge vs. direct to the backend; report the p99 overhead your edge costs.

Stretch goals

  • Least-connections and weighted balancing strategies.
  • Graceful drain: finish in-flight requests on shutdown before removing a node.
  • WebSocket passthrough — Upgrade support, generous idle timeouts, sticky routing (WebSockets & bidirectional protocols).
  • Adaptive concurrency limits (AIMD) instead of fixed quotas.

Related