Nginx load balancing in 5 Minutes Nginx load balancing in 5 Minutes Nginx load balancing in 5 minutes Nginx load balancing in 5 minutes

preface

For e-commerce platforms, with the continuous development and expansion of business, website visits and data volume also increase sharply, which brings a certain burden to the server. From the perspective of user experience, due to the delay caused by server-side data processing, the response speed of the page is too slow, and the operation fluency is blocked. This could even potentially affect the volume of transactions on the platform to some extent. Providing high efficiency and high quality service has become an urgent problem to be solved. The emergence and development of load balancing strategy has become an effective way to alleviate the above problems. This article will take you through load balancing based on Nginx.

What is load balancing

Load Balance, which can provide a cheap, effective and transparent method to expand the bandwidth of network devices and servers on the existing network structure, and can increase the throughput to a certain extent, strengthen the network data processing capacity, improve the flexibility and availability of the network, etc. According to its website, it acts as a “traffic commander” in network traffic, “standing” in front of the server and processing all requests between the server and client to maximize response rates and capacity utilization while ensuring that no server is overworked. If a single server fails, load balancing redirects traffic to the remaining cluster servers to ensure service stability. After a new server is added to a server group, it can automatically process requests from clients through load balancing. (For details, see: What Is Load Balancing?)

In short, load balancing is actually a strategy of distributing a large number of requests.

What is Nginx load balancing

From the simple concepts introduced above, you probably have a rudimentary idea of load balancing as a scheduling strategy. So what is Nginx? How does Nginx implement load balancing? This starts with forward and reverse proxies.

  • Forward agent

    The most important feature of a Forward Proxy is that the client is very clear about the server address to be accessed. It acts as a Proxy for the client and sends requests for the client. For example: tree learning online, commonly known as FQ (warning ⚠️ : FQ operation in violation of relevant laws and regulations, this article is just to explain the forward agent to the reader for example, only for learning reference, do not blindly FQ).

    Suppose the client wants to access Google. It knows that the address of the server to be accessed is www.google.com/, but due to constraints, it looks for… Google’s “friends” : proxy servers. The client sends the request to the proxy server, which requests Google on its behalf and returns the response to the client. This is a forward proxy process in which the server does not know who is actually making the request.

  • The reverse proxy

    So, with the explosive growth of requests, the server decided that it could not handle it by itself and needed the help of its brother servers, so it called on its brother and proxy server friends.

    At this point, all requests from different clients are actually sent to the proxy server, which then distributes the requests to each server according to certain rules.

    This is called a Reverse Proxy, which hides the server’s information and acts on behalf of the server to receive requests. In other words, in a reverse proxy, the client does not know which server is processing its request. In this way, the access speed is improved and the security is guaranteed.

    In this process, the reverse proxy needs to consider how to balance the division of labor, control traffic, and avoid the problem of excessive load on local nodes. Generally speaking, it is how to allocate requests reasonably for each server, so that it has a higher overall work efficiency and resource utilization.

  • What is Nginx?

    As a high-performance Web server based on C, Nginx can solve the above load balancing problem through a series of algorithms. Because of its high concurrency, high reliability, high scalability, open source and other characteristics, it has become a commonly used reverse proxy tool for developers.

Common load balancing algorithms

1. Round-robin

Polling is a basic and simple algorithm in load balancing. It does not need to configure additional parameters. Assuming that there are M servers in the configuration file, the algorithm traverses the list of server nodes and selects one server in each round to process requests according to the node order. When all nodes have been called once, the algorithm traverses again from the first node.

Features: Because each request is distributed to different servers one by one in chronological order, this algorithm is suitable for clusters with similar server performance, where each server carries the same load. However, for clusters with different server performance, the algorithm is easy to cause problems such as unreasonable resource allocation.

2. Weighted polling

In order to avoid the disadvantages of ordinary polling, weighted polling came into being. In weighted polling, each server will have its own weight. In general, a higher value of weight means that the server is performing better and can handle more requests. In this algorithm, requests from clients are allocated proportionally by weight, and when a request arrives, the server with the largest weight is allocated first.

Features: Weighted polling can be applied to clusters with different server performance to rationalize resource allocation.

Nginx weighted polling source visible: ngx_http_upstream_round_robin. C, source analysis can refer to: about the polling strategy principle of self-understanding. The core idea is to traverse each server node and calculate the node weight. The calculation rule is the sum of current_weight and its corresponding effective_weight. In each round of traverse, the node with the maximum weight is selected as the optimal server node. Effective_weight varies with resource and response status during algorithm execution. The core parts are as follows:

for (peer = rrp->peers->peer, i = 0;
	peer; 	/* Peer is the current traversal server node */
  peer = peer->next, i++)
{
  ...
    
	/* Each iteration updates the current weight of the peer */peer->current_weight += peer->effective_weight; ./* Best is the best node in the current server, that is, the selected server node in this round */
	if (best == NULL|| peer->current_weight > best->current_weight) { best = peer; p = i; }... }Copy the code

3. IP Hash

Ip_hash Assigns servers based on the hash value of the client IP address. This algorithm can ensure that requests from the same IP address are mapped to the same server or different IP addresses with the same hash value are mapped to the same server.

Features: This algorithm solves the problem that sessions are not shared in a cluster deployment environment to some extent.

The Session non-sharing problem is that if the user has logged in, the requests sent by the user are allocated to server A. However, server A breaks down suddenly and the requests are forwarded to server B. However, because the Session is not shared, USER B cannot read user login information to perform other operations.

