Skip to content
HLD Learn/Three-Tier Architecture & Scaling Up
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Load Balancers: L4 vs L7

7 min read

You'll learn to

  • -Explain what a load balancer does and why it is needed the moment there is a second server
  • -Choose between round robin, weighted, least-connections and IP-hash, with a reason
  • -Explain Layer 4 versus Layer 7 load balancing with the marshal picture
  • -Say how health checks keep a failing server out of rotation, and why the balancer itself needs redundancy

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

Horizontal scaling gives you several interchangeable servers, but it creates a new question: when a request arrives, which server gets it? Clients cannot be expected to know, and you do not want them to. The component that answers is the load balancer. It becomes the new front door of your logic tier: the single address clients talk to, which quietly fans their requests out to the servers behind it.

An Analogy: One Queue, Many Counters

Picture an airport check-in hall. Instead of a separate queue at each counter, there is a single queue and a marshal at the front. Each time a counter frees up, the marshal sends the next passenger to it. Passengers never choose a counter and never need to know how many are open. If a counter's agent goes home sick, the marshal simply stops sending anyone there.

  • -The passengers are requests, and the counters are your servers.
  • -The marshal is the load balancer. Everyone talks to the marshal, and only the marshal knows about the counters.
  • -Deciding who goes where is the balancing algorithm.
  • -Noticing that a counter has closed is the health check: the balancer regularly asks each server whether it is fine, and stops using any that fail to answer.

The two pictures below show the problem and the fix:

1One server: a single point of failure and a hard ceiling
Client
The Only Server

Every request depends on this one machine. It crashes, everyone is down; it saturates, everyone slows down.

2A load balancer in front of multiple servers
Client
Load Balancer
Server A
Server B

The load balancer distributes requests and stops sending traffic to any server that fails its health check.

What a Load Balancer Gives You

  • -Spread: no single server takes all the load, so each stays in its comfortable range.
  • -Resilience: a server that fails its health check is taken out of rotation automatically, and users only notice if too many fail at once.
  • -One address: clients see one name no matter how many servers exist behind it, so you can add and remove servers without telling anyone.
  • -Painless deploys: take one server out, update it, put it back, then do the next (a rolling deploy), with no downtime.
  • -Optional extras: many balancers also end the TLS encryption on behalf of the servers, and can compress or cache responses.

How It Chooses: The Algorithms

The simplest rule is round robin: send request 1 to server A, request 2 to B, request 3 to C, request 4 to A again, and so on. It is fair in count, but count is not the same as work, and a small example shows why.

Three servers, and six requests arrive. Request 1 is a slow report that takes 5 seconds; the other five are quick lookups that take 50 milliseconds each. Round robin sends requests 1 and 4 to server A, so request 4 now waits behind a 5-second job even though B and C are idle. A smarter rule, least connections, always picks the server with the fewest requests in progress right now. A is busy with the report, so request 4 goes to B or C and finishes in 50 ms.

  • -Round robin: simple and fair in count. Good when requests are all similar and servers are identical.
  • -Weighted round robin: stronger servers get a bigger share (a server with weight 3 gets three requests for every one that a weight-1 server gets). Good for mixed hardware.
  • -Least connections: send to whoever is least busy. Good when request times vary a lot, as in the example.
  • -IP hash: the same client address always reaches the same server. It gives sticky sessions without a cookie, but it breaks down when thousands of users share one address, and it brings back the state problem the next chapter warns about.

Play through the simulation below to watch round robin make that decision request by request, and notice that it hands out requests in turn without looking at how busy each server is.

Round Robin, Load Balancing

The balancer walks the server list in fixed order - each request goes to the next server.

Clients

Client A

10.0.0.42

Client B

10.0.0.108

Client C

192.168.1.7

Client D

203.0.113.55

Load Balancer

Round Robin

1234

pick: web-01

Servers

web-01

routed 1

10.0.1.11:8080

web-02

routed 0

10.0.1.12:8080

web-03

routed 0

10.0.1.13:8080

web-04

routed 0

10.0.1.14:8080

Dispatch Order

#1 → web-01
1 / 16

Layer 4 vs Layer 7

