Skip to content

Load Balancer (L4 vs L7)

Distributes incoming traffic across multiple servers so no single server gets overwhelmed.

Clients


Load Balancer
   ├── Server 1
   ├── Server 2
   └── Server 3

L4 — Transport Layer Load Balancer

Operates at TCP/UDP level. Doesn't look inside the packet, just routes based on:

  • Source/Destination IP
  • Source/Destination Port
  • Protocol (TCP/UDP)
Client --- LB sees: IP + Port only --- forwards to server
           (never opens the packet)
  • Very fast — no deep inspection.
  • Can't make smart routing decisions (doesn't know if it's HTTP, video, API).
  • One TCP connection: Client → LB → Server (NAT-based or TCP passthrough).
  • Use case: Raw speed, non-HTTP traffic, database connections, game servers.

L7 — Application Layer Load Balancer

Operates at HTTP/HTTPS level. Actually reads the request content:

  • URL path
  • Headers
  • Cookies
  • HTTP method
Client --- LB reads full HTTP request --- smart routing decision
           "oh this is /api → send to API servers"
           "oh this is /video → send to media servers"
  • Smarter routing decisions.
  • Can terminate TLS (decrypt, inspect, re-encrypt).
  • Can modify requests/responses.
  • Slightly more overhead than L4.
  • Use case: Web apps, microservices, API gateways.

L4 vs L7 Comparison

L4L7
LayerTransport (TCP/UDP)Application (HTTP)
SeesIP + PortFull request content
SpeedFasterSlightly slower
Routing based onIP/PortURL, headers, cookies
TLS terminationNoYes
Use caseDB, games, raw TCPWeb, APIs, microservices

Load Balancing Algorithms

AlgorithmHow
Round RobinRequests go 1→2→3→1→2→3 in order
Weighted Round RobinPowerful servers get more requests
Least ConnectionsSend to server with fewest active connections
IP HashSame client IP always goes to same server (sticky)
RandomPick a server randomly

Key Concepts

Health Checks

  • LB periodically pings servers.
  • If server fails → removed from pool automatically.

Session Persistence (Sticky Sessions)

  • Same user always hits same server.
  • Needed when session state is stored locally on server.
  • L7 can do this via cookie, L4 via IP hash.

TLS Termination

  • L7 LB decrypts HTTPS traffic.
  • Backend servers receive plain HTTP.
  • Offloads crypto work from app servers.
Client --HTTPS--> L7 LB (decrypts) --HTTP--> Backend servers

Examples

  • L4 — AWS NLB, HAProxy (TCP mode)
  • L7 — AWS ALB, Nginx, Cloudflare, HAProxy (HTTP mode)