In this article, we have covered the idea of Layer 4 and Layer 7 Load Balancing along with advantages and disadvantages. This is an important topic in System Design and is an effective Load Balancing technique.
Table of content:
- What is a load Balancer?
- Layer 4 Load Balancing
3. Advantages of using a Layer4 Load Balancer
4. Disadvantages of using Layer4 Load Balancer
- Layer 7 Load Balancing
6. Advantages of Layer 7 Load Balancing
7. Disadvantages of Layer 7 Load Balancing
- Layer 4 vs Layer 7 Load Balancing
What is a load Balancer?
A load balancer manages and carefully distributes the network traffic among backend servers.
In this article, we will discuss layer 4 and layer 7 load balancing. Layer 4 and layer 7 load balancing occurs in the Transport Layer and Application Layer of the OSI Model respectively as shown in the image below.
Layer 4 Load Balancing
In layer 4 load balancing, the load balancer has limited data information. It only has visibility on network information which is the ports and the IP address of the client who is sent in the request.The load balancer doesn't really know the content of the data it's receiving because the data is usually encrypted or scrambled.
In this form of load balancing, all client requests are sent to the load balancer which then analyzes the data and distributes them to the appropriate backend servers based on an inbuilt software algorithm. For instance, a load balancer with IP address 18.104.22.168 receives data from the client with IP address 22.214.171.124 and a destination IP address of 126.96.36.199.
The load balancer process the request by changing the destination IP address to the appropriate backend server IP address let's say 188.8.131.52 and also changes the IP address of the client to that of the load balancer which is 184.108.40.206. During this whole process only one TCP connection is made.
The decision of the choice of the destination is based on inbuilt software algorithm such as a round robin algorithm. The changes in IP address is done using network address translation. During the whole operation, the client has no idea which server handles its request and vice versa.
Advantages of using a Layer4 Load Balancer
- Simpler Load Balancing
- Efficient and fast since there's no data look up.
- There is only one TCP connection between the destination and source.
- It uses the network address translation
Disadvantages of using Layer4 Load Balancer
- There is no smart load balancing because the load balancer can't access request content.
- It is not applicable to microservices.
- There's no caching since you can't access the data.
Layer 7 Load Balancing
In layer 7 load balancing, the load balancer can access the application data and can be authorized to decrypt the data sent through the request. There can also be a TLS connection between the client and the load balancer. Data can be accessed easily when the connection is http. Therefore, based on the data accessed the load balancer can easily make decision on which backen server the data is supposed to be sent.
Suppose, we have two servers dedicated to pictures and comments respectively. If the client makes a GET/pictures request, the load balancer creates a new TCP package and establish a connection between itself and the server responsible to the process pictures request. Similar to layer 4 load balancing, the server does not know which client sent the request and vice versa.
However, in layer 7 load balancing, the load balancer might modify the content or even change the header. This is because the load balancer has full access to the data and it's content once it's authorized to decrypt it. It should also be noted that several TCP connections are made in layer 7 load balancing.
Advantages of Layer 7 Load Balancing
- There is smart load balancing since load balancing in this layer has application awareness.
- Caching can be implemented.
- It is great for microservices.
Disadvantages of Layer 7 Load Balancing
- It must share the TLS Certificate.
- It has two TCP connections.
- There's decryption of data
Layer 4 vs Layer 7 Load Balancing
The differences between Layer 4 and Layer 7 Load Balancing are:
- Layer 4 uses only TCP connection from client to the server while layer 7 uses two TCP connections from client to the server.
- Layer 7 has application awareness whilst layer 4 only has on network and application ports.
- Layer 7 makes smart and informed load balances based on the content of the data,however, layer 4 carries out its load balancing based on its inbuilt software algorithm.
- Layer 4 is not applicable to microservices but layer 7 is great for microservices.
Let's recap what we just learnt with some quick True or False question:
A layer 7 load balancer has application awareness
A layer 4 load balancer only has visibility on network information such as application ports and TCP/UDP
With this article at OpenGenus, you must have the complete idea of Layer 4 and Layer 7 Load Balancing.