0 day streak

Rate limiter

A distributed rate limiter is the service that enforces rules like “at most 100 requests per minute per user” across a whole fleet of servers, not just one. Each user gets something like a token bucket: a counter that refills at a steady rate, and each request spends a token or gets rejected. The hard part is that the count has to be shared, usually in a fast store such as Redis, and the check-and-spend must be atomic, otherwise two servers can both read “one token left” and both let a request through. You also have to decide what happens when that shared store is unreachable: fail open and risk overload, or fail closed and block legitimate users.

In this lesson: Distributed token buckets.