Four-layer, seven-layer load balancing differences

Source: Internet
Author: User
Tags email protocols node server haproxy nginx load balancing

original link: https://www.jianshu.com/p/fa937b8e6712
    • Brief introduction
    • Difference
    • Advantages and disadvantages of Nginx, LVS and haproxy load balancer software
First, Introduction
  1. * * The so-called four layer is based on ip+ Port load balancing, seven layer is based on the URL and other application layer information load balancing; * * Similarly, there are two-tier load balancing based on MAC address and three-tier load balancing based on IP address. In other words, a two-tier load balancer receives requests through a virtual MAC address, which is then assigned to a real MAC address, and a three-tiered load balancer receives the request through a virtual IP address, then assigns it to the real IP address, and the four tiers receive the request through the virtual ip+ port before being assigned to the real server Layer seven receives the request through a virtual URL or hostname and then assigns it to the real server.

  2. * * The so-called four to seven load balancing, is in the background of the server load balancing, based on the four layer of information or seven layer of information to determine how to forward traffic. * * For example, four layer of load balancing, is through the release of the three layer of IP address (VIP), and then add four layer of port number, to determine which traffic needs to do load balancing, to handle the traffic to the NAT processing, forwarding to the background server, and record the TCP or UDP traffic is from which server processing, All subsequent traffic for this connection is also forwarded to the same server for processing. Seven layer of load balancing, is on the basis of four layer ( no four layer is absolutely impossible seven layer ), and then consider the characteristics of the application layer, than like a Web server load balancing, in addition to the VIP plus 80 port to identify whether to handle traffic, but also according to the seven layer of the URL, browser category and language to determine whether to load balance. For example, if your Web server is divided into two groups, one for the Chinese language and one for the English language, the seven-tier load balancer can automatically identify the user's language when the user accesses your domain name, and then select the corresponding language server group for load Balancing.

  3. A load balancer is often referred to as a four-layer switch or a seven-layer switch. The four-layer switch mainly analyzes the IP layer and the TCP/UDP layer to achieve four-layer traffic load balancing. In addition to supporting four-tier load balancing, layer seven switches also analyze application layer information, such as HTTP protocol URI or cookie information.

  4. Load balancing is divided into L4 switch (quad Exchange), which is the TCP layer at the OSI 4th level. This type of load balance does not understand the application protocol (such as Http/ftp/mysql, etc.). Example: Lvs,f5.

  5. Another is called L7 switch (seven-layer switching), the highest layer of the OSI, the application layer. At this point, the load balancer can understand the application protocol. Example: Haproxy,mysql Proxy.

Note: Many of the above load balancer can be exchanged in four layers or seven layers.

Second, the difference
  1. Technically,
    the so-called four-layer load balancer , which is the main destination address and port in the message, plus the server selection method of the Load Balancer device settings, determines the final selected internal server.
    in the case of common TCP, the load balancer device, when it receives the first SYN request from the client, chooses an optimal server in the way described above, and modifies the destination IP address in the message (instead of the backend server IP) and forwards it directly to the server. TCP connection is established, that is, the three-time handshake is established directly between the client and the server, the load balancer device is just a router-like forwarding action. In some deployment situations, in order to ensure that the server back-up can be correctly returned to the load balancer device, while forwarding the message may also be the original source address of the message to modify.

      difference

     

    so-called seven-layer load balancing , also known as "content Exchange", that is, mainly through the message of the real meaningful application layer content, coupled with the Load Balancer Device Settings Server selection mode , determine the final choice of internal servers.

    Take the common TCP, for example, if the load balancing device is to select a server based on the actual application tier content, it can only proxy the final server and the client to establish a connection (three handshake) before it can accept the message sent by the client to the real application layer content, and then according to the specific fields in the message, Plus the server selection method of the Load Balancer device setting determines the final selected internal server. Load balancer device In this case, it is more like a proxy server. The load balancer and the front-end clients and the backend servers establish TCP connections separately. So from this point of view, the seven-layer load balancer is significantly more demanding for load balancing devices, and the ability to handle seven layers is bound to be lower than the four-tier mode deployment.

  2. Application Scenarios
    The benefit of seven-tier application load is to make the whole network more intelligent. For example, access to a Web site user traffic, you can pass the request of the picture class to a specific image server through seven layers, and can use the caching technology, the text class request can be forwarded to a specific word server and can use compression technology. Of course, this is only a small case of seven-tier application, from the technical principle, this way can be the client's request and the response of the server in any sense, greatly improved the application system in the network layer of flexibility. Many of the features deployed in the background, such as Nginx or Apache, can be moved forward to the load balancer device, such as header rewriting in customer requests, keyword filtering in server responses, or content insertion.
    Another feature that is often mentioned is security. The most common SYN flood attack in the network, that is, hackers control many source clients, using a false IP address to send SYN attacks to the same target, usually this kind of attack will send a large number of SYN packets, exhausted the relevant resources on the server to achieve denial of Service (DoS ) for the purpose. It can also be seen from the technical principle that these SYN attacks are forwarded to the backend server in the four-layer mode, whereas in the seven-tier mode these SYN attacks are naturally cut off on the load-balanced device without affecting the normal operation of the backend servers. In addition, the load Balancer device can set up various strategies at seven layers, filter specific messages, such as SQL injection and other application-level attack methods, and further improve the overall security of the system from the application level.
    Now the 7-layer load balancing, mainly focus on the application of HTTP protocol, so its application is mainly a number of web sites or internal information platform, such as based on B/s development system. The 4-tier load balancer corresponds to other TCP applications, such as ERP systems based on C/s development.

