BOOKS DEEP-READ · SYSTEM DESIGN INTERVIEW VOL 1 · CH 4

Design a Rate Limiter

System Design Interview: An Insider's Guide · Vol 1 · Ch 4 · Alex Xu · 2020

中文 →

What is this chapter about?

Plenty of online services hand you a usage cap: the free tier converts five files a day, that forum won't let you post twenty comments in a minute. This chapter is about the little part behind that cap — the rate limiter: a doorman who enforces "at most so many entries per person per minute" and turns away anyone over the line.

Something odd first

You probably assume the door is there to keep bad actors out. Most of what it turns away is well-meaning. More precisely: a well-meaning program with a bug — some customer's software has a loop that forgot to stop and is firing thousands of requests a second. They don't know, they meant no harm, and the effect is identical to an attack. The limiter's day job is to stop one person's slip from becoming everyone's outage.

Why the old world was hard

With no door, everything runs first-come-first-served: whoever shouts loudest gets served. That sounds fair and is brutal in practice — one runaway program can eat a service's entire capacity in seconds, and thousands of perfectly well-behaved users get locked out together. There's a blunter cost too: many services call somebody else's metered API behind the scenes, billed per call, and a loop that won't stop can burn a month's budget overnight.

The mechanism: four ways to guard a door

1. The candy bowl (token bucket). Put a bowl by the door. Two candies drop in every second; the bowl holds at most 40. To get in, take one; empty bowl, no entry. The nice part: if things were quiet for a while the bowl fills up, and then 40 people can walk in at once — it accommodates a short rush, yet over any long stretch the average is still two per second.

2. The queue (leaky bucket). One line at the door. However many people pile up outside, exactly two per second are let in — never more. The pace inside is perfectly even; the price is that once the line is full, latecomers can't even join it and are simply turned away.

3. Reset on the hour (fixed window). The doorman holds a counter that zeroes every minute; five per minute and no more. Simplest possible thing, with one hole: if five people squeeze in at 0:55 and five more at 1:05, then across those few seconds ten actually got in — twice the rule.

4. Look back one minute (sliding window). Change "this minute" to "the minute counting backwards from now," and the hole closes. The cost is that the doorman must remember when each person walked in, and with a crowd that ledger gets thick and expensive. The compromise is to keep no ledger at all and just carry over a fraction of last minute's headcount by overlap — not exactly right, but far cheaper.

How to choose

Want to leave users room to be occasionally hasty? Candy bowl. Need the pace downstream to be perfectly even? The queue. Just want a crude barrier up fast? Reset-on-the-hour takes half an hour to write.

One honest cost: the door itself can break — and when it does, do you let everyone in or shut everyone out? There's no standard answer, but every team shipping a rate limiter has to settle it in advance.

Remember this

A rate limiter is a doorman, and what it mostly guards against isn't attackers — it's one person's slip taking everyone down. Four postures: candy bowl (absorbs bursts), queue (even pace), reset-on-the-hour (cheap but leaks at the boundary), look-back-a-minute (accurate but expensive). Which one you pick comes down to whether you can tolerate bursts.

Want the algorithms, magnitudes and diagrams? → switch to Deep