Lesson 9 of 14
25. Rate Limiting

Memory card and where the analogy breaks

3 min read+10 XP

The memory card (tear this page out)

#QuestionMemory code
25.1Why limit?Limit the few so the room works for everyone
25.2Algorithms?Passes refill, queues drip, clickers reset, logs remember, and two clickers estimate
25.3Comparing them?Bucket by default, sliding for precision, leaky to protect the fragile
25.4Distributed with Redis?One list for every door, updated in one move
25.5Where to limit?Gate for floods, door for tickets, bar for drinks
25.6429 and headers?Say "too many", say how many are left, and say when to come back
25.7Bots and abuse?Limit what the attacker wants, not just who they claim to be

The whole chapter in one line: Use a token bucket (or a sliding window counter) per client, keep the count in one atomic shared place, limit at the layer that understands the key, answer 429 with when to retry, and key abuse limits on the thing being attacked.

Where the analogy breaks

  • A bouncer can see faces. An API sees only keys, tokens and IP addresses, and many users can share one IP while one attacker can use thousands.
  • A club's passes are physical, and you can't take two at once. A shared counter isn't: without an atomic update, two servers can hand out the same last pass.
  • A turned-away guest goes home. A rejected program retries immediately unless you tell it when to come back.

Quick check

Five questions on this chapter. Pick an answer, then check it.

quiz