Rate Limiting And Throttling
Asked of: Software Engineer
Last updated

What's being tested
Rate limiting and throttling questions test whether you can design a correct distributed admission-control mechanism under concurrency, retries, bursts, and partial failures. The interviewer is probing algorithm selection, state placement, atomicity, multi-instance behavior, and the distinction between rejecting, delaying, and degrading work. DoorDash services must protect checkout, delivery orchestration, authentication, and partner APIs from traffic spikes without turning transient overload into cascading failure, so the key skill is balancing client fairness, system safety, and latency.
Core knowledge
-
Rate limiting bounds request volume over time; throttling is broader and may delay, queue, shed, or reduce work. Clarify whether the requirement is “N requests per minute,” “N concurrent requests,” a byte limit, or a downstream-capacity limit.
-
The token bucket stores tokens replenished at rate , up to capacity . Each request consumes tokens; it allows bursts up to while maintaining long-term throughput near . It is usually the strongest default for APIs.
-
The leaky bucket drains at a fixed rate, smoothing output and limiting downstream pressure. It provides predictable shaping but can add queueing latency; define behavior when the queue is full: reject, drop oldest, or drop newest.
-
A fixed window counter is simple and cheap but permits boundary bursts: a client can issue nearly requests across adjacent windows. Add a distributed atomic increment and expiration, but do not present it as precise fairness.
-
A sliding-window log records timestamps and removes entries older than the interval, giving accurate enforcement at the cost of state per key. A sliding-window counter approximates this using adjacent buckets and is often a better large-scale tradeoff.
-
For multiple service instances, local in-memory state is fast but inconsistent across replicas. Shared state in
Rediscan coordinate decisions, provided token updates use an atomic Lua script or an equivalent server-side operation rather than separate read-modify-write commands. -
A limiter key should match the protection and fairness boundary:
user_id, API key, IP, restaurant, merchant, route, or a tuple such as(tenant, endpoint). Use hierarchical limits when needed: per-user, per-tenant, global, and downstream-resource limits. -
Atomicity matters at the exact decision point: calculate elapsed time, refill tokens, test availability, decrement, and set expiration as one operation. Otherwise concurrent requests can overspend tokens or reset a counter incorrectly.
-
Return
429 Too Many Requestsfor policy-based rejection and includeRetry-Afterwhen a retry time is meaningful. Distinguish this from503 Service Unavailable, which signals server or dependency incapacity; clients may apply different retry policies. -
A distributed limiter must define failure behavior. Fail-open preserves availability but risks overload; fail-closed protects dependencies but can cause broad outages. A bounded local fallback, short state TTLs, and circuit breakers often provide a safer hybrid.
-
Retries can multiply traffic: one logical request may generate several physical requests. Combine rate limits with idempotency keys, exponential backoff with jitter, request deadlines, and bounded retries so throttling does not create a retry storm.
-
Fairness and capacity are different goals. A global bucket protects total throughput, while per-client buckets prevent one noisy customer from monopolizing it. Weighted costs can charge expensive operations more than cheap reads.
Worked example
Worked example — “Design a distributed rate limiter”
In the first 30 seconds, clarify the limit dimensions, burst allowance, request cost, enforcement scope, acceptable decision latency, and whether rejection or queuing is expected. I would state an assumption such as “each user gets 100 requests per minute with bursts of 20, enforced consistently across service replicas.” I would organize the answer around the algorithm, state and key design, atomic distributed implementation, and operational behavior under failures and overload. A token bucket in front of the protected handler gives an intuitive burst policy, while a Redis Lua script can atomically refill and consume tokens using server time. The key might be (tenant_id, route_class) with TTL cleanup, plus a global bucket for a dependency whose capacity is shared across tenants. I would explicitly trade stronger global consistency against latency and availability: a centralized Redis decision is more accurate but adds a network hop and creates a dependency. On Redis timeout, I would choose a bounded local fallback or fail-closed for a dangerous write path, rather than silently allowing unlimited traffic. I would return 429 with Retry-After, expose allowed/rejected counts and decision latency, and test concurrent requests, clock behavior, failover, and boundary bursts. If I had more time, I would add weighted costs, configuration rollout safeguards, and load tests measuring p99 latency and downstream saturation.
A second angle
Worked example — “Throttle requests to a downstream dependency”
Here the important constraint is not merely fairness per user; it is protecting a dependency with a known concurrency or throughput budget. A concurrency limiter such as a semaphore may be more appropriate than a time-based bucket because ten slow calls can exhaust capacity even when request rate looks acceptable. I would combine a maximum in-flight count with a timeout and possibly a queue bounded by age, rejecting quickly when waiting would violate the caller’s deadline. The design should account for dependency-specific quotas, connection-pool limits, and circuit-breaker state. This framing emphasizes latency and resource occupancy rather than only requests per minute.
Common pitfalls
Pitfall: Saying “use a counter in
Redis” without defining atomicity, expiration, or the window algorithm leaves race conditions and boundary bursts unresolved. Name the exact state transition and make it atomic.
Pitfall: Treating every overload response as
429is analytically wrong. Explain whether the client exceeded a policy limit, the service is temporarily unavailable, or a downstream dependency is saturated; each case needs different retry semantics.
Pitfall: Focusing only on the happy-path algorithm misses production depth. A stronger answer covers clock choice, multi-region behavior, fail-open versus fail-closed policy, hot keys, observability, and how retries interact with the limiter.
Connections
An interviewer may pivot to circuit breakers, backpressure, load shedding, queue-based buffering, or idempotency. They may also ask about multi-region consistency, API gateway enforcement, or capacity planning for Redis hot keys.
Further reading
-
RFC 6585 — Additional HTTP Status Codes — Defines
429 Too Many Requestsand the intended use ofRetry-After. -
“The Token Bucket” — Cisco documentation — Provides practical background on token-bucket policing and shaping.
-
Designing Data-Intensive Applications, Chapter 9 — “Consistency and Consensus” — Useful context for distributed state, failure modes, and consistency tradeoffs.