As a background service, there is usually a processing limit PPS (packets per second), and if the request exceeds this processing power, there may be an " avalanche effect ", so the backend service needs an overload protection mechanism.
1, there is a simple algorithm can realize the flow control function: Set a unit time (such as 1s, 1min) within the maximum number of visits, and maintain a unit of time in the counter.
When the access request arrives, first determine whether the unit control time has timed out, if it has timed out, reset counter is 0;
Otherwise, the counter is incremented by 1, and the value of the counter is determined to exceed the maximum traffic setting, if it is exceeded, access is denied.
The pseudo code is as follows:
1 Longtimestamp=getnowtime ();2 intReqcount=0;3 Const intMaxreqcount=10000;//maximum number of requests during the time period4 Const Longeffectiveduration=Ten;//Time Control Cycle5 6 BOOLGrant ()7 {8 Longnow=getnowtime ();9 if(Now <timestamp+effectiveduration)Ten { One //within the range of time control Areqcount++; - returnreqcount>maxreqcount;//exceeds maximum request control in the current time range - } the Else - { -Timestamp=now;//Reset after timeout -Reqcount=0; + return true; -}
21}
The implementation of this algorithm is really a requirement of "Maximum traffic control in unit time", but, under careful study, it is found that the processing of the critical value of two unit time is defective.
Such as: Set the maximum request to control the number of 1w, the first unit time in the last second to reach the number of requests is 1w, the second unit time in the first second to reach the number of requests is 1w, because the time-out reset occurs between two units of time,
So this 2w request will be controlled, that is, processing 2w requests in 2s, in violation of the requirements of 10s 1w requests we set.
2. Token bucket algorithm
The principle of the token bucket algorithm is that the system puts a token into the bucket at a constant speed, and if the request needs to be processed, it needs to get a token from the bucket and then deny the service when there is no token in the bucket. From the principle, the token bucket algorithm and leaky bucket algorithm is the opposite, a "water", one is "leaking".
Longtimestamp=getnowtime ();intcapacity;//capacity of barrelsintrate;//token into SpeedintTokens//Current Water VolumeBOOLGrant () {//perform an action to add a token first Longnow =Getnowtime (); Tokens= min (capacity, tokens+ (now-timestamp) *Rate ); TimeStamp=Now ; //The token is exhausted, Access denied if(tokens<1) { return false; } Else { //There are tokens, pick tokenstokens--; Retuntrue; }}
Token bucket-Flow control