Introduction ( TL;DR )
If you are familiar with Google Cloud Platform or AWS, you must be aware of famous software-based load balancers, all they do is, efficient distribution of network or application traffic across multiple servers. They usually sit in between client devices and backend servers.
Now there are several options for selecting a Load Balancer from popular Cloud Providers. Today, let's discuss the differences between a Load Balancer based on layers of the OSI Model: Layer 4 and Layer 7.
Layer 4 Load Balancer
Layer 4 load balancers usually route traffic based on IPs, TCP, or UDP ports, do not care about the content of the message, and deals with the delivery of the packets. The load balancer delivers traffic by combining this limited network information with a load balancing algorithm such as round-robin and by calculating the best destination server based on the least connections or server response times.
- Control routing rules based on IP or TCP/UDP packets
- A simple approach for Packet-Level load balancing
- Messages are neither inspected nor decrypted, allows them to deliver messages quickly, efficiently, and securely.
- Preservers client source IP
- Not possible to route traffic based on media-type, localization rules.
- It is fast but can’t perform any action on the protocol above layer 4.
- Cannot access features of Application Layer, eg. routing based on the path
Layer 7 Load Balancer
Dealing with the actual content of each message, HTTP is the predominant Layer 7 protocol for website traffic on the Internet. Layer 7 load balancers route network traffic in a much more sophisticated way than Layer 4 load balancers, particularly applicable to TCP‑based traffic such as HTTP.
While the need for encryption incurs a performance penalty for layer 7 processing, this can be largely reduced through the use of SSL offload functionality.
Layer 7 Load Balancer supports a unique feature called Session Affinity or Sticky Session which can identify unique client sessions to provide server persistence, sending all client requests to the same server for greater efficiency.
A device that performs Layer 7 load balancing is often referred to as a reverse‑proxy server.
- Control routing rules based on IPs, TCP or UDP ports, or any information it can get from the application protocol (mainly HTTP)
- It is capable of performing optimizations and changes to the content (such as compression and encryption)
- Session Affinity or Sticky Sessions for identifying servers
- SSL/TLS offloading
- More CPU‑intensive than packet‑based Layer 4 load balancing.
- Little slower than Layer 4 LB because of the complex underlying algorithm.
Decision Tree ( GCP )