Skip to content

What a load balancer does

A load balancer receives requests at one address and passes each one to one of several servers behind it. AWS describes its Elastic Load Balancing service in the same terms: it distributes incoming traffic across several targets, monitors their health, and routes traffic only to the healthy ones. Three jobs follow.

  • Spread the work. More servers can serve more requests only if requests reach all of them.
  • Hide failures. Clients know one address. A server that fails stops receiving requests, and nobody outside has to be told.
  • Allow change. AWS notes that targets can be added and removed without disrupting the flow of requests. That is what lets a team deploy a new version, or add capacity, while traffic keeps arriving.

The first job is more than “use every server”. The lesson on queueing compared ways of queueing the same capacity. At 80% utilisation, with servers that take 20 ms, one server takes 100 ms to answer, four servers sharing one queue take 34.9 ms, and four servers with a queue each behave like four single servers, at 100 ms each. A load balancer puts each request on one server’s own line, so it starts at the bad end of that comparison and tries to get as close as it can to the good end. How close it gets depends on what it knows when it chooses, and the next lessons are about that.

Mental model. A load balancer places each request on one server’s own queue, and cannot take it back. The quality of balancing is how well that one placement guesses which line will be shortest, using only what the balancer can see.

The numbers in “layer 4” and “layer 7” are layers of the OSI model. AWS’s Network Load Balancer works at the fourth, and its Application Load Balancer at the seventh, the application layer. The difference is what each one looks at before it chooses.

A layer 4 balancer decides from the connection. For TCP traffic, AWS’s Network Load Balancer picks a target with a hash of the protocol, the source address and port, the destination address and port, and the TCP sequence number, and sends each TCP connection to one target for as long as the connection lasts. It handles TCP and UDP traffic, and it does not need to understand what is inside. The unit it places is the connection.

A layer 7 balancer reads the request. AWS’s Application Load Balancer checks its listener rules in priority order against the content of the traffic. A rule can match the URL path, the host in the HTTP header, other HTTP headers, the method, query parameters or the source address, and its action can forward the request to a group of targets, redirect it, or return a response of its own. The balancer can also decrypt TLS using certificates, so that the servers behind it do not have to. The unit it places is the request.

The unit matters because of what the lesson on HTTP versions found: a client reuses one connection for many requests, and HTTP/2 puts many requests on it at once. A layer 4 balancer places the connection once, and every request on it goes to the same server. Take two clients, one sending 300 requests a second on one connection and the other sending 10 on another. If the balancer puts both connections on the same server, that server receives 310 requests a second and the others receive none, and nothing can move the requests afterwards. A layer 7 balancer places each request, so the same traffic is spread over all the servers.

Layer 4 Layer 7
Decides from Protocol, addresses and ports The request: URL path, host, headers, method
Places A connection A request
Can send /images and /api to different groups No Yes
Needs to understand the application protocol No Yes

Reading a request is more work than reading the addresses of a connection, so layer 7 is likely to cost more per request. How much depends on the product and the traffic, and the lab does not model it.

Balancing happens at several levels. Between regions, it can start with DNS. Amazon Route 53 chooses its answer to a DNS query by a routing policy: by latency, for resources in several regions; by location; by weight, to send traffic in proportions that you set; or by failover, for an active-passive pair. This is global load balancing, and it is introduced here and taken up in the multi-region module. The choice is made when the name is looked up, so the time to live of the record bounds how quickly it can change, as the lesson on DNS found.

The balancer is also a set of machines that something must balance. Google’s Maglev, a software network load balancer, is spread over many commodity servers: routers distribute packets evenly to the Maglev machines, and each machine then spreads them to the service’s endpoints. The question “what balances the balancer?” has an answer, and the next section shows why it has to.

Eight statements. Which layer does each one need?

Challenge

Layer 4 or layer 7?

A layer 4 balancer decides from the connection: the protocol, the addresses and the ports. A layer 7 balancer reads the request itself. Put each requirement under the layer that it needs.

Sorting needs JavaScript.

Categories

Layer 4
Decides from the connection, and places connections.
Layer 7
Reads the request, and places requests.
  1. Line 1 of 8Spread DNS queries, which arrive as UDP datagrams, over a group of servers.
  2. Line 2 of 8Balance connections to a service that speaks a binary protocol of its own over TCP, which the balancer knows nothing about.
  3. Line 3 of 8Choose the server from the protocol, the source and destination addresses and ports, and keep the connection on that server until it closes.
  4. Line 4 of 8Every request on one long-lived connection goes to the same server, however many the connection carries.
  5. Line 5 of 8Send requests whose path starts with /images to one group of servers and those that start with /api to another.
  6. Line 6 of 8Serve two domains from one balancer by looking at the host named in each request.
  7. Line 7 of 8Spread the requests of one HTTP/2 connection over several servers.
  8. Line 8 of 8Answer some requests with a redirect, or with a fixed response, without involving any server.

Check your answers at least once before looking at the answers.

What happens if the balancer is one machine?

Section titled “What happens if the balancer is one machine?”

Suppose four servers are together available 99.99% of the time, which allows 52.6 minutes of downtime a year. They sit behind one balancer that is available 99.9% of the time. A request needs both, so the two multiply: 0.999 × 0.9999 = 0.9989, an availability of 99.89%. The downtime is now 0.11% of the year, about 9.6 hours. The servers account for less than an hour of it.

The balancer is the weakest part, and adding servers does nothing about it. That is the arithmetic of parts in series, as in the availability calculator of the module on core properties, applied to the part that everything passes through. Real balancers are therefore run as several machines. AWS creates a load balancer node in each availability zone that you enable, and Maglev is a pool behind routers. The lab treats the balancer as a single copy that never fails, which is a simplification, not advice.

  • A load balancer is one address in front of several servers. It spreads the work, hides failures, and lets servers be added and removed without clients noticing.
  • It places each request on one server’s own queue. At 80% utilisation that is the difference between 100 ms for separate queues and 34.9 ms for one shared queue, and the algorithm decides how much of the gap is closed.
  • A layer 4 balancer decides from protocol, addresses and ports and places connections. A layer 7 balancer reads the request, can route by path, host or header, and places requests.
  • Placing connections can leave a busy connection and an idle one on the same server. Placing requests cannot.
  • Balancing between regions can start with DNS, as in Route 53’s latency, weighted and failover policies.
  • A single balancer in front of servers at 99.99% and itself at 99.9% gives 99.89%, about 9.6 hours of downtime a year, so real balancers are run as several machines.

Next: balancing algorithms, and how much of the gap each one closes.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.