Today, we're going to talk about the exponential backoff algorithm (exponential backoff), before the topic of index avoidance algorithm starts to throw a few questions to everyone: what is the exponential backoff algorithm? Why use an exponential backoff algorithm? What are the application scenarios for the exponential backoff algorithm? How is the code implemented? Take these questions gentlemen and look down.
What exactly is the
exponential backoff algorithm? There's an explanation on the wiki: "Exponential backoff is a algorithm that uses feedback to multiplicatively decrease the rate of some process , in order to gradually find a acceptable rate ". Popular point, the Backoff algorithm is the network node after the transmission of data conflicts, wait for a certain time after the hair, wait time is exponential growth, so as to avoid frequent triggering conflicts. In the computer network, the binary exponential backoff algorithm or truncated exponential backoff algorithm is often used as a strategy to avoid the network congestion of the same data block. After n collisions occur, the wait time is selected randomly between 0~2^n-1 interval time (slot times). For example, after the first conflict, each sender will wait for 0 or 1 gap time (slot times), and after the second conflict, the wait time for each sender will be selected between 0 and 3 gap times (slot time), and after the third conflict, The wait time for each sender will be arbitrarily selected between 0 and 7 Gap times (slot time), and so on, and as the number of collisions increases, the waiting time for the sender will increase exponentially. And from the literal meaning of "truncation (truncated)" We may as well guess that, to a certain number of times, the exponential operation will stop, that is, the waiting time will not increase indefinitely. For example, if you set an upper limit of n=10, the maximum wait time is 1023 clearance time. Because some scenarios also have the possibility of a conflict occurring during the wait time, the general process terminates after 16 retries. The specific Backoff algorithm is as follows:
Determines the basic Backoff time, which is the contention period (one-way end-to-end propagation Shiyank on the bus is X, end-to-end round-trip time of Ethernet 2x). Ethernet will set the contention period to 51.2us. For 10mb/s Ethernet, 512bit, or 64 bytes, can be sent during the contention period. It can also be said that the contention period is 512-bit time. 1-bit time is the time required to send 1 bits. So this time unit is closely related to data rate.
Randomly extracts a number from a discrete set of integers [0,1,...,], recorded as R. The time for retransmission to be postponed is the contention period for R times. The above parameter k is calculated according to the following formula: K=min[retransmission times, 10] visible when the number of retransmissions is not more than 10 o'clock, the parameter K equals the number of retransmissions, but when the retransmission is more than 10 o'clock, K does not increase and always equals 10.
When the 16 re-transmission is still unsuccessful (which indicates that there are too many data stations to be sent at the same time, so there is a continuous conflict), discard that and report to the top. For example, on the 1th retransmission, k=1, random number R selects a number from the integer {0,1}. So the retransmission delay is 0 or the contention period, at which time one of the two randomly selected. If a collision occurs again, the k=2, random number R will select a number from the integer {0,1,2,3} when retransmission. So the retransmission delay time is randomly extracted in 0,2x, 4x and 6x 4 time. Similarly, if a collision occurs, the retransmission is k=3, and the random number R selects a number from the integer {0,1,2,3,4,5,6,7}. And so on If there are multiple consecutive collisions, it indicates that there may be more stations participating in the contention channel. However, the use of Backoff algorithm can make retransmission need to delay the average time with the number of retransmissions increased (this is also known as dynamic Backoff), thus reducing the probability of collisions, the stability of the whole system.
I believe the answer to the second question is read here, and most of the exponential backoff algorithms use jitter (random delay) to prevent continuous collisions. However, if you use concurrent clients, jitter can help you execute requests more quickly and successfully.
What are the scenarios for the exponential backoff algorithm? Exponential backoff algorithm is widely used in the computer network, here simply say two scenes, the first scenario, access to three-party payment services, in the three-party payment provided by the Access interface specification, the end of service transaction results notification and the merchant actively query the transaction results are used to the re-issued mechanism, this is called the Backoff algorithm, This place also leads to another point of knowledge--the power of the interface (the result of repeating an interface with the same parameter to the same resource is the same as the result of a call), which is not too much to repeat. In the second scenario, in an app, many scenarios encounter polling-class problems, and general polling is a huge disaster for app performance and power consumption. So how do we solve this problem? The app uses an exponential backoff algorithm to reduce the frequency of updates, saving resources and reducing power consumption in the event that it has not been used since the last update operation.
Finally, here's a simple way to show you how to implement incremental delay polling in pseudo-code and Java code.
Pseudo code
Do some asynchronous Operation. Retries= 0Do wait for(2^retries * 100) milliseconds status=Get The result of the asynchronous operation. IF Status=SUCCESS Retry=falseELSE IF Status=Not_ready Retry=trueELSE IF Status=throttled retry=trueELSE Some Other error occurred, so stop calling the API. Retry=falseEND IF Retries= retries + 1While (Retry and (retries< max_retries))
Java code
Public enumResults {SUCCESS, Not_ready, throttled, server_error}/** Performs an asynchronous operation, then polls for the result of the * operation using an incremental delay.*/ Public Static voidDooperationandwaitforresult () {Try { //Do some asynchronous operation. Longtoken =asyncoperation (); intRetries = 0; BooleanRetry =false; Do { LongWaitTime =math.min (Getwaittimeexp (retries), max_wait_interval); System.out.print (WaitTime+ "\ n"); //Wait for the result.Thread.Sleep (waitTime); //Get The result of the asynchronous operation.Results result =Getasyncoperationresult (token); if(Results.success = =result) {Retry=false; } Else if(Results.not_ready = =result) {Retry=true; } Else if(results.throttled = =result) {Retry=true; } Else if(Results.server_error = =result) {Retry=true; } Else { //Some Other error occurred, so stop calling the API.Retry =false; } } while(Retry && (retries++ <max_retries)); } Catch(Exception ex) {}}/** Returns The next wait interval, in milliseconds, using a exponential * backoff algorithm.*/ Public Static LongGetwaittimeexp (intRetryCount) { LongWaitTime = ((Long) Math.pow (2, retrycount) * 100L); returnWaitTime;}
OK, just talk about this, from the limitations of their own cognition, perhaps some of the places will have the wrong place, welcome to correct.
"Algorithm" 6, exponential backoff algorithm