Token bucket

The token bucket algorithm work as follows:

A token bucket is a container that has pre-defined capacity. Tokens are put in the bucket at preset rates periodically. Once the bucket is full, no more tokens are added.

As shown the token bucket capacity is 4. The refiller puts 2 tokens into the bucket every second.

Once the bucket is full, extra tokens will overflow.

Each request consumes one token. When a request arrives, we check if there are enough tokens in the bucket. Figure 4-5 explains how it works.

If there are enough tokens, we take one token out for each request, and the request goes through.

If there are not enough tokens, the request is dropped.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/79014efe-e6f6-4abd-8a9d-daeade04ae06/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a0f8bd55-76fc-49c0-8a12-d891fcbf8577/Untitled.png

In this example, the token bucket size is 4, and the refill rate is 4 per 1 minute:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d066d1d1-4aaf-491d-b313-3e301c12775a/Untitled.png

The token bucket algorithm takes two parameters:

How many buckets do we need? This varies, and it depends on the rate-limiting rules. Here

are a few examples.

if a user is allowed to make 1 post per second, add 150 friends per day, and like 5 posts per

second, 3 buckets are required for each user.

global bucket shared by all requests.

Pros: