SproutStackSproutStack home

Load Balancing Intro

6 min readBeginner-friendlyFree to read

Before this, it helps to know: DNS, HTTP, TCP in 10 Minutes.

Read a little, play a little. No scary maths, and no rush.

Share:

One server, one bad day

A single server handling everything is a single point of failure: one crash, one deploy gone wrong, one traffic spike — and the whole product is dark. A load balancer sits in front of two or more identical servers and spreads incoming requests across them. One box dies? The balancer notices and sends traffic to the survivors. Users never know anything happened.

removedUsersLoad balancerhealth checksServer AServer BServer Cfailed its checkSession storeshared, e.g. Redis
Any server can serve any user because sessions live in a shared store. When Server C fails its health check, the balancer stops sending it traffic.

Two flavors: L4 and L7

  • L4 (transport): routes by IP and port without looking inside. Fast and dumb — perfect for raw throughput.
  • L7 (application): understands HTTP — routes /images/* to one pool, /api/* to another, terminates TLS, injects headers. Smarter, slightly slower.

Beginners can just say "a Layer-7 load balancer" for web apps and be right.

How it picks: the algorithms

  • Round-robin: 1, 2, 3, 1, 2, 3… beautifully fair when servers are equal.
  • Least connections: send to whoever is least busy — better when requests vary in cost.
  • IP hash: same user always lands on the same server. Useful, but dangerous (see below).

Health checks and the sticky trap

The balancer health-checks every server every few seconds and quietly drops the dead from rotation. And a warning about sticky sessions (pinning a user to one server so their session data is there): it feels convenient until that server gets hot, or dies and takes everyone's sessions with it. Prefer stateless servers + a shared store (Redis) so any server can serve any user.

A worked example: deploying with zero downtime

Three servers sit behind a load balancer, and a new version needs to go out during business hours.

  1. Take server A out of rotation. The balancer stops sending it new requests and lets requests already in progress finish, a step called connection draining.
  2. Deploy the new version to A and wait for its health check to pass.
  3. Put A back in rotation and repeat for B, then C.

At every moment two servers are serving, so users notice nothing. This rolling deploy is only possible because a balancer can add and remove servers without anyone outside knowing. If the new version fails its health check on A, you stop there and roll back, having affected nobody.

Where load balancers live

  • Managed cloud balancers from your hosting provider are the usual choice: you configure them, the provider runs them.
  • Software balancers such as nginx or HAProxy run on your own machines.
  • DNS can spread traffic coarsely by returning different addresses, often to send users to the nearest region.

One catch: the balancer itself must not become the new single point of failure. Managed balancers are redundant by design; if you run your own, run at least two with a way to fail over between them.

Designing the health check

A health check that only asks "is the process alive?" misses a server that is up but broken. One that checks every dependency deeply can do the opposite: a brief database hiccup makes every server fail its check at once, and the balancer removes all of them. A good check confirms the server can do its core job cheaply and quickly, without making the whole fleet depend on one shared component being perfect.

Remember this

  • LB + 2 replicas is the minimum credible "high availability" answer.
  • Name an algorithm and say why; warn about sticky sessions unprompted.

Check your understanding

5 questions · first-try answers earn the most XP

1. Why does the chapter warn against sticky sessions even though they simplify session handling?
2. What is the difference between an L4 and an L7 load balancer?
3. When is least-connections a better algorithm than round-robin?
4. What does a health check do for a load balancer?
5. What is the minimum arrangement the chapter calls credible high availability?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

Practice this in an incident

Try a design drill

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.