This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details
The opening
This is the 10th day of my participation in Gwen Challenge
In the face of a large number of users access, high concurrent requests, huge amounts of data, you can use the high performance server, a large database, storage devices, high-performance Web server, USES efficient programming language (Go, Scala), for example, when single capacity limits, we need to consider the business split and distributed deployment, trafficked to solve large-scale Web site, Concurrency is high, massive data problems.
From a stand-alone website to a distributed website, the important difference is service splitting and distributed deployment. After splitting applications, they are deployed on different machines to achieve a large-scale distributed system. Distribution and business separation solve the problem of moving from centralization to distribution, but there are also problems of single points of failure and unified access for each deployed independent business. To solve the single point of failure, we can adopt redundant approach. Deploy the same application on multiple machines. To solve the problem of unified access, we can add load balancing devices in front of the cluster to achieve traffic distribution.
Load Balance refers to the balancing and distribution of loads (work tasks, access requests) across multiple operation units (servers, components) for execution. It is the ultimate solution for high performance, single point of failure (high availability), and scalability (horizontal scaling).
Principle of load balancing
System expansion can be divided into vertical (vertical) expansion and horizontal (horizontal) expansion.
- Vertical expansion: from the perspective of a single machine by increasing the hardware processing capacity, such as CPU processing capacity, memory capacity, disk and other aspects, to achieve the improvement of server processing capacity, can not meet the large distributed system (website), large traffic, high concurrency, mass data problems. Therefore, a horizontal expansion approach is needed.
- Scale-out: Add machines to meet the processing power of large web services. For example, if one machine cannot meet the requirements, add two or more machines to share the access pressure. This is a typical clustering and load balancing architecture:
Application of the cluster: The same application is deployed on multiple machines to form a processing cluster. The application receives requests from the load balancing device, processes them, and returns corresponding data.
Load balancing device: distributes user access requests to a processing server in the cluster based on the load balancing algorithm. (A device that distributes network requests to available servers in a server cluster)
Functions of load balancing (problems solved) :
- Solve concurrency pressure, improve application processing performance (increase throughput, strengthen network processing capacity);
- Provide failover to achieve high availability;
- Provide web site scalability by adding or reducing the number of servers;
- Safety protection; (Perform some filtering, blacklist and whitelist processing on the load balancing device)
Common load balancing schemes
DNS Load Balancing
The earliest load balancing technology uses domain name resolution to achieve load balancing. Multiple A records are configured on the DNS server, and the servers corresponding to these A records form A cluster. Large web sites always partially use DNS resolution as a first-level load balancer. The diagram below:
advantages
- Using a simple: The DNS server handles load balancing, which saves the maintenance of the load balancing server
- To improve performance: Supports domain name resolution based on the address. The domain name is resolved to the nearest server address, which speeds up access and improves performance.
disadvantages
- Poor usability: DNS resolution is multi-level. After a DNS is added or modified, the resolution takes a long time. During the resolution process, the user will fail to access the site.
- Extensibility is low: DNS load balancing is controlled by the domain name provider, so it cannot be improved or expanded.
- Poor maintenance: Does not reflect the current running status of the server. Fewer algorithms are supported. Inability to differentiate between servers (inability to determine load based on system and service state)
Practice Suggestion: Use DNS as the first-level load balancer, record THE IP address of the internal load balancer, and use the internal load balancer to distribute requests to real Web servers.
IP load Balancing
Load balancing is performed at the network layer by modifying the destination address of the request. After a user requests a packet and reaches the load balancing server, the load balancing server obtains the network packet from the kernel process of the operating system, obtains a real server address based on the load balancing algorithm, and then changes the destination address of the request to the obtained real IP address, without processing by the user process. After processing the packets, the real server returns the packets to the load balancing server. The load balancing server changes the source IP address of the packets to its own IP address and sends the packets to the user’s browser. The diagram below:
(IP load balancing)The real physical server returns to the load balancer server in two ways:
- The load balancing server changes both the destination IP address and the source IP address. The source ADDRESS of a packet is set to its own disk, that is, source Address translation (SNAT).
- The load balancing server also functions as the gateway server of the real physical server cluster.
Advantages:
- Data distribution in kernel process is better than distribution in application layer.
Disadvantages:
- All requests and responses need to pass through the load balancing server. The maximum throughput of the cluster is limited by the network adapter bandwidth of the load balancing server.
Link layer load balancing
Change MAC addresses at the data link layer of communication protocols for load balancing. If the IP address is not changed during data distribution, the target MAC address is changed and the virtual IP addresses of all machines in a real physical server cluster are configured to be the same as the IP addresses of load balancing servers. In this way, the source and target IP addresses of data packets are not changed and data distribution is implemented. The IP address of the actual processing server is the same as the destination IP address of the data request. Therefore, the response packets are directly returned to the user’s browser, avoiding the bandwidth bottleneck of the network adapter of the load balancing server. Also known as direct routing mode (DR mode). The diagram below:
advantages: Good performance;
Disadvantages: Complex configuration;
Practical suggestion: DR mode is the most widely used load balancing mode.
Hybrid load balancing
Due to the hardware devices in multiple server group, their size, provide services such as difference, can consider to give each server group adopt the most appropriate way of load balancing, and then between the multiple server group, load balancing or clustered up again as a whole, provide service to the outside world (i.e., the multiple server group as a new server group). To achieve the best performance. This approach is called hybrid load balancing.
Mode one, as shown below:
The above mode is suitable for the scenario with static and dynamic separation. The reverse proxy server (cluster) can cache and distribute dynamic requests. When static resources are cached on the proxy server, they are directly returned to the browser. If the dynamic page requests the application load balancer (application cluster) behind.
Mode 2, as shown below:
The above mode is suitable for dynamic request scenarios. Due to the hybrid mode, you can flexibly match various methods according to specific scenarios.
The above two methods are for reference only.
Load balancing algorithm
Common load balancing algorithms are polling, random, least link, source address hash, weighting and other methods;
polling
Distribute all requests to each server in turn, which is suitable for the scenario where the servers have the same hardware.
Advantages: Same number of server requests; Disadvantages: The server pressure is different, not suitable for different server configurations;
random
Requests are randomly assigned to each server.
Advantages: simple to use; Disadvantages: not suitable for different scenarios of machine configuration;
The minimum link
Assign requests to the server with the fewest connections (the server currently processing the fewest requests).
Advantages: According to the server’s current request processing situation, dynamic allocation; Disadvantages: the algorithm implementation is relatively complex, need to monitor the server request connection number;
Hash (source address Hash)
The IP address is obtained based on the Hash calculation.
Advantages: Forwards requests from the same IP address to the same server during the same session. Implement session stickiness. Disadvantages: When the target server is down, the session will be lost.
Load balancing based on weights
On the basis of polling, randomization, least linking, Hash and other algorithms, load server allocation is carried out by way of weighting.
Advantages: Adjust the number of forwarding server requests according to the weight; Disadvantages: Relatively complex to use;
Hardware load balancing
Load balancing is achieved by hardware, usually a separate load balancing server, which is expensive and can be considered by general tuhao companies. There are two leading models in the industry, F5 and A10. When using hardware load balancers, consider the following aspects:
- Functional considerations: fully supports load balancing at all levels, comprehensive load balancing algorithms, and global load balancing;
- Performance considerations: it is difficult for a software load balancer to support concurrency up to level 50,000, but a hardware load balancer can support it
- Stability: Commercial hardware load balancers, which have been well and rigorously tested, are high in stability from large-scale use;
- Security protection: In addition to the load balancing function, the hardware balancer also provides security functions such as firewalls and anti-ddos attacks.
- Maintenance: provide good maintenance management interface, after-sales service and technical support;
- F5 Big Ip price: 15W ~ 55W; A10 price: 55W-100W;
disadvantages
- Expensive;
- Poor expansion ability;
Software load balancing
Ngnix Load Balancer Lvs Load Balancer Haproxy load balancer
Application load balancing
Based on Ribbon, an open source project of Netflix, and OpenFeign of Spring Cloud
👍 ❤️ Don’t get lost
The article continues to update every week, you can search wechat “ten minutes to learn programming” the first time to read and urge more, if this article is not bad, feel something if you support and recognition, is the biggest power of my creation, we will see the next article!