Token bucket algorithm

Source: Internet
Author: User
token bucket algorithm (token bucket algorithm)

When implementing a QoS policy, the user's data can be limited to a specific bandwidth, and when the user's traffic exceeds the rated bandwidth, the excess bandwidth is handled in other ways. To measure whether the traffic exceeds the rated bandwidth, network equipment is not a simple digital plus subtraction to determine, that is, such as the bandwidth of 100K, and the user sent the traffic is 110K, network equipment is not by 110K minus 100K equals 10K, it is believed that the user exceeded the traffic 10K. Network equipment

To measure whether the traffic exceeds the rated bandwidth, a token bucket algorithm is used to calculate it. The following is a detailed description of the token bucket algorithm mechanism:

When the network device measures whether the traffic exceeds the rated bandwidth, it needs to look at the token bucket, and the token bucket will place a certain number of tokens, a token allows the interface to send or receive 1bit data (sometimes 1 byty data), when the interface through 1bit data, but also to remove a token from the bucket. When there is no token in the bucket, any traffic is considered to exceed the rated bandwidth, and the data can pass through the interface only if there is a token in the bucket. Tokens in the token bucket can not only be removed, the same can also be added, so in order to ensure that the interface at any time the data through, it must be kept to the bucket to add tokens, this shows that the speed of adding tokens to the bucket, determines the data through the interface speed. Therefore, we control the bandwidth of user traffic by controlling the speed of tokens in the token bucket. The rate at which this user transmits data is known as the Committed Information rate (CIR), which is typically measured in seconds. For example, we set the user's bandwidth to a single bit per second, as long as it is guaranteed to add 1000 tokens to the bucket every second.

Cases:

Set the CIR to 8000 bit/s, then you must put 8,000 tokens per second into the bucket, when the interface has data through, remove the corresponding token from the bucket, each through 1 bit, remove 1 tokens from the bucket. When there is no token in the bucket, any traffic is considered to exceed the rated bandwidth, and the excess traffic will take extra action.

The token that is added to the bucket every second determines the rate of user traffic, which is the CIR, but the total number of tokens that need to be added to the bucket per second is not a one-time addition, and the number of tokens that are put in once is called burst size (BC), and if BC is just half of the CIR, Then it's obvious that you need to add two tokens per second to the bucket, and the number of times you add is always the number of BC.

There is the time of the token, interval, the TC means how long to add a token to the bucket, and this time is not manually set, because this time can be calculated by the CIR and Bc relations, bc/cir= Tc.

Cases:

If the CIR is 8000,BC is 4000, that is, plus two times per second, TC is 4000/8000=0.5, that is, 0.5 seconds, that is, three Ms.

If BC is set to 2000, the TC is 2000/8000=0.25, which is the MS.




First, the application of traffic supervision

The agreed access rate (CAR) is one of the most common techniques used in traffic supervision, which can be applied in the port in and out direction, generally applied in the direction, and its supervision principle is shown in Figure 1.

A. Serving tokens to a token bucket at a specific rate

B. The message is classified according to the preset matching rules, and the message that does not conform to the matching rule does not need to be processed by the token bucket and sent directly;

C. Messages that match the matching rules need to be processed by the token bucket. When there are enough tokens in the bucket, the message can be continued and the token volume in the token bucket is correspondingly reduced by the length of the message;

D. When the token in the token bucket is insufficient, the message cannot be sent until a new token is generated in the bucket and the Wencai can be sent. This limits the amount of traffic that can only be less than or equal to the token generation to limit traffic.

Second, the application of general traffic shaping

A. Serving tokens to a token bucket at a specific rate

B. The message is classified according to the preset matching rules, and the message that does not conform to the matching rule does not need to be processed by the token bucket and sent directly;

C. Messages that match the matching rules need to be processed by the token bucket. When there are enough tokens in the bucket, the message can be continued and the token volume in the token bucket is correspondingly reduced by the length of the message;

D. Buffering of messages exceeding the rate limit when the token is less than the token is sent to the message, the message is cached in the queue, and then sent after having enough tokens.

E. When the token in the token bucket is insufficient, the message cannot be sent until a new token is generated in the bucket and the Wencai can be sent.

The principle of general traffic shaping (GTS) is slightly different from car:

First, the GTS only for the direction of flow speed limit, car access direction can be, but generally more used in the direction;

Second, the use of car traffic control, the speed limit of the message is discarded directly, and GTS is to exceed the rate limit of the message buffer that is, when the token is less than the token to the message can not be sent, the message will be cached into the queue, and so have enough tokens to send, so that the message is reduced to drop, Note, however, that if the cache queue is full, the incoming messages will still be discarded.

Three, the application of the port speed limit

The port speed Limit (LR) (shown in Figure 3) is also used for direction, but unlike GTS: First, GTS and car are implemented at the IP layer, so the messages that do not pass through the IP layer do not work, and LR can limit all messages passed on the physical interface; LR not only caches messages that exceed traffic limits, but also caches messages using QoS-rich queues such as priority queue (PQ), custom Queue (CQ), weighted fair pair (WFQ), and so on.

A. Serving tokens to a token bucket at a specific rate

B. Classify messages according to pre-set matching rules, and cache messages using QoS-rich queues such as priority queue (PQ), custom Queue (CQ), weighted fair pair (WFQ), etc.

C. Messages that match the matching rules need to be processed by the token bucket. When there are enough tokens in the bucket, the message can be continued and the token volume in the token bucket is correspondingly reduced by the length of the message;

D. Buffering of messages exceeding the rate limit when the token is less than the token is sent to the message, the message is cached in the queue, and then sent after having enough tokens.

E. When the token in the token bucket is insufficient, the message cannot be sent until a new token is generated in the bucket and the Wencai can be sent.

Source Document

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.