Nginx load balancing strategy is enough!!!

Posted by kiwibrit on Sun, 23 Jan 2022 22:09:54 +0100

It's enough to read this article on Nginx load balancing strategy. Sincerely, I didn't lie and didn't say much. Let's start~

1, Nginx load balancing policy

Load balancing is used to select a server from the list of back-end servers defined in the "upstream" module to accept user requests. The most basic upstream module is as follows. The server in the module is the server list:

#Dynamic server group
    upstream dynamic_zuoyu {
        server localhost:8080;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083;  #tomcat 9.0
    }

After the upstream module configuration is completed, the specified access reverse proxy to the server list:

#Reverse proxy other pages to tomcat container
        location ~ .*$ {
            index index.jsp index.html;
            proxy_pass http://dynamic_zuoyu;
        }

This is the most basic example of load balancing, but it is not enough to meet the actual needs; At present, the upstream module of the Nginx server supports six modes of allocation:

Load balancing strategy
polling Default mode
weightWeight method
ip_hashAccording to ip distribution mode
least_connMinimum connection mode
fair (third party)Response time mode
url_hash (third party)Assign by URL

Here, only the load balancing strategy provided by Nginx is described in detail, and the third party will not describe it.

1. Polling

The most basic configuration method is polling, which is the default load balancing policy of the upstream module. Each request is assigned to a different back-end server in chronological order.

There are the following parameters:

fail_timeoutAnd Max_ Use with failures.
max_fails

Set in fail_ The maximum number of failures within the time set by the timeout parameter. If all requests to the server fail within this time, the server is considered to be down,

fail_timeThe length of time that the server will be considered down. The default is 10s.
backupMark the server as an alternate server. When the primary server stops, requests are sent to it.
downMark that the server is permanently down.

be careful:

  • During polling, if the server goes down, the server will be automatically eliminated.
  • The default configuration is the polling policy.
  • This strategy is suitable for stateless and fast services with equivalent server configuration.

2,weight

The weight method specifies the probability of polling based on the polling policy. Examples are as follows:

#Dynamic server group
    upstream dynamic_zuoyu {
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082   backup;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }

In this example, the weight parameter is used to specify the polling probability. The default value of weight is 1,; The value of weight is directly proportional to the access ratio. For example, Tomcat 7.0 is twice as likely to be accessed as other servers.

be careful:

  • The higher the weight, the more requests are allocated to be processed.
  • This policy can be used with least_conn and IP_ Use with hash.
  • This strategy is more suitable for the situation where the hardware configuration of the server is quite different.

3,ip_hash

Specifies that the load balancer is distributed based on the client IP. This method ensures that the requests of the same client are always sent to the same server to ensure the session. In this way, each visitor has a fixed access to a back-end server, which can solve the problem that sessions cannot cross servers.

#Dynamic server group
    upstream dynamic_zuoyu {
        ip_hash;    #Ensure that each visitor has fixed access to a back-end server
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }

be careful:

  • Before nginx version 1.3.1, you cannot use IP_ weight is used in hash.
  • ip_hash cannot be used with backup.
  • This strategy is suitable for stateful services, such as session.
  • When a server needs to be removed, it must be manually down.

4,least_conn

Forward the request to the back-end server with fewer connections. Polling algorithm is to forward the request to each back-end evenly, so that their load is roughly the same; However, some requests take a long time, resulting in a high load on the back end. In this case, least_conn this way can achieve better load balancing effect.

#Dynamic server group
    upstream dynamic_zuoyu {
        least_conn;    #Forward the request to the back-end server with fewer connections
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082 backup;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }

be careful:

  • This load balancing strategy is suitable for server overload caused by different request processing times.

5. Third party strategy

The implementation of the third-party load balancing strategy requires the installation of third-party plug-ins.

①fair

Requests are allocated according to the response time of the server, and those with short response time are allocated first.

#Dynamic server group
    upstream dynamic_zuoyu {
        server localhost:8080;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083;  #tomcat 9.0
        fair;    #Achieve priority allocation with short response time
    }

②url_hash

Allocate the request according to the hash result of the access url, so that each url is directed to the same back-end server, which should be used in conjunction with the cache hit. Multiple requests for the same resource may reach different servers, resulting in unnecessary multiple downloads, low cache hit rate and waste of some resources and time. Instead, use the url_hash enables the same url (i.e. the same resource request) to reach the same server. Once the resource is cached and the request is received, it can be read from the cache.  

#Dynamic server group
    upstream dynamic_zuoyu {
        hash $request_uri;    #Each url is directed to the same back-end server
        server localhost:8080;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083;  #tomcat 9.0
    }

2, Summary

The above are the implementation methods of six Nginx load balancing strategies. In addition to polling and polling weight, they are implemented by Nginx according to different algorithms. In practical application, it needs to be selectively used according to different scenarios, mostly a combination of multiple strategies to meet the actual needs.

I hope the Nginx load balancing strategy in this paper will be helpful to you~~

Welcome to my WeChat official account, Java head, too.

Topics: Java Nginx