CSE Roadmap

Core tier · about 3 hours · 9 parts

Part 3 of 9 · 3 min read

Layer 4 and layer 7

Two levels at which a load balancer can look at traffic, and what each one can and cannot do.

Think of a post room. One worker sorts parcels by the address on the outside and never opens them. Another worker opens every parcel and reads the letter inside. The first is fast. The second can do smarter things.

Network traffic is described in layers. Only two matter here.

Layer 4 and layer 7

Layer 4 is the transport layer, where TCP works: connections and bytes. Layer 7 is the application layer, where HTTP works: requests, paths, headers and cookies. The numbers come from a standard seven-layer model of networks.

Example: a TCP connection from one address to another is a layer 4 fact. The path /orders in a request is a layer 7 fact.

A layer 4 load balancer works at the TCP level. When a client opens a connection, it picks a server and passes the packets of that connection back and forth. It never reads the HTTP inside. The book states that a connection is identified by its source and destination addresses and ports, and that the LB usually runs a hash on them to choose the server.

Hash function

A hash function turns any input into a number in a fixed range. The same input always gives the same number.

Example: a hash of the text "10.0.0.7:5501" might give 3 on a pool of 4 servers, and every packet of that connection goes to server 3.

Layer 4 is quick, because it does not parse messages. Its weakness is that it cannot use anything higher up. It cannot send /images to one pool and /api to another. It cannot keep a user on one server by cookie. It cannot end the TLS handshake, because TLS (from page 1) needs the content.

A layer 7 load balancer is an HTTP reverse proxy. It receives the whole request, reads it, then sends it to a server. The book notes that two separate TCP connections exist: one from the client to the LB and one from the LB to the server. Since it reads each HTTP request, it can split the requests that share one connection and send them to different servers. That matters with HTTP/2, where one connection carries many requests, and some connections cost far more to serve than others.

With layer 7 the LB can:

  • route by path, host name, header or cookie;
  • end TLS, so that the servers do not each hold a certificate;
  • apply rate limits by header (page 7);
  • keep a user's requests on one server with a cookie (page 5);
  • change or add headers, and retry a failed request on another server.

The price is lower throughput than layer 4, since it does more work per request. The book says layer 4 balancers are better placed to take attacks that flood the connection setup, and that a common layout puts a layer 7 balancer behind a layer 4 one.

Internet
   |
[ Layer 4 LB ]        fast, simple, spreads connections
   |
[ Layer 7 LB ]  x3    reads HTTP, routes by path, ends TLS
   |
[ Web servers ] xN
Layer 4 Layer 7
Sees Addresses and ports Full HTTP request
Speed Very fast Slower
Routes by path or cookie No Yes
Ends TLS No Yes
Typical place Outermost Behind layer 4

Common mistake

Putting a layer 7 balancer alone at the edge of a very large system and treating it as free. It parses every request, so it can become the busiest machine. Large designs spread this work, or place a layer 4 balancer in front.

Check yourself:

  • Which layer can route /images to a different pool, and why?
  • Why can a layer 4 balancer not end TLS?