1. Which rate limiting algorithm would you use for a public API, and where would you enforce it?
Mid-levelrate-limitingapi-gateway
What the interviewer is really testing
Whether you can choose an algorithm from the requirements (burst tolerance, memory, accuracy) instead of reciting all five, and whether you know that limits belong at more than one layer.
Show the model answer, follow-ups and checklist
Model answer
I would start from what the limit is for. On a public API the goals are usually to stop one customer hurting the others and to give customers a quota they can reason about. Honest clients are bursty: one page load can fire ten calls at once. So I want an algorithm that tolerates short bursts and enforces an average.
That points to a token bucket per API key, for example refilling at 100 tokens per second with a capacity of 200. It needs two numbers per key, it is cheap to evaluate, and its two parameters are exactly what I would publish in the documentation. Expensive endpoints can cost more than one token.
I would not use a fixed window for the main limit, because a client can spend two quotas back to back across a window boundary. For small, strict limits, such as five login attempts per fifteen minutes per account, I would use a sliding window log, which is exact and cheap at that size. If a downstream needed a constant rate, I would queue in front of it (a leaky bucket) instead of rejecting.
Where to enforce it:
- At the API gateway for the per-key limit, so that rejection happens before any service does work.
- At the edge, a coarser per-IP limit for unauthenticated traffic.
- In the service, limits that depend on business meaning, such as exports per day on a given plan.
What the client sees: 429 with a Retry-After header, plus remaining-quota headers so that well-behaved clients can pace themselves.
With more than one gateway node, the counters go in Redis and each check is a single atomic operation. I would fail open if Redis is unreachable, because a short period without limits is better than rejecting all traffic, and I would say so explicitly as a trade-off.
Likely follow-ups
- Why not simply limit by IP address?
- Many users share one address behind a corporate NAT or a mobile carrier, so an IP limit punishes the innocent, and an attacker can rotate addresses. Use an IP limit only for unauthenticated traffic, as a coarse first filter. For authenticated traffic an API key or user id is the fair unit.
- How would you pick the actual numbers for rate and burst?
- From capacity and observed usage. Measure what the backend can sustain, look at the high percentiles of legitimate per-customer traffic, and set the limit above normal use but low enough that a few misbehaving customers cannot consume the headroom. Run the limiter in log-only mode first to see who would have been blocked.
- What is the difference between rate limiting and load shedding?
- Rate limiting enforces a policy per caller, whether or not the system is busy, and answers with 429. Load shedding protects the server when total demand exceeds capacity, based on signals such as queue depth or concurrency, and answers with 503 whoever is calling. A public API needs both.
- A customer says they are being limited while under their quota. How do you investigate?
- Find out which limit rejected them, which is why responses and logs should name the policy and the key. Common causes are several of their services sharing one key, their own retries counting against the quota, a per-IP limit at the edge catching an office behind one address, or a burst that exceeds the bucket size even though the average is fine.
Common mistakes
- Listing all five algorithms without choosing one or tying the choice to a requirement.
- Proposing a fixed window and not mentioning that it allows twice the limit across a boundary.
- Enforcing only by IP address.
- Never saying what the client receives: the status code and how long to wait.
- Keeping counters in each gateway's memory without noticing that this multiplies the limit by the number of gateways.
A strong answer includes
- Starts from the purpose of the limit and the shape of legitimate traffic
- Chooses token bucket for the main limit and states a rate and a burst size
- Explains why a fixed window was rejected
- Uses a different algorithm where the need differs, such as an exact log for login attempts
- Places per-key limits at the gateway and business limits in the service
- Returns 429 with Retry-After
- Mentions shared, atomically updated counters and a fail-open or fail-closed decision
Revise: Rate limiting algorithms