Load balancing: layers, algorithms and trade-offs
A load balancer spreads requests over a group of servers so that no one server is overloaded and one failure does not take the service down. A design answer picks where the balancer sits (layer 4 or layer 7) and how it chooses a server (round robin, least connections or a hash), then says what happens when a server fails. The four checked problems below give practice with the arithmetic.
Your first hour with George Li (60 minutes by video, $250) ends with a written plan within one working day.
Book your first hour ($250)What a load balancer buys you
Without a balancer, users must know the address of a particular server, and that server is both the only door and the only worker. With one, users know a single address, and behind it you can add servers when load grows, remove them when it shrinks, take one out to update it while the rest keep serving, and survive the loss of any one machine. Spreading load also helps with a flood of requests, because it is harder to overwhelm a pool than a single machine. The balancer itself must not become the weak point, so serious designs run more than one.
Where the balancer looks: DNS, layer 4 and layer 7
The names come from the layered model of networking, in which layer 4 is the transport layer, where connections are carried, and layer 7 is the application layer, where requests such as a web page fetch live.
| Kind | What it can see | What it can do | Cost |
|---|---|---|---|
| DNS | Only the name lookup before any request is sent | Answer different clients with different server addresses, for example by region | Clients and resolvers cache answers, so changes spread slowly |
| Layer 4 | Addresses, ports and the protocol of each connection | Send all of one client’s connections to the same server; very fast | Cannot route by URL, cookie or user |
| Layer 7 | The whole request: URL, headers and cookies | Route video to one pool and static files to another; keep one user on one server | More work per request, and it must read the traffic, so it usually ends the encrypted connection itself |
Reading encrypted traffic means the layer 7 balancer holds the certificates and the decrypted requests, so it becomes a concentrated place to protect. In exchange, the servers behind it do not each have to do the decryption. The usual pattern at scale is a simple layer 4 balancer in front of a pool of layer 7 balancers, so that no single smart balancer carries everything.
How the balancer chooses a server
The nginx documentation names the methods most designs start from: round robin, which is the default, least-connected and ip-hash. Algorithms split into those that ignore the current state of the servers and those that read it.
| Algorithm | How it chooses | Works best when | Weakness |
|---|---|---|---|
| Round robin | Each request goes to the next server in a fixed order | Servers are alike and requests cost about the same | Ignores how busy each server is |
| Weighted round robin | Like round robin, but a larger server appears more often in the order | Servers differ in size | Weights are set by hand and ignore current load |
| Random | Each request goes to a random server | Many requests, and several balancers that should not need to coordinate | Evens out only over many requests |
| Hash of the client address or the URL | The same key always reaches the same server | You want a user on one server, or content on one server for caching | Adding a server moves many keys (Problem 2) |
| Least connections | The server with the fewest open connections gets the next request | Requests vary a lot in cost | The balancer must track state |
| Power of two choices | Pick two servers at random and send the request to the less busy | A pool of several balancers that would all pile onto the same least-busy server | Slightly less optimal than a perfect global view |
The nginx page shows weights concretely: with one server weighted 3 and two others at 1, every 5 new requests go 3, 1 and 1 across the three servers, which is what Problem 1 counts. The last row exists because of a problem that appears with several balancers: if each one sends every request to the server that looks least busy, they all pick the same one at the same moment and overload it. Picking the best of two random choices spreads that out. Google’s chapter on load balancing in the datacenter treats this territory in depth, including limiting how many backends each client connects to.
Choosing in one sentence
- Similar servers, similar requests, one balancer: round robin.
- Different server sizes: weighted round robin.
- Requests that vary a lot in cost: least connections.
- Several balancers at high volume: random or the best of two choices.
- Cache or session on the server: a hash, ideally one that moves few keys when the pool changes.
Health checks, capacity and failure
A balancer sends traffic only to servers that pass a health check, so a crashed server is removed from rotation automatically. Capacity planning has to assume that happens. If you need 10 servers’ worth of capacity and run exactly 10, then losing one pushes the other nine over their limit, and the failure can cascade. So size for the load at the utilization you want, then add one more server so that any one can be out. Problem 3 does this arithmetic. Say aloud what the utilization cap is, because running servers at their limit leaves no room for a burst.
Three other points are often asked about: sessions, where a user must keep reaching the same server or the session data must live somewhere every server can read; the balancer’s own redundancy; and rolling updates, where you take one server out, update it, check it and put it back before the next. Each is a sentence or two in an answer.
A note on who needs this. Our system design for interns guide found no system design round named in our program records, and a first internship interview is mostly coding, your projects and behavioral questions. This page is for students who are curious, who have a design question in a course or a project, or who are looking ahead to a new-graduate loop. If a recruiter says there will be a design round, ask what scale it expects and prepare for that.
Practice problems
These problems are our own, written for this site. Every answer is recomputed when the site is built, and a wrong answer would stop the build. Try each before you open the steps. Read how the problems are checked.
Problem 1. A balancer deals requests in the repeating order A, A, A, B, C, because server A has weight 3 and servers B and C have weight 1. The first request goes to A. Of the first 23 requests, how many go to server A? Give an integer.
Show the answer and the working
Answer: 15
- One pass of the order covers 5 requests and sends 3 of them to A.
- 23 = 4 x 5 + 3, so there are 4 full passes and 3 requests left over.
- Four passes send 4 x 3 = 12 requests to A, and the 3 left over are the first three positions, A, A and A, so 3 more go to A.
- In all 12 + 3 = 15 of the 23 requests go to A.
Problem 2. A pool of 4 servers is chosen by key mod 4. A fifth server joins and the pool now chooses by key mod 5. Of the keys 1 to 60, how many are sent to a different server than before? Give an integer.
Show the answer and the working
Answer: 48
- A key stays put when key mod 4 equals key mod 5.
- That happens when key mod 20 is 0, 1, 2 or 3, because for those remainders both moduli give the same value.
- In 1 to 60 the keys with remainder 1, 2 or 3 mod 20 are 9 keys (1, 2, 3, 21, 22, 23, 41, 42, 43) and the keys with remainder 0 are 20, 40 and 60, so 12 keys stay.
- 60 - 12 = 48 keys move, which is 80 percent. This is why simple remainder hashing is a poor way to add servers to a cache.
Problem 3. A service must carry 900 requests per second. Each server can handle 120 requests per second, and the team wants no server above 75 percent of that. The target must also hold with any one server down. What is the fewest servers to run? Give an integer.
Show the answer and the working
Answer: 11
- At 75 percent of 120, a server should carry at most 90 requests per second.
- 900 / 90 = 10, so 10 servers must be standing.
- One may be down, so run one more than the standing count: 10 + 1 = 11.
Problem 4. Four servers hold 12, 7, 9 and 7 open connections. A least-connections balancer sends the next request to the server with the fewest open connections, and when two servers tie it picks the one listed first. Servers are numbered 1 to 4 in that order. Which server number gets the request? Give an integer.
Show the answer and the working
Answer: 2
- The smallest count is 7.
- Servers 2 and 4 both hold 7 open connections.
- The tie goes to the one listed first, which is server 2.
Questions
What is the difference between a layer 4 and a layer 7 load balancer?
A layer 4 balancer decides from the connection details, such as addresses and ports, without reading the request. A layer 7 balancer reads the request itself, such as the URL or a cookie, so it can route by content, at the cost of more work per request.
Which load balancing algorithm should I name first?
Round robin when servers and requests are similar, weighted round robin when servers differ, and least connections when requests vary in cost. nginx’s documentation describes all three.
Is the load balancer a single point of failure?
It can be, and a good answer says so and adds a second balancer or a managed service that is itself redundant.
How a session helps
In a session George can work a small design question with you aloud, ask where each of your numbers came from and mark where a failure case was left unsaid. You say each design in your own words; nothing here is written for you to recite. See the packages page for the sessions and book a session when you have a date to work toward.
Want help with your own applications? See internship interview coaching, a career coach for college internships, or resume review for internships.