A brief understanding of several scheduling algorithms for Nginx seven-layer load balancing

A brief understanding of several scheduling algorithms for Nginx seven-layer load balancing

This article mainly introduces several scheduling algorithms for Nginx seven-layer load balancing. The example code in this article is very detailed and has a certain reference value for everyone's study or work. Friends in need can refer to it.

Nginx is a lightweight, high-performance web server, as well as an excellent load balancer and reverse proxy server. It is often used as a seven-layer load balancing because of its support for powerful regular matching rules, dynamic and static separation, URL rewrite function, simple installation and configuration, and very little dependence on network stability. If the hardware is not bad, it can usually stably support tens of thousands of concurrent connections. If the hardware performance is good enough and the system kernel parameters and Nginx configuration are optimized, it can even reach more than 100,000 concurrent connections.

The following are several commonly used scheduling algorithms and applicable business scenarios for Nginx as a seven-layer load balancing

1. Polling (default scheduling algorithm)

Features: Each request is assigned to a different backend server for processing in chronological order.
Applicable business scenarios: Used when the hardware performance configuration of the backend servers is exactly the same and there are no special business requirements.

upstream backendserver { 
server 192.168.0.14:80 max_fails=2 fail_timeout=10s; 
server 192.168.0.15:80 max_fails=2 fail_timeout=10s; 
}

2. Weighted Round Robin

Features: Specify the polling probability, the weight value is proportional to the access ratio, and user requests are allocated according to the weight ratio.
Applicable business scenarios: Used in situations where the hardware processing capabilities of backend servers are uneven.

upstream backendserver { 
server 192.168.0.14:80 weight=5 max_fails=2 fail_timeout=10s; 
server 192.168.0.15:80 weight=10 max_fails=2 fail_timeout=10s;
}

3. ip_hash

Features: Each request is assigned according to the hash result of the access IP, so that each visitor accesses a fixed backend server, which can solve the problem of session retention.
Applicable business scenarios: Applicable to systems that require account login and services that maintain session connections.

upstream backendserver { 
ip_hash; 
server 192.168.0.14:80 max_fails=2 fail_timeout=10s; 
server 192.168.0.15:80 max_fails=2 fail_timeout=10s; 
}

4. Minimum number of connections least_conn

Features: According to the number of connections between the nginx reverse proxy and the backend server, the one with the least number of connections will be allocated first.

Applicable business scenarios: Applicable to businesses that need to maintain a long connection between the client and the backend server.

upstream backendserver { 
least_conn;
server 192.168.0.14:80 max_fails=2 fail_timeout=10s; 
server 192.168.0.15:80 max_fails=2 fail_timeout=10s; 
}

5. fair (need to compile and install the third-party module ngx_http_upstream_fair_module)

Features: Requests are distributed according to the response time of the backend server, with priority given to requests with shorter response time.
Applicable business scenarios: businesses that have certain requirements on access response speed.

upstream backendserver {
fair; 
server 192.168.0.14:80 max_fails=2 fail_timeout=10s; 
server 192.168.0.15:80 max_fails=2 fail_timeout=10s; 
}

6. url_hash (need to compile and install the third-party module ngx_http_upstream_hash_module)

Features: Allocate requests based on the hash result of the access URL, so that the same URL accesses the same backend server.

Applicable business scenarios: This method is more effective when the backend server is a cache server.

upstream backendserver { 
server 192.168.0.14:80 max_fails=2 fail_timeout=10s;
server 192.168.0.15:80 max_fails=2 fail_timeout=10s; 
hash $request_uri; 
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Use of Python machine learning library xgboost
  • Engineers must understand the LRU cache elimination algorithm and Python implementation process
  • Understand the principle of page replacement algorithm through code examples
  • A brief discussion of the top ten algorithms that you need to know about machine learning
  • Detailed explanation of the derivation of the merge sort time complexity process

<<:  Implementing a simple calculator based on JavaScript

>>:  How to optimize MySQL performance through MySQL slow query

Recommend

In-depth understanding of Vue's plug-in mechanism and installation details

Preface: When we use Vue, we often use and write ...

How to use glog log library in Linux environment

Generate Linux library The Linux version uses cen...

Usage of HTML H title tag

The usage of H tags, especially h1, has always bee...

Right align multiple elements in the same row under div in css

Method 1: float:right In addition, floating will ...

Detailed explanation of mysql permissions and indexes

mysql permissions and indexes The highest user of...

What you need to understand about MySQL locks

1. Introduction MySQL locks can be divided into g...

In-depth analysis of JDBC and MySQL temporary tablespace

background Temporary tablespaces are used to mana...

How to write a MySQL backup script

Preface: The importance of database backup is sel...

How to create an Nginx server with Docker

Operating environment: MAC Docker version: Docker...

How to use js to determine whether a file is utf-8 encoded

Conventional solution Use FileReader to read the ...

JavaScript Dom implements the principle and example of carousel

If we want to make a carousel, we must first unde...

Execution context and execution stack example explanation in JavaScript

JavaScript - Principles Series In daily developme...

Self-study of MySql built-in functions knowledge points summary

String functions Check the ascii code value of th...

Vue keeps the user logged in (various token storage methods)

Table of contents How to set cookies Disadvantage...

How to use css overflow: hidden (overflow hiding and clearing floats)

Overflow Hide It means hiding text or image infor...