How Nginx implements polling algorithm
May 21, 2023 pm 09:43 PMSimple polling algorithm
This algorithm is relatively simple. For example, you have three servers
First Server | 192.168.1.1 |
Second server | 192.168.1.2 |
Third server Server | 192.168.1.3 |
After the first request comes, it will access the first server by default, the second request will access the second server, and the third server The first request comes to access the third station, the fourth request comes to access the first station, and so on. The following is a simple algorithm implemented by my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
The result of simulated execution 4 times is
If I have a server performance comparison at this time OK (such as 192.168.1.1), I want this server to handle more requests. At this time, the weight probability is involved. This algorithm cannot be implemented. Please see the polling upgrade algorithm I describe later.
Weighted polling algorithm
At this time, I need to set the weights of the three servers in front of me. For example, the first one is set to 5, the second one is set to 1, and the first one is set to 1. Three settings 1
##Third server192.168.1.31First server | 192.168.1.1 | 5 |
Second server | 192.168.1.2 | 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
Smooth Weighted Polling Algorithm
This algorithm may be more complicated, and it was a bit confusing when I first looked at it. I don’t quite understand. I’ve read relevant information later and combined it with my own understanding to explain it with pictures and text. The server configuration and weights I gave as an example here are still the same as aboveRequest Current weight = own weight current weight after selection Total weightCurrent maximum weightReturned ipCurrent after selection Weight = current maximum weight - total weight ##1##2{3,2 ,2}73192.168.1.1{-4,2,2}3{1,3,3}73192.168.1.2{1,-4 ,3}4{6,-3,4}76 192.168.1.1{-1,-3,4}5{4,-2,5}75192.168.1.3{4,-2,-2}6{9,-1,-1}79192.168.1.1{2,-1,-1}7{7,0,0}77192.168.1.1{0,0,0}As can be seen from the above figure, although the weight of the first server is set to 5, it is not the fifth request. In the past, it was always executed on the first server, but it was executed in a distributed manner. The scheduling sequence was very even, and after the seventh scheduling was selected, the current weight returned to {0, 0, 0}, and the state of the instance was consistent with the initial state. Therefore, the scheduling operation can be repeated in the future.{5,1,1} | 7 | 5 | 192.168.1.1 | {-2,1,1} | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
You can see The execution results here are consistent with those described in the table.
The above is the detailed content of How Nginx implements polling algorithm. For more information, please follow other related articles on the PHP Chinese website!

Hot tools Tags

Hot Article

Hot tools Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to allow external network access to tomcat server

What are the nginx start and stop commands?

How to solve the problem of nginx when accessing the website

What are the differences between tomcat and nginx

How to deploy nodejs project to server
