Rate Limiter Algorithms
Understand the working of various rate limiter algorithms.
Algorithms for rate limiting
The task of a rate limiter is directed by highly efficient algorithms, each of which has distinct advantages and disadvantages. However, there is always a choice to choose an algorithm or combination of algorithms depending on what we need at a given time. While different algorithms are used in addition to those below, we’ll take a look at the following popular algorithms.
Token bucket
Leaking bucket
Fixed window counter
Sliding window log
Sliding window counter
Token bucket algorithm
This algorithm uses the analogy of a bucket with a predefined capacity of tokens. The bucket is periodically filled with tokens at a constant rate. A token can be considered as a packet of some specific size. Hence, the algorithm checks for the token in the bucket each time we receive a request. There should be at least one token to process the request further.
The flow of the token bucket algorithm is as follows:
Assume that we have a predefined rate limit of RRR and the total capacity of the bucket is CCC.
The algorithm adds a new token to the bucket after every R1 seconds.
The algorithm discards the new incoming tokens when the number of tokens in the bucket is equal to the total capacity C of the bucket.
If there are N incoming requests and the bucket has at least N tokens, the tokens are consumed, and requests are forwarded for further processing.
If there are N incoming requests and the bucket has a lower number of tokens, then the number of requests accepted equals the number of available tokens in the bucket.
The following illustration represents the working of the token bucket algorithm.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.