Try
↑ ↓ move↵ openesc close

Interview prep / Rate limiting algorithms

Senior · Rate limiting algorithms · System design interview question

Design the rate limiting for a public API with a free tier and a paid tier, where some endpoints cost far more than others.

Sketch your own answer first, then compare it with the one below.

A strong answer

Start from what the limit is for. A free tier limit protects the service and nudges people to pay. A paid tier limit is a contract. Those want different algorithms and different failure behaviour.

Key the limit by account or API key, not by address, with a coarser limit by address as a guard against anonymous abuse. Give each tier its own rate and bucket size. A token bucket suits the rate, because paid users can burst and the bucket size is the burst you promise. For a hard monthly quota, count separately and exactly.

Charge by cost, not by count. A bulk export should take many tokens and a lookup one, so that one expensive call cannot hide behind a per-request limit. Keep a lower, separate limit on the costly endpoints.

Return 429 with Retry-After and headers for the limit and remaining allowance. Keep the limiter fast, local where possible, with a shared store for the exact counts. Decide failure modes per limit: open for the protective one, closed for the quota. Add a layer of load shedding behind it, because limits per caller do not stop the sum of many callers from overloading the service.

Naming "token bucket" is the easy part. The reasoning is in why a paid tier wants burst room and a free tier wants a tight rate.

Sources

Learn it by running it

The rate limiting algorithms lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms