Advantages and disadvantages of nginx/LVS/haproxy Server Load balancer Software

Source: Internet
Author: User
Tags email protocols node server haproxy

PS: nginx/LVS/haproxy is currently the most widely used three load balancing software. I have implemented these software in multiple projects. I have referenced some materials and combined some of my experience in using it, to sum up.

Generally, Server Load balancer uses different technologies at different stages as the website scale increases. Specific Application requirements have to be analyzed. For small and medium-sized web applications, such as daily PV smaller than 10 million, nginx is enough. If there are many machines, DNS round-robin can be used, LVS consumes a lot of machines. LVS can be used for large websites or important services with a large number of servers.

One is through hardware. Common Hardware includes expensive F5, array, and other commercial load balancers, it has the advantage of a professional maintenance team to maintain these services. The disadvantage is that the cost is too large. Therefore, it is not required for small network services for the time being; in addition, it is similar to nginx/LVS/haproxy's Linux-based open-source free Server Load balancer software, which is implemented at the software level, so the cost is very low.

Currently, the website architecture is generally reasonable and popular: The Web Front-end uses nginx/haproxy + keepalived as the Load balancer, And the backend uses the MySQL database for master-slave and read-write separation, use the LVS + keepalived architecture. Of course, you must develop a scheme based on the specific needs of the project.
Next we will talk about their respective characteristics and applicable occasions.

Nginx has the following advantages:

1. Working on Layer 7 of the network, you can implement some shunting policies for HTTP applications, such as for domain names and directory structures.Regular rulesIt is more powerful and flexible than haproxy, which is also one of the main reasons for its widespread popularity. nginx alone is far more usable than LVS.
2,Nginx has little dependence on Network stability.Theoretically, the server load can be pinged. This is also one of its advantages. On the contrary, LVS relies heavily on Network stability;
3. nginx is easy to install and configure, and is easy to test. It can print errors in logs. It takes a long time to configure and test LVS. LVS is highly dependent on the network.
3. It can handle high load pressure and stability. In the case of poor hardware, it can support tens of thousands of concurrent requests, and the load is relatively smaller than LVS.
4. nginx can detect internal server faults through ports, such as processing status codes returned by webpages Based on the server, timeout, and so on, and resubmit the returned error requests to another node, however, the disadvantage is that URL detection is not supported. For example, if the user is uploading a file and the node that handles the upload fails during the upload process, nginx will cut the upload to another server and re-handle it, and LVS will be disconnected directly, if you upload a large file or a very important file, the user may be dissatisfied.
5. nginx is not only an excellent Load balancer/reverse proxy software, but also a powerful Web application server. Lnmp is also a very popular Web architecture in recent years. It is also stable in high-traffic environments.
6. nginx is now becoming more and more mature as the Web reverse acceleration cache, which is faster than the traditional squid server. You can consider using nginx as the reverse proxy accelerator.
7. nginx can be used as a middle-layer reverse proxy. nginx basically has no rivals on this layer. The only thing that can be compared with nginx is Lighttpd. However, Lighttpd does not yet provide complete nginx functions, the configuration is not so clear and easy to read, and community information is far from nginx active.
8. nginx can also be used as a static web page and image server, which has no performance rival. The nginx community is also very active, and there are many third-party modules.

Nginx has the following Disadvantages:

1. nginx only supports HTTP, https, and email protocols, so it is smaller in scope. This is a disadvantage of nginx.
2,Backend server health check only supports port detection, but does not support URL detection.Direct session persistence is not supported, but can be solved through ip_hash.

LVS: implements a high-performance and high-availability server Load balancer using Linux kernel clusters. It has good scalability, reliability, and manageability ).

LVS has the following advantages:

1. Strong load resistance, working on Layer 4 of the network for distribution only,No traffic generatedThis feature also determines its strongest performance in the Server Load balancer software, and its consumption of memory and CPU resources is relatively low.
2. Low configuration, which is a disadvantage and an advantage. Because there is no configuration item, there is no need for too many contacts, which greatly reduces the chance of human error.
3. stable operation, because of its strong load resistance capability, it has a complete dual-machine Hot Standby solution, such as LVS + keepalived, however, LVS/DR + keepalived is the most widely used in project implementation.
4. No traffic. LVS only distributes requests, but the traffic does not go out of it. This ensures that the I/O performance of the balancer will not be affected by the large traffic.
5. A wide range of applications. Because LVS works on Layer 4, it can load balance almost all applications, including HTTP, databases, and online chat rooms.

LVS has the following Disadvantages:

1. software itselfRegular Expression Processing is not supported, and dynamic/static separation cannot be performed.But now many websites have strong requirements in this regard. This is the advantage of nginx/haproxy + keepalived.
2. If there are a large number of website applications, the implementation of LVS/DR + keepalived is complicated. In particular, if there is a Windows server machine later, if the implementation, configuration, and maintenance process are complicated, nginx/haproxy + keepalived is much simpler.

Haproxy features:

1. haproxy also supports virtual hosts.
2. Advantages of haproxy can supplement some shortcomings of nginx, suchSupports session persistence and cookie guidance. You can also obtain a specified URL to check the status of the backend server.
3. Similar to LVS, haproxy itself is only a Server Load balancer software. In terms of efficiency alone, haproxy has a better load balancing speed than nginx, it is superior to nginx in concurrent processing.
4. haproxySupport for TCP Server Load balancer forwardingYou can perform load balancing on MySQL reads, check the backend MySQL nodes, and perform load balancing. You can use LVS + keepalived to perform load balancing on the MySQL master and slave nodes.
5. There are many Load Balancing policies for haproxy. The following eight Load Balancing algorithms are available for haproxy:
① Roundrobin indicates simple round robin. This is basically what Server Load balancer has;
② Static-RR, indicating that according to the weight, we recommend that you pay attention to it;
③ Leastconn, which indicates that the minimum number of connectors is handled first. We recommend that you pay attention to it;
④ Source indicates the request source IP address. This is similar to the ip_hash mechanism of nginx. We use it as a solution to the session problem. We recommend that you pay attention to it;
⑤ Ri, indicating the URI based on the request;
⑥ Rl_param indicates the request URL parameter 'balance url_param' requires an URL parameter name;
7. HDR (name) indicates locking each HTTP request based on the HTTP request header;
Extends RDP-cookie (name) indicates that each TCP request is locked and hashed Based on the cookie (name.

Summary of comparison between nginx and LVS:

1. nginx works on Layer 7 of the network. Therefore, it can be used to distribute traffic to HTTP applications, such as domain names and directory structures. In contrast, LVS does not have such a function, therefore, nginx is much more usable than LVS alone. However, nginx uses these functions to make them more adjustable than LVS, so it often needs to touch and touch more, there is a high probability of human problems.
2. nginx has little dependence on Network stability. Theoretically, nginx can be connected if the ping succeeds and the webpage access is normal. This is a major advantage of nginx! Nginx can also be divided into Intranet and Internet. If it is a node with both Intranet and Internet, it is equivalent to a single machine with a backup line. LVS is dependent on the network environment, currently, the server is in the same CIDR Block and LVS uses direct to distribute traffic. The effect is guaranteed. In addition, note that LVS needs to apply for at least one more IP address as the virtual IP address from the publisher, and it seems that it cannot use its own IP address as the VIP address. To be a good LVS administrator, you must follow up and learn a lot about network communication, so it is no longer as simple as an HTTP.
3. nginx installation and configuration are relatively simple, and testing is also very convenient, because it can basically print out errors using logs. It takes a long time to install, configure, and test LVS. LVS relies heavily on the network. In many cases, failure to complete the configuration is due to network problems rather than configuration problems, it will be much more troublesome to solve the problem.
4. nginx can also withstand high loads and stability, but its load and stability are poor. LVS has several levels: nginx handles all traffic, so it is limited by machine I/O and configuration; it is still difficult to avoid bugs.
5. nginx can detect internal faults on the server, such as status code returned by the server to process webpages, timeout, and so on. In addition, it will resubmit the request with an error returned to another node. Currently, ldirectd in LVS can also be monitored based on internal server conditions, but the principle of LVS makes it unable to resend requests. For example, if a user is uploading a file and the node that handles the upload fails during the upload process, nginx will cut the upload to another server and re-process it, and LVS will be disconnected directly, if you upload a large file or a very important file, the user may be annoyed.
6. asynchronous processing of requests by nginx can help the node server reduce the load. If Apache is used to directly connect to external services, when many narrow-band connections occur, the Apache server will occupy a large amount of memory and cannot be released. If one more nginx is used as the Apache proxy, these narrow-band connections will be blocked by nginx, apache does not pile up too many requests, which reduces the resource usage. This also applies to squid. Even if squid is configured as not cached, it is of great help to Apache.
7. nginx supports HTTP, https, and email (fewer email functions). LVS supports more applications than nginx. In terms of use, generally the frontend should adopt LVS, that is, the DNS should point to the LVS balancer. The advantages of LVS make it very suitable for this task. Important IP addresses are best managed by LVS, such as the IP address of the data library and the IP address of the WebService server. Over time, these IP addresses will become larger and larger, if the IP address is changed, the fault will come one after another. Therefore, it is the most secure to hand over these important IP addresses to LVS for hosting. The only drawback of this operation is that the number of VIP addresses is large. Nginx can be used as an LVS node machine. First, nginx functions can be used, and second, nginx performance can be used. Of course, squid can also be used directly at this layer. Squid features are much weaker than nginx, and performance is inferior to nginx. Nginx can also be used as a middle-layer proxy. Basically, nginx has no rival in this layer. The only thing that can shake nginx is Lighttpd. However, Lighttpd is not fully capable of nginx yet, the configuration is not so clear and easy to read. In addition, the IP address of the Middle-layer proxy is also important, so the Middle-layer proxy also has a VIP and LVS is the most perfect solution. The specific application has to be analyzed. If it is a relatively small website (with a daily PV of less than 10 million), nginx can be used. If there are many machines, DNS round-robin can be used, LVS consumes a large number of machines. When large websites or important services are not worried about, LVS should be used more.

Currently, the use of network load balancing is based on different stages as the scale of the website increases:

Phase 1: Use nginx or haproxy for single-point load balancing. In this phase, the server scale is just out of the billing server and single database mode and requires a certain amount of load balancing, however, there is still a small scale, no professional maintenance team for maintenance, and no large-scale website deployment is required. In this way, nginx or haproxy is the first choice. At this time, these items are easy to configure and can be configured over layer-7 using the HTTP protocol. This is the first choice.

Stage 2: As the network service expands, nginx with single points cannot meet the requirements. LVS or commercial array is the primary choice, and nginx is used as the node of LVS or array, the specific LVS or array options are based on the company's size and budget,The application delivery function of array is very powerful. I have used it in a project, and its cost effectiveness is much higher than that of F5. It is the first choice for commercial use!However, at this stage, the related talents cannot keep up with the business improvement, so purchasing commercial load balancing has become the only way to go.

Stage 3: network services have become a mainstream product. At this time, with the further expansion of the company's popularity, the capability and quantity of relevant talents have also increased. At this time, no matter whether it is developed to suit the customization of its own products, as for cost reduction, the open-source LVS has become the first choice. At this time, LVS will become the mainstream.
Finally, an ideal basic architecture is formed:Array/LVS-nginx/haproxy-squid/Varnish-appserver.

Related Article

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: 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.