Layer 4 load balancing operates at the transport layer of the Open Systems Interconnection (OSI) model. This layer enables connection-oriented data streaming, reliability, flow control, and multiplexing to handle concurrent requests on a single connection.
How does Layer 4 load balancing work?
Layer 4 load balancing distributes network traffic based on information found in the transport layer headers of the data packets. This typically includes information such as source and destination IP addresses, as well as ports.
Layer 4 load balancers forward client requests based on this information, directing traffic to available servers based on various algorithms to help optimize resource consumption. It functionally emulates a firewall while performing health checks along the way. They also handle TCP and UDP traffic—the former of which accounts for approximately 91.5% of the internet's overall traffic share.
Layer 4 load balancing also comes in multiple forms:
Direct Routing (DR) – The load balancer routes requests directly to backend servers by rewriting destination MAC addresses.
Network Address Translation (NAT) – The load balancer distributes traffic across multiple similar network interfaces. NAT methods can include sticky IP, round-robin, remapping, and random distribution.
Source Network Address Translation (SNAT) – The load balancer forwards incoming traffic attributed to one IP address to one of multiple firewall-protected servers.
Layer 4 load balancers don't inspect packet content. Additionally, Layer 4 load balancers often ship as dedicated hardware devices and have since evolved to handle protocol developments over time.
Why is Layer 4 load balancing useful?
Because a staggering number of applications generate TCP traffic, Layer 4 load balancers have become essential infrastructure components and help prevent application servers from becoming overloaded. They're fast and enhance the scalability, reliability, and performance of web applications and APIs. Many such load balancers also operate within a greater application delivery controller (ADC) system—playing pivotal roles in security and application acceleration.
Relatively speaking, Layer 4 load balancing is simpler than application-aware Layer 7 load balancing. Few infrastructure changes are needed to accommodate them, ultimately underscoring their plug-and-play nature (especially when considering hardware appliances).
Does HAProxy offer Layer 4 load balancing?
Yes! HAProxy, HAProxy Enterprise, HAProxy ALOHA, and HAProxy (Enterprise) Kubernetes Ingress Controller support Layer 4 load balancing. We can balance all TCP traffic across a wide variety of deployment models and application environments.