System DesignIntermediate12 min

Rate limiting & load shedding

Protect shared resources from abuse and overload with limits that are fair and cheap to enforce.

See it on the map

A rate limiter caps how much one caller can take so a single noisy client cannot starve the rest. The token bucket is the usual choice: tokens refill at a steady rate, each request spends one, and a bucket size allows short bursts. A sliding window gives smoother accuracy at higher cost.

Limits need an identity: API key, user, IP or tenant. IP alone punishes shared networks and is easy to dodge. In a distributed setup the counters live in a shared fast store, and you accept slight inaccuracy in exchange for not adding a hop to every request.

Return a clear 429 with a retry hint. Beyond per-caller limits, load shedding drops low-priority work when the whole system is saturated, which keeps the important paths alive.

Key points

  • Token bucket allows bursts while enforcing an average rate.
  • Pick the limiting identity carefully: key, user, tenant or IP.
  • Return 429 with Retry-After so well-behaved clients back off.
  • Shed low-priority traffic first when the system is saturated.

Answer these out loud

  1. 1Design per-tenant limits for an API with free and paid plans, including burst handling.
  2. 2Your limiter's shared store goes down. Should requests be allowed or denied, and why?

Build it

Implement a token bucket with an injectable clock so you can unit test refill and burst behaviour deterministically.

Use this from an agent

Any MCP client, including Grok, can read this lesson. See how to connect.

REST
curl https://engineeros-delta.vercel.app/api/v1/topics/rate-limiting

This unlocks