SkillSprout
Chapter 8 · Lesson 2
Beginner
3 min read

Load Balancing Algorithms and Health Checks

Overview

A load balancer needs a rule for choosing a server (the algorithm) and a way to know which servers are alive (health checks). These two settings decide most of how a balancer behaves in practice.

Detailed Notes

What is a load balancing algorithm

The algorithm is the rule that picks which server gets the next request. A health check is the test that decides whether a server is allowed to receive requests at all.

The problem it solves

If you always pick the same server, it overloads. If you rotate blindly, one server can end up with all the slow requests while others sit idle. And without health checks, users keep being sent to dead servers. Good choices here give fair use of capacity and automatic recovery from failures.

How it works

Common algorithms

  • Round robin: servers in turn: A, B, C, A, B, C. Simple; best when servers and requests are similar.
  • Least connections: send to the server with the fewest active requests. Best when request durations vary a lot.
  • Hash-based: hash a key (such as the client IP or user ID) to choose the server, so the same key always reaches the same server. Useful when a server holds data for that key.
  • Consistent hashing: servers and keys are placed on an imaginary ring and each key goes to the next server on the ring. When a server is added or removed, only a small share of keys move. A plain "hash modulo number of servers" would reshuffle almost every key.

Health checks

  • Active: the balancer probes on a schedule, for example GET /health every 10 seconds; 3 failures means unhealthy.
  • Passive: the balancer watches real traffic and ejects a server after, say, 5 errors in a row.

Tune three settings: interval (how often), threshold (failures needed to change state) and timeout (how long to wait). Too aggressive and servers flap in and out; too lax and failures linger.

A shallow check only proves the process answers. A deep check also tests dependencies such as the database, but if a shared dependency hiccups, a deep check can remove every server at once. Keep checks lightweight and meaningful.

Connection draining: when a server is removed (deploy, scale-in), the balancer stops sending new requests but lets in-flight ones finish. Without it, every deploy cuts users off mid-request.

A simple example

Think of supermarket checkouts. Round robin sends shoppers to tills in turn, even if one has a huge trolley. Least connections sends you to the shortest line. A health check is the manager closing a till with a broken scanner.

System design example

A photo service has four servers. Most requests take 50 ms, but video exports take 30 seconds. With round robin, one server collects several exports in a row and its queue grows. Switching to least connections evens the load. The team also uses a lightweight /health endpoint and enables connection draining so deploys are invisible to users.

Summary

  • Round robin is simple; least connections handles uneven request cost.
  • Hashing keeps the same key on the same server; consistent hashing limits reshuffling when servers change.
  • Active checks probe; passive checks watch real traffic.
  • Draining lets in-flight requests finish before a server is removed.

Architecture Diagram

Algorithm choice and health checks
Architecture
Requests
Balancerleast connections

Pool

A5 active
B2 active
Cunhealthy

The balancer combines health status with an algorithm to pick a server.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course