Load balancers

Meet the person standing at the street corner sending each customer to the right shop - and see what happens when a shop catches fire.

25 min read

LB
balancer
srv-1
srv-2
srv-3

Someone has to direct the crowd

Last lesson you opened a second shop. Immediately, a question appears that never existed with one shop: when a customer walks down the street, WHO decides which shop they enter?

You hire someone to stand at the corner. Every customer talks to them first, and gets pointed to a shop: 'you - shop A, you - shop B.' On the internet, that person is called a load balancer. Every big website has one, and every request you have ever sent to Google or Instagram went through one first.

The balancer gives you a superpower that's easy to miss: customers only ever know about the person at the corner - never the shops themselves. So you can add shops, close shops, repaint shops... and no customer ever needs to know. The corner person just quietly starts (or stops) sending people there.

Imagine it like this: The load balancer is the person at the street corner. Customers only know the corner - never the shops - so you can change the shops freely behind them.

Words you just learned
load balancer
- the machine that receives every request and forwards it to one of your servers

How does it choose? Three strategies

Strategy 1 - Take turns (round-robin). First customer to shop A, second to B, third to C, then back to A. Dead simple, totally fair, and it's the default everywhere. It works great when every customer wants roughly the same thing.

Strategy 2 - Least busy first. Look at the shops, count the customers inside each, send the newcomer to the emptiest one. This shines when orders are wildly different sizes - one customer buys a stamp (2 seconds), another orders a wedding cake (2 hours). With take-turns, one unlucky shop collects all the wedding cakes. Least-busy notices and self-corrects.

Strategy 3 - Same customer, same shop (sticky). Remember each customer's face and always send them to the same shop. When is this needed? Only when the workers are NOT forgetful - when a shop keeps something about you in its head. Notice something? If you followed last lesson's rule (forgetful workers, shared notebook), you never need this. Needing stickiness is usually a smell that state is hiding where it shouldn't be.

Remember these
  • Take turns: the default - fair when requests are similar
  • Least busy: wins when some requests are heavy and some are light
  • Sticky: only needed when servers secretly remember things - a smell
Words you just learned
round-robin
- taking turns: A, B, C, A, B, C...
sticky session
- always sending the same visitor to the same server

Your turn: be the balancer

Time to do the job yourself. In the widget below, YOU are the person at the corner. Send some requests with 'take turns' and watch them spread evenly. Then switch to 'least busy' and watch it favor idle servers. Finally - the important part - click a server to break it, and keep sending. Notice you never send anyone to the broken shop. How did you know it was broken? That's the next section.

Try it yourself: YOU are the load balancer. Send requests, switch strategies, and click a server to break it.
0 sent · 3/3 healthy

Taking turns: fair when every request costs the same. Notice broken servers are skipped automatically - that's the health check.

Health checks: how the corner knows a shop is on fire

In the widget, you could SEE the broken server. A real load balancer can't see - so every few seconds, it walks over and knocks on each shop's door: 'you alive?' That knock is called a health check.

A good health check doesn't just ask 'is anyone there?' - it asks 'can you actually serve customers?' A shop can have its lights on while the till is broken. In computer terms: the health endpoint should check that the server can reach its database, not just that it's switched on.

Here's the math every engineer should do once. Say the balancer knocks every 5 seconds, and pulls a shop out after 3 missed knocks in a row. Then a shop that catches fire keeps receiving customers for up to 15 seconds - and every customer sent there during that window has a bad day. Knock more often and you find fires faster, but you also risk pulling out a shop that just blinked. It's a dial, not a right answer.

Remember these
  • Health check = the balancer knocking on each server's door every few seconds
  • Good checks test 'can you serve?', not just 'are you on?'
  • Detection time = knock interval × missed-knocks threshold. Do this math
Words you just learned
health check
- an automatic 'are you okay?' probe sent to each server

The polite goodbye: connection draining

One more trick separates amateur setups from professional ones. Sometimes YOU want to remove a shop on purpose - to renovate it (deploy new code). The clumsy way: lock the doors instantly. Every customer mid-order gets thrown out. They notice. They complain.

The polite way: the corner person stops sending NEW customers to that shop, but everyone already inside gets to finish their order. Once the shop is empty, it closes for renovation. Nobody was thrown out; nobody noticed anything.

This is called connection draining, and it's why the best websites can update their code twenty times a day without you ever seeing an error. When a deploy is invisible, this is the trick making it invisible.

Remember these
  • Draining: stop NEW requests, let in-flight ones finish, then remove
  • This is why good sites deploy constantly and you never notice
Words you just learned
connection draining
- letting current requests finish before taking a server out
deploy
- shipping a new version of the code to the servers

Wait - who watches the watcher?

Sharp readers spot the loose thread: the person at the corner is now a single point of failure! If THEY faint, nobody gets directed anywhere, and all three healthy shops sit empty.

The industry's answer is refreshingly unclever: have two corner people, where the second one takes over the moment the first faints. And in practice most teams don't even manage that themselves - every cloud provider rents you a load balancer that is already a redundant team behind the scenes. In an interview, one sentence covers it: 'the balancer itself is redundant, typically managed by the cloud.' Then move on to the interesting parts.

And that's module 1. You now know: the four parts inside a server, the two ways to scale, the golden rule of statelessness, and the machine that makes many servers look like one website. Take the quiz below - module 2 tackles the shared notebook itself: databases, and what happens when THEY get crowded.

Remember these
  • The balancer is redundant too - usually two, or a managed cloud service
  • Module 1 done: four resources → two scaling moves → stateless rule → balancer

Check your understanding

0/5 answered

Q1Some requests take 2ms and some take 2 seconds. Which strategy spreads the work best?

Q2Health checks knock every 5 seconds, and a server is removed after 3 missed knocks. How long can a dead server keep receiving customers?

Q3What does connection draining do during a deploy?

Q4Needing sticky sessions (same visitor → same server) is usually a sign of what?

Q5The load balancer itself could fail. What's the standard answer?

Vertical vs horizontal scaling Next: Databases under load - coming soon