System Design Academy

Saving…

Lesson 6 of 8 in Module 20 of 54 lessons complete

Load-Balancing Algorithms & Health Checks

Module 2, lesson 6Concept10 min

Definition

Load-balancing algorithm

In plain English
A load-balancing algorithm is the rule a load balancer uses to choose the server for the next request.
Think of it like
It is like the rule a host uses to pick the next table.

Health check

In plain English
A health check is a regular test request a load balancer sends to see whether a server is working.
Think of it like
It is like a teacher taking the register before handing out work.

Why it exists

One podcast conversion finishes in two seconds; another takes ten minutes. Equal request counts leave some servers busy while others sit idle.

A broken server also needs to stop receiving work. A routing rule and regular checks solve different parts of this problem.

How it works

Round robin

In plain English
Round robin sends requests to servers in turn, one after another.
Think of it like
Like dealing cards around a table.

Least connections

In plain English
Least connections sends each request to the server currently handling the fewest.
Think of it like
Like joining the shortest checkout queue.

Weighted balancing

In plain English
Weighted balancing sends more requests to stronger servers.
Think of it like
Like giving the fastest chef more orders.
RuleHow it picksGood whenWeak when
Round robinTake turnsRequests are similarLong requests pile up
Weighted round robinTake turns in proportion to configured weightsMachines have measured capacity differencesWeights become outdated
Least connectionsChoose fewest active connectionsOne busy connection represents one long requestConnections carry very different amounts of work
Least response timePrefer the lowest measured response timeRecent times reflect current loadMeasurements are noisy
Hash-basedTurn a user identifier into a stable server choiceA user should return to the same serverA few busy users overload one server
  1. Ask regularly

    Request /healthz every five seconds, with a short response deadline.

  2. Remove a failing copy

    After three failures in a row, stop sending new work to it.

  3. Restore cautiously

    After two successful checks, return the server to service.

The load balancer checks Server 2 three times without a reply, removes it, and sends user requests to servers 1 and 3.

Health checks remove an unresponsive server from new request routing.

The load balancer checks Server 2 three times without a reply, removes it, and sends user requests to servers 1 and 3.

  • Active checks send dedicated test requests to each server.
  • Passive checks observe failures in real user requests.
  • Check local ability to serve requests without turning a shared outage into removal of every server.

Examples

Spread long conversions

Converting podcast audio into another format takes two seconds to ten minutes. Three long requests landed on one server under round robin.

In this setup, each conversion holds one connection until it finishes. Least connections therefore steers new requests toward servers with fewer conversions.

  • Count active work rather than requests already completed.
  • For work continuing after a connection closes, measure unfinished jobs instead.

The insight

Choose a rule whose measurement reflects the work still running.

Trade-offs

Pros

  • Match routing to uneven work.
  • Remove broken servers automatically.

Cons

  • Collect more measurements.
  • Debug more complex routing.
  • Poor checks remove healthy application copies.

Use when

  • Use weights for measured hardware differences.
  • Use active-work measurements for long requests.

Avoid when

  • Avoid complex rules for short, similar requests.
  • Avoid checks that remove all copies during a recoverable shared outage.

Quick check

Requests take anywhere from 2 seconds to 10 minutes. Which algorithm avoids piling long jobs on one server?
Your health check also queries the shared database, which blips for 10 seconds. What is the risk?

Takeaways

  • Round robin suits even work; connection counts or weights help with uneven work.
  • Health checks stop new traffic reaching broken servers.
  • A shared dependency failure must not blindly empty the server pool.

Try the quick check above whenever you feel ready.

Previous: Load Balancers