Load Balancing is simply to form a server cluster with multiple servers, and then assign "work tasks" to the server cluster according to the rules we set.
Various solutions for
load balancing:
HTTP redirect
When a user sends a request, the Web server returns a new URL by modifying the Location tag in the HTTP response header, and then the browser continues to request this new URL, which is actually a page redirection.
Through redirection, to achieve the goal of "load balancing". For example, when we download the PHP source code package, when we click the download link, in order to solve the problem of download speed in different countries and regions, it will return a download address close to us. The redirected HTTP return code is 302
This redirection is very easy to implement, and various strategies can be customized. However, it has poor performance under large-scale visits. Moreover, the user experience is not good, the actual request is redirected, which increases the network delay.
Reverse proxy load balancing
The core work of the reverse proxy service is mainly to forward HTTP requests, acting as a relay between the browser side and the background web server. Because it works at the HTTP layer (application layer), which is the seventh layer in the seven-layer network structure, it is also called "seven-layer load balancing". There are many software that can be used as a reverse proxy, and the more common one is Nginx.
Nginx is a very flexible reverse proxy software that can freely customize forwarding strategies and assign server traffic weights. A common problem in reverse proxy is the session data stored by the Web server, because the general load balancing strategy is to randomly allocate requests. The request of the same logged-in user cannot be guaranteed to be allocated to the same Web machine, which will cause the problem of unable to find the session.
There are two main solutions:
1) Configure reverse proxy forwarding rules, so that the same user's request must fall on the same machine (by analyzing cookies). Complex forwarding rules will consume more CPU and increase the burden of the proxy server.
2) Use an independent service to store information such as session, such as Redis/memchache. This solution is more recommended.
The reverse proxy service can also enable caching. If it is enabled, it will increase the burden of the reverse proxy, and it needs to be used with caution. This load balancing strategy is very simple to implement and deploy, and its performance is relatively good.
However, it has a "single point of failure" problem, if it fails, it will bring a lot of trouble. Moreover, in the later period, Web servers continue to increase, which may become the bottleneck of the system.
DNS load balancing
DNS (Domain Name System) is responsible for the service of domain name resolution. The domain name url is actually the alias of the server. The actual mapping is an IP address. The resolution process is to complete the domain name to IP mapping by DNS. A domain name can be configured to correspond to multiple IPs. Therefore, DNS can also be used as a load balancing service.
This load balancing strategy is simple to configure and has excellent performance. However, rules cannot be freely defined, and it is troublesome to change the mapped IP or the machine fails, and there is also the problem of delay in DNS activation.
CDN/GSLB load balancing
Our commonly used CDN (Content Delivery Network, content delivery network) implementation method is actually based on the same domain name being mapped to multiple IPs, and through GSLB (Global Server Load Balance, global load balance) mapping domain names according to specified rules IP.
In general, according to the geographical location, the IP close to the user is returned to the user to reduce the jump consumption between routing nodes in the network transmission.
In the Web system, CDN is generally used to solve the loading problem of larger static resources (html/Js/Css/pictures/videos/files, etc.), so that these are more dependent on the content downloaded from the network, as far as possible from users. Recently, improve user experience.
This method, like the previous DNS load balancing, has excellent performance and supports the configuration of multiple strategies. However, the cost of construction and maintenance is very high. Internet first-line companies will build their own CDN services, and small and medium-sized companies generally use CDNs provided by third parties.
IP load balancing
The IP load balancing service works at the network layer (modified IP) and transport layer (modified port, fourth layer), and its performance is much higher than working at the application layer (seventh layer).
The principle is that he is to modify the IP address and port information of the IP layer data packet to achieve the purpose of load balancing. This method is also called "four-layer load balancing".
A common load balancing method is LVS, which is implemented through IPVS (IP Virtual Server, IP virtual service).
LVS is short for Linux Virtual Server, which means Linux virtual server, which is a virtual server cluster system.