Skip to content
hostcomplex

Layer 4 vs Layer 7 Load Balancing

Maarten Soetens 3 min read

What the Layer Numbers Mean

Layer 4 vs layer 7 load balancing describes how much of a network request a load balancer reads before choosing a server. Layer 4 works at the transport level and uses network information such as IP addresses and port numbers, which are numbered entry points for a service. Layer 7 works at the application level and can read the content of HTTP requests, the messages web browsers send to ask for pages.

The numbers come from the OSI model, a reference that divides network communication into seven layers. Transport (layer 4) moves data between two endpoints. Application (layer 7) is where web protocols such as HTTP operate. The higher the layer, the more detail the balancer can inspect.

How Each Approach Routes Traffic

  • Layer 4 forwards connections based on address and port. It does not open the request, so it cannot tell a product page from a login request on the same port. It is fast and simple, and it suits any TCP or UDP service (two common transport protocols), such as a database or a game server.
  • Layer 7 reads the HTTP request, including the host name, the URL path and headers. It can send /images requests to one group of servers and /api requests to another, and it can use cookies to keep a user on the same server.

Layer 7 balancers must decode each request and often end the encrypted connection, which takes more processing than layer 4 forwarding. The trade-off is flexibility against raw speed and simplicity.

Choosing Between Layer 4 and Layer 7

Consider a company that runs a website, a chat service and a database. The database and the chat service use custom network protocols with no web content to inspect, so layer 4 balancing is a good fit. The website has an API and a static file area that should be served by different servers, which calls for layer 7 routing.

Many architectures combine both. A layer 4 balancer spreads connections across several layer 7 balancers, and each of those routes requests within the application. Teams can then add routing rules without changing the front layer. For the wider concept, see the load balancer guide.

Frequently asked questions

Is layer 7 load balancing always better?

No. Layer 7 gives more control over routing, but it requires more processing and more configuration. When traffic is simple, speed matters most or the service does not use HTTP, layer 4 is often the better choice. The right layer depends on what the balancer needs to see. The method for picking a server is covered in load balancing algorithms.

Can layer 4 load balancing read HTTP headers?

No. Layer 4 decides based only on network information such as IP addresses and ports. It forwards the connection without reading the request inside it, so it cannot route by URL or host name. Those decisions require layer 7.

Which layer suits a typical website?

A website built on HTTP is usually well served by layer 7, because it can route requests by path and host name. A small site running on one server may not need a load balancer at all. Choose the simplest option that meets your routing and security needs.

  • Two-Factor Authentication

    Two factor authentication hosting adds a second check to control panel, domain and CMS logins. Learn which methods are strongest and why 2FA helps.

  • What Is an A Record?

    What is an A record? Learn how this DNS record maps a hostname to an IPv4 address, why domains use several A records and the root and www pattern.

  • Billing and Automation Platforms

    What is WHMCS? Learn how this commercial billing and automation platform helps hosts sell plans, collect payments and create accounts automatically.