Loadbalancing Basics
When scaling modern applications, load balancers act as the ultimate traffic cop. They sit between incoming client requests and backend server pools, ensuring no single server gets overwhelmed while maximizing throughput, minimizing latency, and guaranteeing high availability. Understanding how load balancers fit into system design requires looking at two core
High Availability Configurations
To prevent the load balancer itself from becoming a single point of failure (SPOF), production environments deploy load balancers in paired or multi-node redundancy modes.
active-passive loadbalancing
In an Active-Passive (or Primary-Standby) architecture, two identical load balancers are deployed, but only one processes incoming traffic at a time.
- How it works: The primary Active load balancer routes all incoming requests. The Passive load balancer runs silently in the background, continuously monitoring the active node via heartbeats.
- Failover mechanism: If the active load balancer fails or drops heartbeats, a floating/virtual IP (VIP) automatically shifts to the passive node, promoting it to active status. keepalived paired with VRRP for transferring the VIP to the passive node.
- Pros: Simple to implement, straightforward session management, zero resource competition.
- Cons: Idle resources (passive unit remains unused during normal operations), brief failover downtime while IP reassignment occurs.
active-active loadbalancing
In an Active-Active setup, multiple load balancers run concurrently, sharing the incoming traffic workload.
- How it works: Incoming requests are split across all active instances simultaneously using methods like DNS round-robin, Equal-Cost Multi-Pathing (ECMP), or Anycast routing.
- Failover mechanism: If one node goes down, remaining active nodes automatically absorb its share of the network traffic.
- Pros: Maximum resource utilization, superior throughput, near-zero failover downtime.
- Cons: Higher configuration complexity, potential for state synchronization challenges across nodes.
Network Layer Categorization (OSI Model)
Load balancers are also categorized by the layer of the Open Systems Interconnection (OSI)
L4 loadbalancers
layer 4 load balancers operate at the Transport Layer (TCP/UDP protocols).
- Routing Logic: L4 routing decisions are based strictly on networking data like source IP, destination IP, and TCP/UDP port numbers. They do not inspect the payload content inside the network packet.
- Packet Handling: They perform basic Network Address Translation (NAT), forwarding TCP/UDP streams to target backend servers without opening the packet context.
- Key Strengths: Ultra-fast, highly efficient, low CPU/RAM consumption, ideal for raw throughput.
- Use Cases: High-volume traffic, video streaming, non-HTTP services (e.g., database connections, SMTP).
L7 loadbalancers
Layer 7 load balancers operate at the Application Layer (HTTP, HTTPS, WebSockets).
- Routing Logic: L7 load balancers terminate the connection, decrypt incoming traffic, and inspect the actual message content—including HTTP headers, cookies, URLs, and JSON/form payloads.
- Smart Features: Enables path-based routing (e.g., sending
/apito one microservice and/imagesto a storage service), SSL/TLS termination, and HTTP session stickiness. - Key Strengths: Highly intelligent routing, fine-grained access control, integrated web application firewall (WAF) functionality.
- Use Cases: Complex microservices architectures, RESTful APIs, modern web applications requiring path/header routing.
Core Differences: L4 vs. L7
- Data Inspected: Layer 4 inspects IP addresses and TCP/UDP ports, whereas Layer 7 inspects HTTP/HTTPS headers, URLs, cookies, and payload data.
- Performance & Speed: Layer 4 is extremely fast with ultra-low latency. Layer 7 is slightly slower due to packet decryption and content inspection.
- Resource Usage: Layer 4 requires low CPU and memory overhead, while Layer 7 demands high CPU and memory resources to parse application data.
- Routing Capability: Layer 4 provides basic packet forwarding. Layer 7 delivers content-aware, path-based routing and sticky session capabilities.
- Security Options: Layer 4 handles basic IP/port filtering, whereas Layer 7 supports SSL termination, Web Application Firewalls (WAF), and rate limiting.
- Tooling used IPVS is used for layer4 loadbalancing whereas haproxy, nginx is used for layer 7 loadbalancing