Learn what layer 4 load balancing is


7th floor load & 4th floor load

The commonly used nginx load balancing technology is in the application layer (layer 7) in the network layer. Nginx establishes a TCP connection (handshake) with the client, and then flexibly distributes the requests to different services according to the request information and local configuration information. The advantages and disadvantages of 7-layer load balancing such as nginx are obvious.

  • advantage: requests can be distributed to different services and can be made according to the request informationflexibleofProxy forwarding; Since the request will pass through the load balancing server, the load balancing server willfilterSome requests (e.g. DoS attacks) prevent all request information from hitting the server and ensure the stable operation of the server.
  • shortcomingIt is at the top layer of the network hierarchy and needs to analyze the dataClient establish connectionLow efficiency

In addition to the 7-layer load balancing strategy of nginx, there is also a load balancing strategy based on transport layer (4-layer). By analyzing the requestedIP address and port numberLoad balancing of requests. According to different request processing modes, 4-layer load balancingalgorithmIt can be divided into:NAT,D-NAT,DRas well asTun tunnel technologyWait. The implementation methods of layer 4 load balancing are:LVS

  • advantage: do not fully parse the data,Do not establish a connection with the client(handshake), requesting distributionFast efficiency
  • shortcomingUnable to be flexibleFor forwarding, the loaded server mustSame server deployedOtherwise, the same request information may obtain different results.

Load mode


NAT (network address translation) technology allocates a load balance service that implements the routing or service of NAT technology in the private internal network. This load balancing server is assigned a public IP (VIP, virtual IP), which is requested by all clients requesting services. Lbs modifies the source IP and target IP of the request packet through different algorithms and forwards them to the real service for business processing. The specific steps can be divided into:

Learn what layer 4 load balancing is

1. The client sends a request. The source IP is CIP and the target IP is VIP

2. Lbs receives the request, parses the datagram IP address, modifies the source IP to VIP and the directory IP to rip. The specific modification is the target IP, and different choices are made through different algorithms. And send data packets after storing relevant information (map).

3. RS receives the request and performs service processing. And return the result to lbs.

4. Lbs receives the corresponding data packet, changes the corresponding SIP to VIP and tip to CIP according to the previously stored map, and sends the data packet.

be careful:connectIs based onClient and real serviceBetween, LBS just parses the packet ip and port number, modifies and forwards it.

__ It can be seen that load balancing is carried out through NAT mode. All requests and responses must pass through the LB server. When the traffic is large, the LB server will become a bottleneck _.


Learn what layer 4 load balancing is

DR (direct routing mode), LB modifies the target MAC address of the request packet, and onlySelf visible Lo: VIP, realize the receiving of data packets (if you don’t have a VIP, the service will not receive data packets). The whole forwarding process is:

  1. Client sends request, source IP: CIP, destination IP: VIP
  2. LB receives the request and willData frameModify the target MAC address in to the address of real serviceMAC address, select which real service to forward to through the scheduling algorithm.
  3. The real service receives the request and finds that it has a VIP and the port number matches. Therefore, the data is received for service processing. The VIP is used as the IP address to send the response data directly to the client.

In Dr mode, LB only receives requests for forwarding, and the response data is directly sent to the client by real service, which reduces the pressure on lb. However, both NAT and Dr require LB and real service to be in the same network segment, so RS cannot be deployed in different machine rooms.

Tun tunnel technology

Learn what layer 4 load balancing is

The idea of Tun is similar to that of Dr. an internally visible Lo: VIP address is configured on the real service. LB forwards the request by encapsulating or modifying the packet information. Different from Dr mode, LB modifies the MAC address in order toImplement different network segmentsThe real service load of Tun mode passes outside the original data packetEncapsulate a layer of IP tunnel, realize data forwarding. Since the packet structure after encapsulating the IP tunnel is different from the normal packet structure, the real serviceThe OS needs to support the tunnel function。 The specific process of Tun forwarding is as follows:

  1. Client sends request, SIP: CIP; TIP:VIP
  2. LB receives the request and encapsulates a layer of IP tunnel outside the data packet. Its SIP is dip and tip is RIP3
  3. After receiving the request, the real service determines that it is a request sent to itself according to the IP address of the IP tunnel for subsequent processing. According to the IP address of the source packet, it is determined that the address of the client is CIP, and the VIP is also the IP configured by itself. After the data is processed, the response data is directly sent to the client through the VIP.

scheduling strategy

Although LB does not fully parse the packet and cannot know the request information, it can judge the connection between the client and the real service by listening to the request header information (for example, syn, fin, etc.). LB maintains the connection information table of each real service by listening to the request information. Through this information, different scheduling algorithms are realized for load balancing.

Static scheduling strategy

  • RR polling

LB forwards requests to different real services in turn

  • WRR weighted polling

Assign different weights to real service, and LB forwards the request according to the weight of rs

  • DH destination address hash

Hash according to the requested target address (resource, such as the same URL) and forward it to rs

  • SH source address hash

Hash the domain name or IP of the client and forward it to rs

Dynamic scheduling strategy

  • LC minimum connection

The LB forwards the request to the least connected rs

  • WLC weighted minimum connection

LB forwards the request through weighted polling and RS connection

  • Lblc is based on local minimum connection

LB maintenanceTarget IP to an RSThe mapping table (the RS recently used by the target IP) forwards the request to the RS through the mapping table. If the RS does not exist or is overloaded, select a new RS through the least connection strategy for forwarding

  • Lblcr local minimum link with replication

LB maintenanceTarget IP to a set of RSThe mapping table (the most recently used rs of the target IP) throughMinimum connectionThe policy selects an RS from the server group for forwarding. If the RS does not exist or is overloaded, select a new RS through the least connection policy for forwarding, and add this rs to the mapping group.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]