Load balancers come in two main kinds, named after layers of the network stack. Go back to the marshal. A Layer-4 marshal only counts heads: they see that a passenger arrived (an IP address and a port) and send them to a counter, without ever looking at the ticket in their hand. It is fast, and it works for any kind of traffic, not only web traffic. A Layer-7 marshal reads the ticket: they see the HTTP path, the headers, even the body, and can say that anyone holding a refund form goes to the refunds desk.

  • -L4 (transport level): sees IP addresses and ports only. Very fast and protocol-agnostic. Use it for raw TCP or UDP traffic, such as game servers or database connections, or when you simply need speed.
  • -L7 (application level): reads the HTTP request. It can send /api/orders to one group of servers and /api/search to another, send 5% of traffic to a new version to test it (a canary), or route by a header or cookie. It costs a little extra latency and CPU for that intelligence.

Who Watches the Watcher?

There is one catch. If all traffic goes through one load balancer and that load balancer fails, nothing gets through: you have created a new single point of failure at the very front door. In practice load balancers are run in pairs or groups (managed cloud load balancers do this for you), so that if one fails, another takes its place within seconds.

In the lab's palette the load balancer is a single box on the canvas. The card below is its reference:

ComponentLoadBalancer

Picture a popular restaurant with one door but 10 chefs in the kitchen. Without a host at the front, every customer would crowd around Chef #1 while Chefs #2 through #10 stand around doing nothing. The load balancer IS that host. It stands at the entrance and says 'Table for you with Chef #3, table for you with Chef #7, table for you with Chef #1...' distributing customers evenly so no single chef gets overwhelmed. In tech terms, a load balancer sits in front of your servers and distributes incoming network requests across them. If you have 5 web servers, the load balancer makes sure each one gets roughly 20% of the traffic. If one server crashes, the load balancer detects it (via health checks) and stops sending traffic there. Users never even notice.

Examples: AWS ALB (Application Load Balancer), AWS NLB (Network Load Balancer), Nginx, HAProxy, GCP Load Balancer, Traefik

For the full set of balancing algorithms and when each one fits, the Wiki card below goes further:

From the Wiki⚖️ Load Balancing Algorithms

How a load balancer DECIDES which backend server gets each incoming request.

A hotel front desk has 5 receptionists. How do you decide which receptionist handles the next guest? Round Robin (next in line), Least Connections (whoever is currently least busy), or IP Hash (the same guest always goes to the same receptionist so they remember their preferences)?

Round Robin: Requests are distributed sequentially across servers: server1, server2, server3, server1, server2... Simple, predictable, and even - but ignores the fact that some requests take 1ms while others take 5 seconds. A slow server accumulates a pile of long-running requests while still receiving new ones.

Weighted Round Robin: Each server gets a weight. Server with weight=3 gets 3 requests for every 1 request the weight=1 server gets. Use this when servers have different hardware capacities.

Least Connections: Route to the server with the fewest active connections. Works well when request processing times vary significantly (e.g., some requests are fast API calls, others are slow image processing jobs). Requires the load balancer to track connection counts.

Least Response Time: A sophistication of Least Connections - route to the server with the lowest combination of active connections AND response time. AWS ALB's Least Outstanding Requests uses this principle.

IP Hash: server = hash(client_ip) % num_servers. Same client always hits the same server. Enables "sticky sessions" without requiring a session token. Breaks badly when clients are behind NAT (many clients sharing one IP all go to the same server).

  • -Round Robin is almost always the default and fine for homogeneous request patterns.
  • -Least Connections is better for heterogeneous workloads (mixed fast and slow requests).
  • -Sticky sessions (IP Hash) should be avoided - use server-side sessions (Redis) instead.
  • -Layer-4 LBs (TCP level) can only use IP/port - Round Robin or IP Hash.
  • -Layer-7 LBs (HTTP level) can inspect URLs/headers and route by content (A/B testing, microservice routing).
Read the full Wiki deep-dive (+1 more)

In one line: a load balancer is the marshal at the head of one queue with many counters. It spreads work (pick the algorithm to match your traffic), skips failed servers using health checks, comes in L4 (counts heads, fast) and L7 (reads the ticket, smart) flavors, and needs a backup of its own.

Interview Signal

One of your five servers behind the load balancer silently starts failing every request. What happens?

Weak Answer

"The load balancer spreads requests evenly, so only 20% of users are affected."

Strong Answer

"Only if there's no health check. Without one, that broken server keeps receiving its full share of traffic forever, and 20% of all requests fail indefinitely. The load balancer needs an active health check (hit a /health endpoint every few seconds) so it can pull the failing server out of rotation automatically, not just balance load blindly."

Check Yourself1 / 6

What can an L7 load balancer do that an L4 load balancer cannot?

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!