Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Load Balancers  ›  Lesson

Layer 7 Load Balancing

Layer 7 Load Balancing 15 minA layer 7 load balancer reads each application request, such as its HTTP path, headers and cookies, so it can route by content, at the cost of more work per request and handling the encryption itself.

Layer 7 Load Balancing

Hop has grown three kinds of back end: redirect servers for short codes, an API service under /api/, and an admin dashboard under /admin/. All three arrive at the same address on port 443. A layer 4 balancer sees identical-looking connections and cannot tell them apart. To route by what is being asked, the balancer must read the request.

The rule

Layer 7 reads the request, so it can route by what is asked.

Layer 7 is the application layer — HTTP for web traffic. A layer 7 load balancer parses each request and can decide using anything in it:

  • the path: /api/… to the API pool, /admin/… to the dashboard
  • the host name: hop.example vs docs.hop.example
  • headers and cookies: a beta-tester cookie goes to the new version
  • the method: send writes to one pool and reads to another

Because it understands the request, it can also do more than choose:

  • terminate HTTPS and manage certificates in one place
  • add or remove headers, rewrite paths, compress, cache
  • retry a failed request on another server, and balance each request rather than each connection

To read an HTTPS request, the balancer must decrypt it — it terminates TLS. That costs CPU per request and makes the balancer the holder of your certificates.

Layer 4 Layer 7
Sees IPs, ports, protocol Paths, hosts, headers, cookies
Balances Connections Individual requests
Speed Faster More work per request
Routing by content No Yes

The shortcut

Write the routing rule in plain words. If the rule mentions a path, host, header or cookie, you need layer 7. If it mentions only "any healthy server", layer 4 will do.

Hop's routing rulesStep 1 of 4
  1. Write the rules

    "/api/ → API pool. /admin/ → dashboard. Anything else → redirect pool."

The trap is paying layer 7 costs where no rule needs content. Many designs use both: a fast layer 4 tier in front, spreading connections over a few layer 7 balancers that do the smart routing.

Drills

Answer fast — trust the rule.

quick drill

Which routing rule needs a layer 7 load balancer?

quick drill

What must a layer 7 balancer do to route HTTPS requests by path?

quick drill

A layer 7 balancer balances at what granularity?

quick drill

What is a real cost of layer 7 compared with layer 4?

Remember

  • Layer 7 reads the request, so it can route by what is asked.
  • Shortcut: if the rule mentions a path, host, header or cookie, you need layer 7.
  • Trap: paying layer 7 costs where "any healthy server" would do.

Practice — level up

0/1 solved

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

ExplainRoute Hop's traffic at layer 7

Hop now has three kinds of back end: redirect servers, an API service at /api/ and an admin dashboard at /admin/. Explain why a layer 4 balancer cannot route this traffic, how a layer 7 balancer would, and what extra work and cost that brings.