Moment For Technology

Network Adventures: Layer 4 load balancing and its application

Posted on Dec. 2, 2022, 11:13 a.m. by 林宜君
Category: The back-end Tag: Load balancing

I remember occasionally being asked how to reinstall a computer or how to crack a piece of software, and every time I was asked, I would tell people what I had found on Baidu because I didn't know (yes, I was happy).

I tell this example, to illustrate, we are engaged in a certain position in the industry, so not in the computer industry, will install and repair computers; Not engaged in the art industry, must be proficient in music, chess, calligraphy and painting.

For me, my knowledge of the underlying network is limited to understanding, like some network protocol or how it works, I always read and forget, forget and read again.

Just so you know, taking the time to get to know your company's network architecture and the components that it uses, you've finally caught up with yourself. So record your understanding of layer 4 load balancing and its application before you forget.

Noun explanation

LVS: Linux Virutal Server, a Linux virtual Server. DR: Direct Route. RS: Real Server. DIR: Director, transponder in LVS. VIP: public IP address exposed by the LVS. RIP: indicates the IP address corresponding to RS. NAT: Indicates Network Address Transfer.

Why do you need load balancing

Load balancing refers to the process of evenly forwarding requests to one backend server according to a policy when there are multiple backend servers. When the volume of requests is small, only one back-end server can handle all the requests, so it doesn't need to be so complicated. When the volume of requests is high and there are multiple servers, you need to consider load balancing.

Layer 4 load balancing

Layer 4 load balancing, as the name suggests, is load balancing at layer 4, the transport layer. It is load balancing based on IP+PORT. The widely used four-tier load balancing components are LVS. The basic working principle of LVS is as follows:The FORWARDING modes used in the LVS are NAT and IP tunnel. The following describes the two modes.


NAT refers to the technology that translates the source IP address or destination IP address of an IP packet through a firewall or router. It can be used to match a public IP address to multiple private network hosts, originally to solve the ipv4 IP resource shortage situation.

Principle of THE NAT mode of the LVS

LVS obtains RIP through a certain load balancing algorithm, and then overwrites the destination IP address and source IP address of the request body and response body to enable the client to communicate with RS.

The IP tunnel

NAT is the most common mode. It is simple and flexible, and saves IP resources. You only need to set THE VIPs to public IP addresses and all RIP to private IP addresses. But there is a problem: both request and return bodies are forwarded through LVS, and LVS can become a bottleneck if the volume is large. The IP tunnel mode can alleviate this problem by allowing the return body to be directly forwarded to the client at the cost of all RIP needing public IP addresses. An IP tunnel is a network communication channel in which IP packets are twice encapsulated and forwarded.

IP tunnel mode of LVS

LVS obtains RIP through a load balancing algorithm, encapsulates the request body twice, and forwards the new request body to RS. After RS unpacks the new request body, it continues to unpack the request body and finds the VIP whose destination IP address is bound to the network adapter. Therefore, after the RS processing is complete, the return body directly returns to the client through the RS public network egress, and the source IP address is still VIP.

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.