In practical applications, we can use IP_hash to forward part of the requests under IP to the server running the new version of the service, and the other part to the server running the old version, so as to achieve grayscale publishing. Moreover, if the request times out due to large files, ip_hash can also be used to upload file slices. It can ensure that the file slices sent by the client are forwarded to the same server, which is conducive to the receiving of slices and subsequent file merge operations.

4. Other algorithms

  • URL hash

    Url_hash assigns servers based on the hash value of the requested URL. The feature of this algorithm is that the requests of the same URL will be allocated to a fixed server, and the efficiency is generally high when there is a cache. However, Nginx does not support this load balancing algorithm by default and relies on third-party libraries.

  • Minimum number of Connections

    Assume that there areWhen a new request appears, the server node list is traversed and the server with the smallest number of connections is selected to respond to the current request. The number of connections can be understood as the number of requests currently being processed.

Application scenarios

So, how do you use nginx-based load balancing? Next, we will take the weighted polling algorithm as an example, take you to try to test the load balancing through their own laptop + Nginx + Node. Since there are no more than one server, so through the many different ports of his notebook to simulate different servers.

Ensure that Nginx is installed and can be successfully started on your computer (take Mac as an example).

If you have a problem like I did with Nginx startup due to port usage, try the following steps to change the port number in the configuration file

  • Related file paths

    • / usr/local/etc/nginx/nginx. Conf (configuration file path)
    • /usr/local/var/www (server default path)
    • / usr/local/Cellar/nginx / 1.8.0 comes with (installation path)
  • Modify ports in the nginx.conf file

    server {
      # listen       8080;
      listen       8086;
      server_name  localhost;
    }
    Copy the code

    The Nginx configuration file nginx.conf contains the following parts:

    • Server: specifies the domain name, IP address, and port number of a virtual host
    • Location: reverse proxy Settings after a URL matches a specific location
    • Upstream: configure load balancing
  • Pause Nginx and restart it

    // Suspend the Nginx service sudo Nginx-sStop // Start Nginx service NginxCopy the code
  • Check whether http://localhost:8086/ is successfully opened. If the following figure is displayed, the startup is successful

Step 2: Build a simple server based on the Node + Express framework

Express is a simple and flexible lightweight Node.js Web application framework. If you are using Express for the first time, install it first.

  • Install Express

    npm i express
    Copy the code
  • Create a new index.js file and write the code

    const express = require('express');
    const app = express();
    
    // Define the port number to listen on
    const listenedPort = '8087';
    
    app.get('/', (req, res) => res.send(`Hello World! I am port ${listenedPort}~ `));
    
    // Listen on the port
    app.listen(listenedPort, () => console.log(`success: ${listenedPort}`));
    Copy the code
  • Start the server

    node index.js
    Copy the code

Node can listen to port 8087,8088,8089 for several services, and send different copytexts for each service to distinguish different servers.

Step 3: Configure the polling server and proxy in the nginx.conf file

  • The polling server, written in the UPSTREAM object in HTTP:
upstream testServer {
  server localhost:8087 weight=10;
  server localhost:8088 weight=2;
  server localhost:8089;
}
Copy the code
  • The proxy address, written in the HTTP server object:
location / {
  root   html;
  index  index.html index.htm;
  proxy_pass http://testServer; // testServer defines its own Server cluster}Copy the code

Step 4: Check the results

  • Restart the Nginx service

  • Open again http://localhost:8086/

Through multiple flushes, it can be found that the server with port number 8087 appears the most times due to different weights, confirming the rule that the higher the weight, the more likely the server is to process requests.

conclusion

As an excellent reverse proxy server, Nginx can use different load balancing algorithms to solve the problem of server resource allocation when the request volume is too large. The more common load balancing algorithms are polling, weighted polling, IP hash and so on, which can deal with different request scenarios respectively. If you are interested, you can go to Github to understand the source code of Daishen. If you have any questions, please join us to discuss ~

reference

  • Self-understanding of polling strategy principles

  • Nginx: What is Nginx? Can you do?

Recommended reading

Visual Construction System for Front-end Engineering Practice (PART 1)

Probably the most complete collection of text overflow truncation ellipsis schemes

An illustrated guide to unlock the mysteries of single sign-on

, recruiting

ZooTeam, a young passionate and creative front-end team, belongs to the PRODUCT R&D department of ZooTeam, based in picturesque Hangzhou. The team now has more than 50 front-end partners, with an average age of 27, and nearly 30% of them are full-stack engineers, no problem in the youth storm group. The members consist of “old” soldiers from Alibaba and netease, as well as fresh graduates from Zhejiang University, University of Science and Technology of China, Hangzhou Electric And other universities. In addition to daily business docking, the team also carried out technical exploration and practice in material system, engineering platform, building platform, performance experience, cloud application, data analysis and visualization, promoted and implemented a series of internal technical products, and continued to explore the new boundary of front-end technology system.

If you want to change what’s been bothering you, you want to start bothering you. If you want to change, you’ve been told you need more ideas, but you don’t have a solution. If you want change, you have the power to make it happen, but you don’t need it. If you want to change what you want to accomplish, you need a team to support you, but you don’t have the position to lead people. If you want to change the pace, it will be “5 years and 3 years of experience”; If you want to change the original savvy is good, but there is always a layer of fuzzy window… If you believe in the power of believing, believing that ordinary people can achieve extraordinary things, believing that you can meet a better version of yourself. If you want to be a part of the process of growing a front end team with deep business understanding, sound technology systems, technology value creation, and impact spillover as your business takes off, I think we should talk. Any time, waiting for you to write something and send it to [email protected]