Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Load Balancers  ›  Lesson

Layer 4 Load Balancing

Layer 4 Load Balancing 12 minA layer 4 load balancer chooses a server using only transport-level information such as IP addresses and ports, which makes it fast and protocol-agnostic but blind to the request's content.

Layer 4 Load Balancing

Hop's redirect servers are all identical: any one of them can answer any short code. The load balancer in front of them does not need to understand anything about the request — it only needs to hand each new connection to some healthy server, very fast. That is exactly what a layer 4 load balancer does.

The rule

Layer 4 sees addresses and ports, never the request inside.

The "layers" come from the networking model. Layer 4 is the transport layer: TCP and UDP. At this level, a connection is described by a handful of numbers:

source IP 198.51.100.7   source port 53122
dest   IP 203.0.113.40   dest   port 443     protocol TCP

A layer 4 load balancer looks only at these, picks a back-end server, and forwards the whole connection there. It never reads the HTTP method, the URL path, headers or cookies — with HTTPS it could not read them anyway without decrypting.

What that gives you:

  • Speed and scale — little work per packet, so it handles huge numbers of connections cheaply.
  • Any protocol — works for HTTP, databases, game traffic, anything over TCP or UDP.
  • Encryption untouched — HTTPS can pass straight through to the servers.

What it cannot do:

  • route /api/ to one pool and / to another
  • choose by cookie, header or user
  • rewrite requests, add headers or cache responses

The shortcut

Ask: "Does the decision need anything inside the request?" No → layer 4 is enough, and it is the faster, simpler choice.

Hop's redirect pool at layer 4Step 1 of 4
  1. A connection arrives

    From 198.51.100.7, port 53122, to Hop's address on port 443.

A subtle trap: because layer 4 balances connections, one browser keeping a connection open for many requests sends them all to the same server. With long-lived connections, load can end up less even than the request count suggests.

Drills

Answer fast — trust the rule.

quick drill

What information does a layer 4 load balancer use to choose a server?

quick drill

Which task is impossible for a pure layer 4 load balancer?

quick drill

Why is layer 4 balancing usually faster than layer 7?

quick drill

Which traffic can a layer 4 balancer handle that an HTTP-only balancer cannot?

Remember

  • Layer 4 sees addresses and ports, never the request inside.
  • Shortcut: if the decision needs nothing inside the request, layer 4 is enough and faster.
  • Trap: it balances connections, so long-lived connections can skew the load.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhat can a layer 4 balancer see?

Hop wants to put a layer 4 load balancer in front of its redirect servers. Explain what information it uses to pick a server, what it cannot do because of that, and why it can still be a good choice for Hop's redirect traffic.