What Is a Load Balancer? Common Algorithms Explained

So what is a load balancer, exactly? It is a component that sits in front of two or more servers and decides which one handles each incoming request. Instead of one machine absorbing all the traffic, the work gets spread around, so no single server becomes the bottleneck and a failure on one machine does not take the whole service down.

If you have ever run a site that slowed to a crawl during a traffic spike, the problem was often not the code. It was one server doing all the work while others sat idle. A load balancer fixes that distribution problem, and the way it decides where each request goes comes down to its algorithm.

What Is a Load Balancer, Really?

A load balancer can be software, like HAProxy or NGINX, or a managed service from a cloud provider. It holds a pool of backend servers, checks their health, and forwards each request to one of them according to a rule. When a backend stops answering health checks, the load balancer stops sending it traffic until it recovers.

Load balancers work at two levels. Layer 4 balancers route based on connection information such as IP address and port, without looking inside the request. Layer 7 balancers read the HTTP request itself, so they can route by URL path, host header, or cookie. That extra visibility makes layer 7 load balancing the right choice when different paths need different backends, for example sending /api/ to one pool and static assets to another.

There is a close relationship with reverse proxies here. A reverse proxy already accepts client connections on behalf of your backends, and many reverse proxy setups grow into load balancers simply by adding more than one upstream server to the pool.

Common Load Balancer Algorithms Compared

Every algorithm is a trade-off. There is no single best choice, only the one that fits your traffic pattern and your backends.

Round Robin

Round robin sends each new request to the next server in the list, cycling back to the start when it reaches the end. Round robin load balancing works best when your backends are roughly equal in power and requests take roughly the same effort. Its weakness shows up when they are not: a slow request still counts the same as a fast one, so a weak server can end up with the same share as a strong one.

Weighted Round Robin

Weighted round robin adds a weight to each server. A server with twice the CPU or memory gets roughly twice the requests. This is the practical fix when your pool mixes instance sizes, for example after you upgraded half your fleet and have not yet replaced the rest. Set weights from real capacity, not guesses, and revisit them after hardware changes.

Least Connections

The least connections algorithm sends each new request to the server with the fewest active connections. It adapts to uneven request costs in a way round robin cannot, because a server stuck on long-running requests naturally accumulates connections and stops receiving new ones. It works best for long-lived sessions such as WebSockets or database-backed apps where request durations vary a lot.

Least Response Time

Least response time combines active connection count with how quickly each server has been responding. A server that answers slowly gets less traffic even if its connection count looks fine. This algorithm needs real measurement data, so it only works on balancers that track response times, and it can oscillate if the measurements are noisy.

IP Hash

IP hash runs the client IP through a hash function and always sends that client to the same backend. That gives you session stickiness without cookies, which matters for apps that keep session state in local memory. The downside is uneven distribution: a few heavy users behind one NAT address can overload a single server, and removing a server from the pool reshuffles many clients.

Least Bandwidth and Random

Some balancers offer least bandwidth, which favors the server currently moving the least data, useful for media or download-heavy workloads. Random selection, sometimes with two random choices compared, is surprisingly effective at scale and avoids the synchronization problems that can hit pure round robin. For small pools, though, randomness just adds unpredictability without much benefit.

Health Checks: The Part That Actually Keeps You Online

An algorithm means nothing if it keeps sending traffic to a dead server. Health checks are regular probes, an HTTP request to a health endpoint or a TCP connection attempt, that decide whether a backend stays in the pool. Configure them thoughtfully: too aggressive and a brief hiccup pulls a healthy server out; too relaxed and users hit a broken backend for minutes.

A good health endpoint checks what matters, not just that the process is alive. For a web app, that means the app can reach its database and serve a real response, not just return a static 200. Pair health checks with sensible timeouts, and test failure behavior on purpose: stop one backend during a quiet hour and watch whether traffic shifts cleanly. This is also where connection tuning matters, since a balancer that cannot handle enough concurrent connections becomes the bottleneck itself.

Session Persistence and When It Matters

Some applications need the same user to keep reaching the same backend, because session data lives in that server’s memory. Options include cookie-based stickiness, where the balancer inserts a cookie naming the backend, and IP hash. Both have costs: stickiness breaks even distribution and makes draining a server for maintenance slower.

The cleaner long-term fix is to stop storing session state on individual servers. Move sessions to a shared store such as Redis or a database, and any backend can serve any user. Once state is shared, you can use the simplest algorithm that fits and scale the pool freely. If you run Kubernetes, the choice of ingress controller also shapes how stickiness and routing behave at the cluster edge.

How to Choose the Right Algorithm

Start from your workload, not from a feature list:

  • Equal servers, short similar requests: round robin is fine and easy to reason about.
  • Mixed server sizes: weighted round robin, with weights matched to real capacity.
  • Long or variable request times: least connections, so busy servers stop attracting new work.
  • Session state stuck on backends: IP hash or cookie stickiness, with a plan to move state to a shared store.
  • Global audience: combine a local balancer with geographic routing. Anycast DNS can steer users to the nearest region before your balancer even sees the request.

Whatever you pick, measure after you deploy. Watch per-backend request counts, response times, and error rates. If one server consistently carries far more load than the others, the algorithm is not matching your traffic, and the numbers will tell you before your users do. You can also reduce backend load outright with caching in front of your app servers, which often does more for capacity than any algorithm change.

Common Mistakes to Avoid

The most common mistake is adding a load balancer and assuming the job is done. A single balancer is itself a single point of failure unless it is deployed as a redundant pair. Another is forgetting TLS: terminating TLS at the balancer simplifies certificates but means traffic between balancer and backends may travel unencrypted unless you configure backend TLS too. Finally, do not copy algorithm settings from a tutorial without testing them against your own traffic pattern. A setup tuned for a chat app will not behave the same under an e-commerce flash sale.

A load balancer earns its keep when traffic spikes, a server fails, or you need to deploy without downtime. Pick the algorithm that matches your workload, back it with real health checks, and keep sessions out of server memory. The rest is tuning, and tuning is much easier when the foundation is right.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.