Three, the advantages and disadvantages of Nginx, LVS and haproxy load Balancing software

Load balancing, built on the existing network structure, provides a cheap and effective way to extend the bandwidth of network devices and servers, increase throughput, enhance network data processing, and improve network flexibility and availability, balancing.

Nginx/lvs/haproxy is currently the most widely used three load balancer software.

The general use of load balancing is to use different technologies depending on the stage of the site's ascent. Specific application needs to be specific analysis, if it is small and medium-sized Web applications, such as the daily PV less than 10 million, with Nginx can be completely; if there are many machines, you can use DNS polling, LVS consumes more machines or more, large web sites or important services, and the server is relatively long, You can consider using LVS.

One is through the hardware, the common hardware has more expensive F5 and array and other commercial load balancer, it is the advantage of a professional maintenance team to the maintenance of these services, the disadvantage is that the cost is too large, so for the smaller network services for the moment there is no need to use The other is Linux-based, open-source, free load balancing software similar to Nginx/lvs/haproxy, which is implemented at the software level, so it's very inexpensive.

At present, the structure of the Web site architecture is generally more reasonable: the Web front-end uses nginx/haproxy+ keepalived as the load balancer, and the backend uses the MySQL database as a master-slave and read-write separation, adopting the lvs+keepalived architecture. Of course, according to the specific needs of the project plan.

The following is the respective characteristics and applicable occasions.

The advantages of Nginx are:

    1. Works on the 7 layer of the network, can do some diversion for the HTTP application strategy, such as for the domain name, directory structure, its regular rules than haproxy more powerful and flexible, which is one of the main reasons for its widespread popularity, The nginx can be used more than LVS on this occasion.

    2. The dependency of the network stability is very small, in theory can ping the load function, this is one of its advantages, but the LVS on the network stability dependence is relatively large. The

    3. Installation and configuration is relatively simple, easy to test, it basically can be printed out the error log. LVS configuration, testing will take a relatively long time, LVS on the network dependence is relatively large.

    4. Can assume high load pressure and stability, in the case of hardware is not bad generally can support tens of thousands of times the concurrency, the load is relatively smaller than LVS. The

    5. Can detect faults on the server through the port, such as the status code returned by the server, timeout, and so on, and will resubmit the request to another node, but the disadvantage is that the URL is not supported for detection. For example, the user is uploading a file, and processing the upload node just in the upload process failure, Nginx will upload to another server to re-processing, and LVS directly broken off, if it is to upload a large file or very important files, users may be dissatisfied.

    6. Nginx is not only a good load balancer/reverse proxy software, it is also a powerful Web application server. Lnmp is also a very popular web architecture in recent years and has a good stability in high-traffic environments.

    7. Nginx is now becoming more mature as a Web reverse-acceleration cache, faster than a traditional squid server, and can be considered as a reverse proxy accelerator.

    8. Nginx can be used as a middle-level reverse proxy, this layer nginx basically no opponent, only can compare Nginx only lighttpd, but LIGHTTPD has not yet done nginx full function, The configuration is not so clear and easy to read, the community data is far from Nginx active. The

    9. can also be used as a static web page and image server, and there is no opponent for performance. The Nginx community is very active and there are many third-party modules.

