Free tech learning for everyone, from basics to pro

High AvailabilitySystems ArchitectureIntermediate

Load balancers: layer 4 versus layer 7

In short: A load balancer gives clients one address and spreads their traffic across healthy backend servers. Layer 4 balancers forward TCP or UDP connections quickly; layer 7 balancers read HTTP requests and can route by path or host, terminate TLS and apply security rules.

What a load balancer does

Request through a load balancerClientconnects toVIPLoad balancerchoosesbackendHealth checkskip failednodesBackendserves requestResponseback to client
Figure 1. The balancer exposes one virtual IP and only sends traffic to backends that pass health checks.

A load balancer presents a single virtual IP (VIP) or DNS name to clients and distributes their requests across a pool of backend servers. It improves availability, because failed servers are taken out of rotation, and scalability, because you can add servers behind the same address. It also lets you take a server out for patching without users noticing.

Layer 4 versus layer 7

Layer 4 versus layer 7Layer 4Sees IP and port onlyVery fast, low overheadAny TCP or UDP protocolNo content routingLayer 7Reads HTTP headers, pathsRoutes by URL or hostCan terminate TLSMore CPU per request
Figure 2. Layer 4 balancers forward connections; layer 7 balancers understand the application request.

A layer 4 balancer works at the transport layer of the OSI model. It sees source and destination IP addresses and ports, picks a backend and forwards the connection. It does not read the content, so it is very fast and works for any TCP or UDP protocol, such as databases, mail or game traffic.

A layer 7 balancer works at the application layer. It reads HTTP methods, hostnames, paths, headers and cookies. It can send /api to one pool and /images to another, host several sites on one IP, rewrite headers, compress responses and apply a web application firewall (WAF). It usually terminates the client connection and opens a new one to the backend.

NeedBetter choice
Non-HTTP protocols or very high throughputLayer 4
Route by URL path or hostnameLayer 7
Inspect or block malicious HTTP requestsLayer 7 with WAF
Keep end-to-end encryption without decryptingLayer 4 TLS passthrough

Algorithms, health checks and persistence

  • Round robin: each server in turn. Simple; weighted versions favour larger servers.
  • Least connections: send to the server with fewest active connections; good for long or uneven requests.
  • Hash-based: hash of client IP or a key picks the server, so the same client usually lands on the same backend.

Health checks probe each backend regularly. A TCP check only proves the port is open; an HTTP check against a page such as /health that also tests the database is much better. Session persistence (sticky sessions) keeps a user on one server, often with a cookie, but it weakens balancing and loses sessions if that server fails. Storing sessions in a shared cache is more robust.

A minimal HAProxy example of a layer 7 pool with health checks:

frontend web_in
    bind *:443 ssl crt /etc/haproxy/certs/example.com.pem
    default_backend web_pool

backend web_pool
    balance leastconn
    option httpchk GET /health
    server web1 192.168.10.11:8080 check
    server web2 192.168.10.12:8080 check

Avoiding a new single point of failure

The balancer itself must be redundant: a pair of appliances using VRRP to share the VIP, or a managed cloud load balancer that spans several availability zones. For global services, combine local balancers with DNS-based global load balancing.

Tip: when you terminate TLS at a layer 7 balancer, the backend sees the balancer's IP. Pass the real client IP in the X-Forwarded-For header or with the PROXY protocol so logs stay useful for security investigations.

Practice

You need to balance traffic to a cluster of SMTP servers. Layer 4 or layer 7 HTTP?
Layer 4, because SMTP is not HTTP and the balancer only needs to forward TCP connections.
Why is an HTTP health check better than a TCP port check?
It confirms the application actually returns a correct response, not just that the port is open.
What is a drawback of sticky sessions?
Load spreads unevenly, and users lose their session if their server fails, unless sessions are stored centrally.

Key takeaways

  • Load balancers give one address and spread traffic across healthy servers.
  • Layer 4 forwards connections fast for any TCP or UDP protocol.
  • Layer 7 reads HTTP, routes by path or host, and can terminate TLS.
  • Use meaningful health checks and avoid relying on sticky sessions.
  • Make the load balancer itself redundant.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.