DEV Community

Aisora Lab
Aisora Lab

Posted on

Load Balancing Explained: From 1 Server to 100

Your shop just went viral, and your single server is drowning. Pages crawl, checkouts fail, and one crash takes everything offline. In the next few minutes, you will see how a load balancer turns one fragile server into a fleet that scales up for traffic and keeps running when machines fail. It is the setup behind almost every big website.

The one-server problem

Picture an online shop running on a single server. Every page view, every search and every checkout lands on that one machine. On a quiet day, that works perfectly well. Then a big sale starts and traffic jumps tenfold. The processor hits its limit, requests wait in line, and pages that loaded in a blink now take seconds. Some simply time out. Worse, that server is a single point of failure. If it crashes, or you restart it to deploy new code, the whole shop goes offline. Every customer sees an error page. You could buy a bigger machine. That is called scaling up, or vertical scaling. It helps for a while, but big machines get expensive fast, and there is always a ceiling.

Adding a load balancer

The other option is scaling out: run several copies of your app and put a load balancer in front of them. Users no longer talk to a server directly. They talk to the load balancer. The load balancer has one public address. Behind it sit three identical servers, all running the same code. Every incoming request is forwarded to one of them. Because the servers are identical, any of them can answer any request. Need more capacity for the sale? Add a fourth server, and the load balancer starts using it. Common choices are Nginx and HAProxy, or the managed load balancers that cloud providers offer. Under the hood, they all solve the same problem: deciding where each request goes.

Choosing a server

The simplest strategy is round robin. The first request goes to server one, the next to server two, then server three, and then it starts over from the top. Round robin assumes every request costs the same. But exporting a big report might take ten seconds, while a page view takes a few milliseconds. One server can quietly pile up slow work. Least connections fixes that. The load balancer tracks how many requests each server is handling right now, and sends new work to the least busy one. A third option hashes something about the client, such as its IP address, so the same user usually lands on the same server. That helps when servers keep useful data in memory.

Surviving failures

Load balancers also watch their servers with health checks. Every few seconds they send a small request, often to an address like slash health, and expect a quick, successful reply. Now server two crashes. Its health checks start failing, and after a few misses in a row, the load balancer marks it unhealthy and stops sending it traffic. Users barely notice. Their requests flow to the two healthy servers instead. A failure that would have taken down the whole shop is now just a little less capacity. When server two comes back and passes its checks again, it rejoins the pool automatically. The same trick lets you deploy new code one server at a time, without downtime.

Sessions at scale

There is one catch: user sessions. If server one keeps your shopping cart in its own memory, and your next request lands on server two, your cart suddenly looks empty. Sticky sessions are one workaround: the load balancer keeps sending you to the same server. It works, but if that server dies, your session goes down with it. The more robust fix is to keep servers stateless. Session data moves to a shared store, often Redis, that every server can read. Now any server can handle any user. And the load balancer itself? Production setups run at least two, so one can take over if the other fails. That way, no single machine can take the whole shop down.

Recap

  • Scaling out beats one ever-bigger server
  • A load balancer spreads requests across identical servers
  • Round robin, least connections and hashing pick the server
  • Health checks send traffic around failed servers
  • Stateless servers and a shared session store make it all work

Your turn

Round robin or least connections: which does your team use, and why? Tell me in the comments.


Aisora Lab explains one system design idea a day in a 60-second video, plus a deep dive every evening. Find it as @aisoralab on YouTube, Facebook and Instagram.

This article was written with AI assistance from the script of the video above, then fact-checked and reviewed before publishing.

Top comments (0)