The drawbacks of Nginx are:

    1. Nginx only supports HTTP, HTTPS, and email protocols, so it's a disadvantage when it's smaller in scope.

    2. Health checks on back-end servers are only supported through ports and are not supported for detection by URLs. The direct hold of the session is not supported, but can be solved by Ip_hash.

LVS: Using a Linux kernel cluster to implement a high-performance, highly available load-balancing server with good scalability (Scalability), Reliability (reliability), and manageability (manageability).

The advantages of LVS are:

    1. Anti-load ability, is the work in the Network 4 layer only for distribution, no traffic generated, this feature also determines its performance in the Load Balancer software, the memory and CPU resource consumption is low.

    2. Low configuration, this is a disadvantage is also a advantage, because there is not much to configure things, so do not need too much contact, greatly reducing the chance of human error.

    3. Stable work, because of its own strong resistance to load, the self has a complete dual-machine hot-standby program, such as Lvs+keepalived.

    4. Without traffic, LVS distributes requests only, and traffic does not go out from itself, which ensures that the performance of the Equalizer Io is not affected by large traffic.

    5. The application scope is wide, because LVS works on 4 levels, so it can load balance almost all applications, including HTTP, databases, online chat rooms, and so on.

The disadvantages of LVS are:

    1. The software itself does not support regular expression processing, can not do static and dynamic separation, and now many sites in this area have a strong demand, this is the advantage of nginx/haproxy+keepalived.

    2. If it is a large web site application, lvs/dr+keepalived implementation is more complex, especially after the Windows server machine, if the implementation and configuration and maintenance process is more complex, relatively speaking, nginx/haproxy+ Keepalived is a lot easier.

Haproxy is characterized by:

    1. Haproxy is also supported for virtual hosts.

    2. The advantages of haproxy can be supplemented with some of Nginx's shortcomings, such as supporting session retention, cookie guidance, and the ability to detect the status of the backend server by obtaining the specified URL.

    3. Haproxy similar to LVS, itself is just a load balancer software, simply from the efficiency of haproxy will be better than nginx load balancing speed, in concurrent processing is better than nginx.

    4. HAProxy supports the TCP protocol load balancing forwarding , can load balance MySQL read, the backend of the MySQL node detection and load balancing, you can use lvs+keepalived to load balance MySQL master and slave.

    5. Haproxy Load Balancing strategy is very many, the Haproxy load balancing algorithm now has the following 8 kinds of specific:
      ①roundrobin, said the simple polling, this is not much to say, this is the load balancer basically have;
      ②STATIC-RR, said according to the weight, suggest concern;
      ③leastconn, indicating that the minimum number of connections to deal with, suggest concern;
      ④source, according to the request source IP, this is similar to Nginx's ip_hash mechanism, we use it as a way to solve the session problem, it is recommended to pay attention to;
      ⑤ri, which represents the URI according to the request;
      ⑥rl_param, which represents the URL parameter according to the request ' balance Url_param ' requires an URL parameter name;
      ⑦HDR (name), which indicates that each HTTP request is locked according to the HTTP request header;
      ⑧rdp-cookie (name), which indicates that each TCP request is locked and hashed according to the cookie (name).

