Token bucket algorithm (token bucket algorithm) current limit

http://blog.csdn.net/sunnyyoona/article/details/51228456

 Watching the QCon conference today, I talked about Alibaba's online management and control system, which mainly uses the token bucket algorithm to achieve the purpose of current limiting. I am very curious, so I will learn what the token bucket algorithm is.

1 Introduction

The token bucket algorithm originally originated from computer networks . When transmitting data on the network, in order to prevent network congestion, it is necessary to limit the flow out of the network, so that the flow is sent out at a relatively uniform speed . The token bucket algorithm implements this function, which can control the amount of data sent to the network and allow burst data to be sent.

The token bucket algorithm is one of the most commonly used algorithms in network traffic shaping (Traffic Shaping) and rate limiting (Rate Limiting). Typically, the token bucket algorithm is used to control the amount of data sent to the network and to allow bursts of data to be sent.

A fixed-size token bucket can generate a steady stream of tokens on its own at a constant rate. If tokens are not consumed, or are consumed faster than they are produced, tokens will keep increasing until the bucket is filled . Subsequent tokens generated will overflow from the bucket. The maximum number of tokens that can be held in the final bucket will never exceed the size of the bucket .

Packets delivered to the token bucket consume tokens. Different size packets consume different number of tokens.

Token Bucket This control mechanism indicates when traffic can be sent based on the presence of tokens in the token bucket. Each token in the token bucket represents a byte. If the token exists in the token bucket, the traffic is allowed to be sent; if the token does not exist in the token bucket, the traffic is not allowed. Therefore, if the burst threshold is properly configured and there are enough tokens in the token bucket, traffic can be sent at the peak rate.

2. Algorithmic process

Algorithm Description:

  • If the average sending rate configured by the user is r, a token is added to the bucket every 1/r second (r tokens are put into the bucket every second);

  • It is assumed that at most b tokens can be stored in the bucket. If the token bucket is full when the token arrives, the token will be discarded;

  • When a packet of n bytes arrives, n tokens are removed from the token bucket (packets of different sizes, the number of tokens consumed is different), and the packet is sent to the network;

  • If there are less than n tokens in the token bucket, the token is not removed and the packet is considered to be outside the traffic limit (n bytes, n tokens required. The packet will be cached or dropped) ;

  • The algorithm allows bursts of up to b bytes, but from the long-run results, the packet rate is limited to a constant r. Packets outside the flow limit can be handled in different ways: (1) they can be dropped; (2) they can be queued for transmission when enough tokens have accumulated in the token bucket; ( 3) They can continue to be sent, but they need to be specially marked, and these specially marked packets will be discarded when the network is overloaded.

Notice:

The token bucket algorithm should not be confused with the leaky bucket algorithm , another common algorithm . The main difference between the two algorithms is that the leaky bucket algorithm can forcibly limit the transmission rate of data , while the token bucket algorithm can limit the average transmission rate of data and also allow a certain degree of burst transmission . In the token bucket algorithm, as long as there are tokens in the token bucket, data is allowed to be transmitted in bursts until the user-configured threshold is reached, so it is suitable for traffic with burst characteristics.

3. Java implementation

We can use Guava's  RateLimiter  to implement token bucket-based flow control, and the RateLimiter token bucket algorithm is a single-bucket implementation. RateLimiter has made some engineering optimizations on the simple token bucket algorithm, the specific implementation is  SmoothBursty . It should be noted that another implementation of RateLimiter, SmoothWarmingUp , is not a token bucket, but a leaky bucket algorithm. Perhaps for simplicity, the time window in RateLimiter can and only be 1s.

SmoothBursty has a bucket that can store tokens generated by N time windows. When the system is idle, the tokens are kept accumulating. In the best case, it can carry a peak of N times the current limit value without affecting subsequent requests. RateLimite allows a request to take more tokens than the number of remaining tokens, but the next request will pay for it until the token deficit is filled and there are enough tokens in the bucket for this request. When a request cannot get the required token, there is a trade-off involved. Should the previous request wait until the token is sufficient before leaving, or should it be allowed to leave the subsequent request and wait? The designers of Guava chose the latter, doing the work in front of you first, and talking about the rest later.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326653819&siteId=291194637