Traffic control based on leaky bucket and token bucket algorithm

Internet services Lai Survival is the basis of traffic, products and operations often through various ways for application, such as Taobao’s double eleven, how to make the system to deal with high consumption or guarantee the stability of their own system, usually The way to improve concurrency in the shortest time is to add a machine, but if the machine is not enough? Then you need to do business degradation or system current limiting, more than two algorithms used in traffic control are drain barrels and token barrels.

Leak Bucket Algorithm (Leaky Bucket)

The drain bar algorithm enforces a constant output rate regardless of the burst of the data stream, when entering idle, the algorithm does not perform any action. Just like a hole in a hole, water is entered. In the drain barrel, the water in the bucket flows out of the following holes, and when the water flow rate is directly overflowing, it can be seen that the drain bar algorithm can force the transmission rate of the data. As shown below:

Token bucket (Token Bucket)

The basic process of the token barrel algorithm is as follows:

There will be R or a token in the bucket per second, or add a token every 1 / r seconds.

The bucket is stored in the bucket, if the bucket is full, the new token will be discarded

When an N-byte packet arrives, consume N token, then send the packet

If the token can be used in the bucket less than n, the packet will be cached or discarded.

Shake bucket and token bucket comparison

The “drain bar algorithm” can force the transmission rate of the data, and the “token barrel algorithm” allows some degree of burst transmission outside of the average transmission data that can limit the data. In the “Token Bucket Algorithm”, as long as there is a token in the token bucket, it is allowed to transmit the data until the upper limit of the user-configured, so it is suitable for traffic with burst characteristics.

Ratelimiter

We can use Guava’s RateLimiter to implement traffic control based on token. The free bucket of the Ratelimiter token barrel algorithm, and RateLimiter has made some engineering optimizations for simple token barrel algorithms, and the specific implementation is SMOothbursty. It should be noted that the other of the RateLimiter implements SmoothWarmingup, which is not a token barrel, but a drain bar algorithm.

Smoothbursty has a bucket that can put N time windows. When the system is idle, the token has been hoping. It is best to carry N times a peak of the limited stream value without affecting the follow-up request, just like the Three Gorges. The dam can be able to have a flood in the millennium.