A summary of the comparison between Nginx and LVs:

  1. Nginx work in the network layer 7, so it can be targeted at the HTTP application itself to do the shunt strategy, for example, for the domain name, directory structure, compared to LVS does not have such a function, so the nginx alone can use the occasion far more than LVS But Nginx is useful for these functions to make it more adjustable than LVS, so often to touch touch, touch more, the probability of human problems will be large.

  2. Nginx dependence on network stability is small, in theory, as long as the ping, Web Access is normal, nginx can be connected, this is a big advantage of Nginx! Nginx at the same time can also distinguish between internal and external network, if the node has both internal and external network, the equivalent of a single machine has a backup line, LVS is more dependent on the network environment, at present, the server in the same network segment and LVS using direct mode shunt, the effect is more guaranteed. In addition, the LVS need to apply to the custodian at least one more IP to do the visual IP, it seems that it is not possible to use their own IP to do VIP. To do a good job of the LVS administrator, do have to follow up learning a lot about network communication knowledge, it is no longer an HTTP so simple.

  3. Nginx installation and configuration is relatively simple, testing is also very convenient, because it basically can be used to print the error log. LVS installation and configuration, testing will take a long time, LVS on the network rely on a large number of times can not be configured to succeed is because of network problems rather than configuration problems, the problem to solve also the corresponding will be more troublesome.

  4. Nginx can also withstand a high load and stability, but the load and stability of the LVS there are several levels: Nginx processing all traffic is limited to machine IO and configuration, and its own bugs are still unavoidable.

  5. Nginx can detect the internal faults of the server, such as the status code returned by the server processing the page, timeout, and so on, and will return the wrong request resubmitted to another node. At present, LDIRECTD in LVS can also support monitoring the internal conditions of the server, but the principle of LVS makes it impossible to re-send requests. For example, the user is uploading a file, and processing the upload node just in the upload process failure, Nginx will upload to another server re-processing, and LVS is directly broken off, if it is to upload a large file or very important files, users may be annoyed.

  6. Nginx asynchronous processing of the request can help the node server to reduce the load, if the use of Apache Direct External services, then a lot of narrow-band links when the Apache server will occupy a lot of memory and can not be released, using more than one nginx to do Apache agent, These narrow-band links will be blocked by Nginx, Apache will not accumulate too many requests, which reduces a considerable amount of resource consumption. This use squid also has the same effect, even if the squid itself is configured to not cache, the Apache still has a lot of help.

  7. Nginx can support HTTP, HTTPS and email (the function of email is less), LVS support the application in this more than Nginx. In use, the general front-end strategy should be the LVS, that is, the DNS point should be the LVS equalizer, the advantages of LVS make it very suitable for this task. Important IP address, preferably by the LVS hosting, such as the database IP, WebService server IP and so on, these IP addresses over time, the use of polygons will become larger, if the replacement of IP will follow. So it is most prudent to give these important IPs to the LVS hosting, the only disadvantage is that the number of VIPs will be more. Nginx can be used as the LVS node machine, one can use Nginx function, the second is the performance of Nginx can be used. Of course, this level can also directly use the function of Squid,squid is weaker than Nginx, performance is also inferior to nginx. Nginx can also be used as a middle-tier agent, this level nginx basically no opponent, the only one can shake Nginx only lighttpd, but lighttpd at present has not been able to do nginx full function, configuration is not so clear and easy to read. In addition, the middle-level proxy IP is also important, so the middle-level agent also has a VIP and LVs is the most perfect solution. Specific application also have to be specific analysis, if it is relatively small site (PV less than 10 million), with Nginx can be completely, if the machine is also a lot, you can use DNS polling, LVS the cost of the machine is more; large web sites or important services, machines do not worry about the time, You should consider using LVs a lot.

The use of Network Load Balancing is now using different technologies as the scale of the site increases depending on the stage:

The first stage: using Nginx or Haproxy for a single point of load balancing, this stage of the server scale just out of the single-server, single-database mode, requires a certain load balancing, but still small, no professional maintenance team to maintain, there is no need for large-scale site deployment. This use of Nginx or Haproxy is the first choice, at this time these things get started fast, easy to configure, on the seven layer above the use of HTTP protocol can. This is the first choice.

The second stage: with the further expansion of network services, then the single-point nginx can not be satisfied, when the use of LVS or commercial array is the first choice, nginx at this time as a LVS or array of nodes to use, The specific LVS or array is selected according to the size of the company and the budget to choose, the application delivery function of the array is very powerful, I used in a project, cost-effective is much higher than the F5, commercial preferred, but generally this phase of the relevant talent can not keep up with the promotion of business, So buying a business load balancer has become a necessity.

The third stage: The Network Service has become the mainstream products, at this time with the company's popularity is further expanded, the ability of relevant talents and the number of the increase, then regardless of the development of their own product customization, and reduce the cost of the open source LVs, has become the first choice, then LVS will become the mainstream.

The final form of the ideal basic structure is: Array/lvs-nginx/haproxy-squid/varnish-appserver.

Four-layer, seven-layer load balancing differences

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: info-contact@alibabacloud.com 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.