Step 1 · The pattern
An open door
Clicks arrive from anyone on the internet and each costs a lookup. Nothing asks who is sending or how often. With ordinary traffic the service has room to spare.
- click
- redirected
In short
Token bucket rate limit: how it works and how it breaks
A shortener is open to the internet, so anyone can send it requests as fast as their machines allow. One script guessing codes, or creating spam links, can use up most of your capacity, and real clicks start to fail. A rate limit gives each client a budget. The token bucket is the common way to build one. Each client has a bucket that holds a few tokens and is topped up at a steady rate. A request spends one token. If the bucket is empty, the request is refused at once with 429 Too Many Requests, before it costs you a lookup. The bucket's size sets how big a burst you tolerate; the refill rate sets the long-run speed. Ordinary users never notice. A limit slows guessing but does not prevent it: a scanner can spread across many addresses, so private links also need long codes.
How this comes up in interviews
"A public endpoint is being hit by a script far faster than any real user. Design a rate limit that stops it without hurting ordinary users."
Then they ask
- How do you choose the bucket size and the refill rate, and what does each one promise?
- What do you key the limit on, and what happens when the script spreads across many addresses?
- How do you keep buckets for a very large number of clients without running a timer for each one?
A strong answer covers
- Each client has a bucket that holds a few tokens and is topped up at a steady rate. A request spends one token.
- An empty bucket means an immediate 429 Too Many Requests, before the request costs a lookup.
- Bucket size sets how big a burst you tolerate and refill rate sets the long-run speed, so choose them separately.
- Store the token count and the time of the last update per key, and work out the refill when a request arrives.
- A limit slows guessing but does not prevent it, because a scanner can spread across many addresses.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Stripe: Scaling your API with rate limiters ↗ · RFC 6585: 429 Too Many Requests ↗ · Georgiev & Shmatikov: Gone in Six Characters (short-URL scanning) ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.