Load Balancing
Send all your traffic to one machine and it buckles. Load balancing is the layer that fans requests across many servers — and it starts, surprisingly, at the level of the domain name you type in.
Assumes you know
After this you should be able to
- Explain the core idea in Load Balancing
- Apply Load Balancing in a representative design
Send all your traffic to one machine and it buckles. Load balancing is the layer that fans requests across many servers — and it starts, surprisingly, at the level of the domain name you type in.
The chapter follows the path a request actually takes: it starts as a domain name, gets resolved to an IP through DNS, and lands on a balancer that picks a server. So we cover the problem first, then DNS (the machinery you must understand before load balancing makes sense), then the balancing methods and algorithms themselves. Anything marked [+] is context I added; everything else is rewritten from the five teaching lessons.
One server can't hold the whole crowd#
A load balancer distributes incoming traffic across the servers in a cluster so no single machine gets overwhelmed. It's a key component of web architecture, and it exists to prevent a specific failure: if all traffic converges on one or a few machines, those machines overload — latency spikes, performance drops, and the nodes can go offline entirely. Spreading the requests avoids that mess.
The balancer also props up availability. If a server dies mid-request, the balancer simply routes future requests to the machines still running, so the service as a whole stays up. It acts as the single point of contact for every client request — clients talk to the balancer, and the balancer decides which server actually answers.
Node, server, server node, instance, and machine all mean the same thing here and are used interchangeably.
Balancers aren't only for the front door. They can sit in front of any application component — backend app servers, the database, a message queue — to spread request load evenly across the machines powering that specific component.
The balancer only trusts servers it can hear#
To route intelligently, a balancer must know which servers are actually alive — so it runs regular health checks. It keeps a real-time list of the machines that are up, and forwards requests only to those. A machine that's up is in-service; a machine that's down is out-of-service. When a server fails a health check, it's dropped from the list. When it recovers and passes again, the balancer adds it back and resumes routing to it.
Hold onto this idea. The health-check list is exactly what DNS-level load balancing lacks — and that gap is the main reason DNS balancing is the crudest of the methods you'll meet in Section 05.
DNS turns names into numbers#
Every machine on the web has a unique IP address, and DNS is the system that lets you reach one without
memorizing it. IP (Internet Protocol) delivers data packets between machines using those addresses —
something like 2001:db8:0:1234:0:567:8:1. Nobody can hold addresses like that in their head, so the
Domain Name System (DNS) maps a memorable domain name such as amazon.com to its IP address for you.
When you type a URL and hit enter, that event is called DNS querying. Resolving the name involves four kinds of servers working in a chain:
| DNS component | Its job |
|---|---|
| DNS Recursive nameserver (aka DNS Resolver) | Receives your browser's request and does the legwork of walking the chain. Usually run by your ISP. |
| Root nameserver | Points the Resolver to the right top-level domain nameserver. |
| Top-Level Domain (TLD) nameserver | Holds data for a whole extension — the .com TLD server knows about all .com
domains, .edu for .edu, and so on. |
| Authoritative nameserver | The final server in the chain; owned by the domain's owner. Returns the domain's actual IP address. |
Following a query through all four servers#
Here's the full lookup for amazon.com, step by step.
- Browser → Resolver. You hit enter; the browser sends the request to the DNS Resolver (recursive nameserver), typically managed by your ISP.
- Resolver → Root. The Resolver asks the Root nameserver where to find the right TLD server. The
Root returns the address of the
.comTLD nameserver. - Resolver → TLD. The Resolver queries the
.comTLD server, which returns the address of theamazon.comauthoritative nameserver. - Resolver → Authoritative. The Resolver queries the authoritative nameserver, which returns the
actual IP address of
amazon.com. - Resolver caches → Browser. The Resolver caches the result and hands the IP to your browser, which then requests the site directly from that IP.
Caching keeps this fast. The whole DNS infrastructure is a distributed system living in large
ISP data centers, with server clusters tuned to answer queries in milliseconds. Results are cached along the way
— and your own device caches DNS records too, with a TTL (Time To Live). Modern browsers do this
automatically to skip the lookup on repeat visits.
The first place to balance: the authoritative server#
A service the size of amazon.com runs across many data centers worldwide, and DNS load
balancing spreads traffic across them right at the authoritative nameserver. Instead of returning a
single IP, the authoritative server returns a list of IPs for the domain — and with each query it
rotates the order of that list in round-robin fashion.
The client tries the first IP on the list; the reason it gets a whole list is so it can fall back to the next IP if the first doesn't respond in time. The next client's query gets the list re-ordered with a different IP on top, and so on. Note that the IP a client hits may not be an application server directly — it's often another load balancer at the data-center level that manages that site's server clusters.
It ignores everything the balancer in Section 02 tracks: current server load, the content a server holds, request-processing time, and in-service status. Worse, because clients and Resolvers cache those IPs, a request can still be routed to a machine that's already out of service. Companies use it anyway — across multiple data centers — because it's cheap and easy to set up.
Hardware or software — pick your balancer#
There are three modes of load balancing overall: DNS (covered above), hardware-based, and software-based. The latter two are the common ways to balance traffic on large-scale services.
| Hardware load balancers | Software load balancers | |
|---|---|---|
| What they are | Dedicated high-performance physical appliances that sit in front of the app servers. | Software installable on commodity hardware and VMs; also available as LBaaS (Load Balancer as a Service) with no setup. |
| Routing inputs | Open connection count, compute utilization, and other parameters. | Far more advanced — data hosted by servers, cookies, HTTP headers, CPU/memory use, network load, plus continual health checks. |
| Cost & ops | Expensive; need maintenance, updates, and a specialist skill set. Must be over-provisioned upfront for peak traffic. | Cost-effective, flexible, easy to upgrade and provision on demand. |
| Verdict | Best raw performance — if you have a network/IT team to run them. | Preferred by dev teams because they don't need specialists to manage. e.g. HAProxy, used by GitHub, Reddit, Instagram, AWS, Tumblr, and StackOverflow. |
The trade-off in one line: hardware wins on peak performance, software wins on cost, flexibility, and how much smarter its routing decisions can be — which brings us to the algorithms.
How a balancer actually picks a server#
DNS balancers only know round robin. Software balancers add several smarter algorithms.
Round robin & weighted round robin
Round robin hands out server IPs sequentially, ignoring load, CPU, or anything else. Weighted round robin fixes the blind spot: each server gets a weight based on its compute and traffic-handling capacity, and traffic is distributed in proportion to those weights. More traffic flows to the beefier machines — useful when a service spans multiple data centers of different sizes, so the larger ones absorb more.
Least connections
Routes the next request to the machine with the fewest open connections. Two flavors:
- Naive: assumes every request costs the same resources — but the machine with the fewest connections might already be pinned at high CPU, so sending more there is a bad bet.
- Resource-aware: also weighs CPU utilization and request-processing time. The ideal target has the shortest processing time, lowest CPU use, and fewest open connections.
Least connections shines when connections stay open a long time — like persistent connections in a gaming application. [+]
Random
Routes traffic to servers at random. The balancer may also group servers that look similar (by existing load, processing time, etc.) and then randomly pick among that similar set.
Hash
Hashes the source IP and/or the request URL to decide the target — and the payoff is stickiness:
Hashing the source IP guarantees a given client always lands on the same server. That server already holds the client's data in local memory, so it skips fetching session data from the cluster's shared session store — lower latency — and lets the client re-establish with the same server if the connection drops. Hashing the URL guarantees a request always hits the cache that already holds its data, avoiding cache misses and removing the need to duplicate data across every cache.
Recap#
Every load-balancing method is the same bet made with better information. Round-robin DNS decides blind; a software balancer decides with health, load, CPU, and hashing signals. The more the router knows about the servers behind it, the smarter — and stickier — its choice of where your request lands.
Further reading#
The section cites two external links. Both verified live today.