Nginx simple load balancing construction

Load balancing is built on the existing network structure, which provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers , increase throughput , strengthen network data processing capabilities, and improve network flexibility and availability.

Load balancing, the English name is Load Balance, means that it is allocated to multiple operating units for execution, such as Web servers , FTP servers , enterprise key application servers and other mission-critical servers, so as to complete work tasks together.

Load balancing algorithm?

1. Random algorithm

  • Random, set random probability by weight. The probability of collision on a section is high, but the larger the number of calls, the more uniform the distribution, and the more uniform the weights are used according to the probability, which is conducive to dynamically adjusting the provider weights.

2. Polling and Weighted Polling

  • Round Robbin When the processing capacity of each server in the server group is the same, and the difference in the processing volume of each service is not large, this algorithm is most suitable for use. Round robin, set the round robin ratio according to the weight after the convention. There is a problem of slow providers accumulating requests. For example, the second machine is very slow, but it does not hang. When the request is transferred to the second machine, it is stuck there. Over time, all requests are stuck on the second machine.
  • Weighted Round Robbin is an algorithm that attaches a certain weight to each server in the round. For example, the weight of server 1 is 1, the weight of server 2 is 2, and the weight of server 3 is 3, then the order is 1-2-2-3-3-3-1-2-2-3-3-3-......

3. Least Connection and Weighted Least Connection

  • Least Connections is an algorithm that communicates with the server that handles the least number of connections (sessions) among multiple servers. Even if the processing capacity of each server is different and the processing volume of each business is different, the load of the server can be reduced to a certain extent.
  • Weighted Least Connection (Weighted Least Connection) is an algorithm that attaches weight to each server in the least connection algorithm. The algorithm allocates the number of processing connections to each server in advance, and transfers client requests to the server with the least number of connections.

4. Hash algorithm

  • plain hash
  • Consistent Hash Consistent Hash, requests with the same parameters are always sent to the same provider. When a certain provider hangs, the request originally sent to the provider will be spread to other providers based on virtual nodes, and will not cause drastic changes.

5. IP Address Hashing

  • An algorithm that uniformly forwards packets from the same sender (or packets to the same destination) to the same server by managing the hash of the sender IP and destination IP addresses. When the client has a series of business needs to be processed and must communicate with a server repeatedly, the algorithm can take the flow (session) as the unit to ensure that the communication from the same client can always be processed in the same server.

6. URL Hashing

  • An algorithm that forwards requests sent to the same URL to the same server by managing the hash of the client's request URL information.

1. The realization idea of ​​dynamic and static separation

Dynamic and static separation is to deploy website static resources (HTML, JavaScript, CSS, img and other files) separately from background applications to improve the speed of users accessing static code and reduce access to background applications.

 

Generally speaking:

The static page access path is short, the access speed is fast, a few milliseconds

The dynamic page access path is long, and the access speed is relatively slow (database access, network transmission, business logic calculation), tens of milliseconds or even hundreds of milliseconds, which requires higher architectural scalability.

 

 

Use nginx configuration to demonstrate a simple example

Using three different sites means the same service is deployed on three servers

Configured in nginx, when accessing a specific website, the request will be distributed to the three configured sites, and the proportion of distribution is different according to the performance of the server.

The config configuration of nginx, where weight represents the proportion of distribution

 upstream ahaoboy{ 
        server   111.230.241.93:8081 weight=1; 
        server   111.230.241.93:8082 weight=2; 
        server   111.230.241.93:8083 weight=3; 
    } 
    server {
        listen       80;
        server_name  127.0.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            # index  index.html index.htm;
            proxy_pass http://ahaoboy;
            proxy_redirect default;
        }

 

Using Python verification, the servers distributed each time are random, but the total distribution ratio is in line with the configuration of 1:2:3

import requests
from collections import defaultdict

url = 'http://127.0.0.1'

state = defaultdict(int)

cnt = 36
for i in range(cnt):
    text = requests.get(url).text
    print(text)
    state[text] += 1

print(state)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325372877&siteId=291194637
Recommended