LVS Scheduling algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor

The connection scheduling algorithm in the kernel

Ipvs load Balancing in the kernel is scheduled to be a connection-granularity. In the HTTP protocol (non-persistent), each object obtained from the Web server needs to establish a TCP connection, the same user's different requests will be dispatched to different servers, so this fine-grained scheduling to a certain extent, can avoid the burst of individual user access caused the load imbalance between servers.

On the connection scheduling algorithm in the kernel, Ipvs has implemented the following eight scheduling algorithms:

    • Round call scheduling (Round-robin scheduling)
    • Weighted round call scheduling (Weighted round-robin scheduling)
    • Minimum connection scheduling (Least-connection scheduling)
    • Weighted minimum connection scheduling (Weighted least-connection scheduling)
    • Minimal link based on locality (locality-based Least Connections scheduling)
    • Local least-link with replication (locality-based Least Connections with Replication scheduling)
    • Destination Address hash Schedule (Destination Hashing scheduling)
    • Source Address Hash Schedule (source Hashing scheduling)
1, round call scheduling

The algorithm is called in turn in order to dispatch the request to different servers, that is, each time the dispatch executes i = (i + 1) mod n, and select the I server. The advantage of the algorithm is that it is simple, and it does not need to record the status of all current connections. It is a stateless dispatch.

When the system is implemented, we introduce an additional condition that when the server's weight is zero, it means that the server is unavailable and not dispatched. This is done to cut the server out of tasks (such as masking server failures and maintenance) while maintaining consistency with other weighted algorithms. Therefore, the algorithm to make the corresponding changes, the algorithm flow is as follows:

Suppose there is a set of servers S = {S0, S1, ..., Sn-1}, an indicator variable i represents the last server selected, W (SI) represents the weight value of the server Si. The variable i is initialized to n-1, where n > 0. j = I;do {j = (j + 1) mod n;if (W (SJ) > 0) {i = J;return Si;}} while (J! = i); return NULL;

The round call scheduling algorithm assumes that all server performance is the same, regardless of the server current connection number and response speed, the algorithm is simple, does not apply to the server group processing performance is not the same, and when the request service time is relatively large, the round call scheduling algorithm easily lead to load imbalance between servers.

2. Weighted round call scheduling

The algorithm can solve the performance of the server between the situation, it is the corresponding weight value of the server processing performance, the default value of the server is 1. Assuming server A has a weight of 1,b of 2, it means that the server's processing performance is twice times the value of a. The weighted round call algorithm assigns requests to each server by the height of the weights and the calling method. The higher the weighted value of the first received connection, the higher the weight of the server to handle more connections, the same weights the server processes the same number of connections. The algorithm flow is as follows:

Suppose there is a set of servers S = {S0, S1, ..., Sn-1},w (SI) represents the weight of the server SI, an indicator variable i represents the last server selected, indicates that the variable CW represents the current dispatch weight, and Max (s) represents the maximum weight of all servers in the collection S, gcd (s) A greatest common divisor that represents all the server weights in the collection S. The variable i is initialized to -1,CW initialized to zero. while (true) {  i = (i + 1) mod n;if (i = = 0) {     CW = CW-GCD (S);      if (CW <= 0) {       CW = max (S);       if (CW = = 0)         return NULL;}  }   if (W (Si) >= CW)     return Si;}

For example, there are three servers A, B, and C that have the right value 4/3/2, and in a single dispatch cycle, the order is aababcabc. The secondary method is also relatively simple and efficient. When the requested service time varies greatly, a separate weighted round call scheduling algorithm still causes load imbalance between servers

3. Minimum connection scheduling

The algorithm is to assign the new connection request to the server with the minimum number of connections, and the minimum scheduling is a dynamic scheduling algorithm, which estimates the server load by the number of connections currently active on the server. The scheduler needs to record the number of connections each server has, and when a request is dispatched to a server, its number of connections is 1, and when the connection is aborted or timed out, its number of connections is reduced by one.

When the system is implemented, we also introduce when the server weight is zero, indicating that the server is unavailable or not scheduled, the algorithm flow is as follows:

Suppose there is a set of servers S = {S0, S1, ..., Sn-1},w (SI) represents the weight value of the server Si, and C (SI) represents the current number of connections for the server Si. for (m = 0, M < n; m++) {if (W (Sm) > 0) {for (i = m+1; i < n; i++) {if (W (SI) <= 0) continue;if (c (SI) < C (S m)) m = i;} return Sm;}} return NULL;
4. Weighted minimum connection scheduling

The algorithm is a superset of minimum connection scheduling, and each server uses corresponding weights to represent its processing power. The default value for the server is 1, and the system administrator can dynamically set the server's weights. Weighted minimum connection scheduling makes the server's established connections and their weights proportional as much as possible when scheduling a new connection.

Suppose there is a set of servers S = {S0, S1, ..., Sn-1},w (SI) represents the weight value of the server Si, and C (SI) represents the current number of connections for the server Si. The sum of the current number of connections for all servers is Csum =σc (Si)  (i=0, 1,., n-1). The current new connection request will be sent to the server SM when and only if the server SM meets the following conditions  (c (SM)/CSUM)/w (SM) = min {(c (SI)/CSUM)/W (SI)}  (I=0, 1,., n-1)  where W ( Si) not zero because csum is a constant in this round of lookups, the judging condition can be simplified to  C (SM)/w (SM) = min {c (SI)/w (SI)}  (I=0, 1,., n-1)  where W (SI) is not zero because the division requires CP U period is more than multiplication, and in the Linux kernel does not allow floating-point division, the server weights are greater than 0, so the criteria for the C (SM)/w (SM) > C (SI)/w (SI) can be further optimized for C (SM) *w (SI) > C (SI) * W (SM). At the same time, the server is not dispatched when the server's weight is zero. Therefore, the algorithm simply executes the following process. for (m = 0, M < n; m++) {if (W (SM) > 0) {for (i = m+1; i < n; i++) {if (c (Sm) *w (SI) > C (SI) *w (Sm)) m = i;} return Sm;}} return NULL;

Reference: http://www.linuxvirtualserver.org/zh/lvs4.html

LVS Scheduling algorithm

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.