Lesson 4 of 14
25. Rate Limiting

25.3 How do the algorithms compare?

3 min read+10 XP

At the club: The bowl of passes is friendly to groups. The steady queue is kind to the bar staff. The hourly clicker is easy but gameable at midnight. The guest book is perfect but needs a thick book. Two clickers are the practical middle.

In the system:

AlgorithmBurstsAccuracyMemory per clientBest for
Token bucketAllowed, up to capacityGood2 numbersMost APIs; per-user and per-key limits
Leaky bucketSmoothed into a steady stream (adds delay)GoodQueue or 2 numbersProtecting a fragile downstream; traffic shaping
Fixed windowUp to 2× at boundariesWeak at edges1 counterSimple quotas (per day), low stakes
Sliding logNone beyond the limitExactOne entry per requestLow limits where exactness matters (5 logins per 15 min)
Sliding window counterSmall errorVery close2 countersHigh-volume APIs needing accuracy cheaply

How to choose in an interview:

  • Default to the token bucket: simple, burst-friendly, cheap, and easy to explain.
  • Mention the fixed window's boundary problem and the sliding window counter as the fix if they ask about accuracy.
  • Use a sliding log for small, security-critical limits (login attempts), where memory per key is tiny anyway.
  • Use a leaky bucket when the point is to smooth traffic into something that can't take bursts.

Memory code: "Bucket by default, sliding for precision, leaky to protect the fragile."