System-Design-Notes

[System Design] Rate Limiter

High-Level Design

Untitled

Rate limiter middleware loads rules from the cache. It fetches counters and last request timestamp from Redis.

Token bucket algorithm

Untitled

Bucket size: the max number of tokens; Refill rate: number of tokens put into.

Leaking bucket algorithm

Untitled

Bucket size: equal to the queue size; Outflow rate: processing speed.

Sliding window log algorithm

Untitled

Keep track of request timestamps using Redis; Remove outdated timestamps when new requests come; limit according to the log size.

Sliding window counter algorithm

Untitled

requests in the rolling window = Requests in current window + requests in the previous window * overlap percentage of the rolling window and previous window.