KatInterview

Rate Limiting an API Under Active Abuse

System DesignModel answer
MediumModel answerSystem Designrate-limitingdistributed-systemsdenial-of-service

Last updated

Design a layered rate limiter that blunts a traffic flood while keeping legitimate clients and anonymous users productive.

Context

Your API is being hammered. Traffic logs have already been analyzed and some client addresses stand out as extreme volumes — but volume alone is not proof of abuse, and a single address can front thousands of real users behind a NAT or corporate proxy. You now have to design the protective layer: rate limiting that absorbs the flood without locking out legitimate customers.

The Task

Describe a rate-limiting design for this API. Your answer should cover:

  • Where enforcement happens. Which tier (CDN/edge, API gateway, per-service) enforces what, and why.
  • What identity to limit on. Address, account, API key, session, endpoint, or some combination — and how you handle unauthenticated traffic and shared egress addresses.
  • The algorithm. How burstiness is handled, not just average rate, and how you'd choose the parameters.
  • Where state lives in a distributed deployment. How multiple gateway nodes share counters without becoming a bottleneck or a single point of failure.
  • Failure behavior. What the system does when the limiter's shared store is slow or unreachable, or when the edge is bypassed.
  • Client-facing behavior. Status codes, headers, retry guidance, and how you tell a throttled client apart from a blocked one.
  • Observability. Which metrics prove the attack is being hindered and which prove you aren't hurting real users.

Constraints & Assumptions

  • No traffic scale, limit values, or request mix are given — state your assumptions and justify numbers.
  • Some endpoints are far more expensive than others (e.g., a search query vs. a health check).
  • Clients may or may not authenticate; anonymous traffic must still be handled.
  • Bursts are sometimes legitimate (batch jobs, app cold start, retries with backoff).

What a Good Answer Demonstrates

Layered limits rather than one magic number, keys that reflect real client identity, burst-tolerant algorithms, an honest discussion of distributed-counter trade-offs, explicit and safe degradation behavior, and monitoring that separates attack mitigation from collateral damage to legitimate users.

Hints

Hint 1

Limits belong at several tiers with different granularities — edge, gateway, per-endpoint — not one global number.

Hint 2

Token bucket (or GCRA/leaky bucket) tolerates bursts; a fixed-window counter does not, and sliding windows cost more memory.

Hint 3

Limiting purely by IP punishes NAT'd users; combine keys (account + endpoint + coarse IP) and weight by endpoint cost.

Hint 4

Decide up front whether your limiter fails open or closed on store outages — and how you keep a local fallback.