Skip to content

Health checks

A balancer sends requests only to servers it believes are up, and it cannot see a server’s state. It has to find out. There are two ways, and real balancers use either or both.

An active check is a probe. The balancer asks each server, at a fixed interval, a small question that a working server answers. AWS’s Application Load Balancer sends a request to each target every HealthCheckIntervalSeconds, takes a target out of service after UnhealthyThresholdCount failed checks in a row, and puts it back after HealthyThresholdCount successful ones in a row. HAProxy’s check works the same way, with a default interval of two seconds, three failures to take a server out and two successes to bring it back.

A passive check watches real traffic. nginx marks a server unavailable after a number of failed attempts to talk to it within a set time, max_fails within fail_timeout, by default one failure within 10 seconds, and then leaves it alone for that long. HAProxy’s observe option and Envoy’s outlier detection watch the traffic in the same spirit. A passive check costs no extra probes, and it finds out only by failing real requests.

Mental model. A health check is a guess made on a delay. The balancer always acts on what was true a few seconds ago, and what a failure costs is the traffic it sends during that delay.

Take a probe every i seconds and a threshold of f failures in a row. The server dies at some moment. The first probe that notices comes within one interval of it, so between 0 and i seconds later. Then f − 1 more probes have to fail, one interval apart. The balancer takes the server out between (f − 1) × i and f × i seconds after the failure: later than the first, and no later than the second.

Settings Interval Failures Out of rotation after
HAProxy defaults 2 s 3 More than 4 s, at most 6 s
An Application Load Balancer’s defaults 30 s 2 More than 30 s, at most 60 s

For as long as the server is in rotation, it still receives its share of the requests, and what it receives is lost. With four servers and 100 requests a second, a dead server is sent 25 a second under round robin. At the HAProxy defaults that is between 100 and 150 requests. At the Application Load Balancer defaults it is between 750 and 1,500.

Set Traffic to 100 and The last server to “fails”. The server stops answering at 30.5 seconds. The chart shows the failed requests for each second in which they arrived, and the sentences above it say what happened. With a check every 5 seconds and 3 failures, the balancer takes the server out at 45 seconds, 14.5 seconds after it failed. Round robin sends it a quarter of the traffic until then, which is about 25 requests a second, and Failed shows 385 requests, 3.1% of all. Nearly all of them were sent to the dead server, and one was already inside it when it stopped.

Requests reach a load balancer, which places each one on one of several servers. Each server has its own waiting line. Choose how the balancer decides, slow one server down, switch on sticky sessions, and make a server fail or be replaced, then compare what every algorithm does with the identical requests.

160 req/s
Servers
4
ClientsHealthy<1%Load balancerHealthy<1%ServersSaturated80%
ClientsHealthy<1%Load balancerHealthy<1%ServersSaturated80%

Each dot stands for many requests. The numbers show the real rate.

Servers saturated: 160 req/s against request capacity of 200 req/s.

Load balancer: the same requests under every algorithm

The balancer
1×
0%
Health checks
5 s
3
During the run
Utilisation
80%Saturated107% without the last server
99th percentile
311.8 msone shared queue: 142.4 ms
Failed requests
00% of the requests

Round robin, 160 requests per second on 4 servers, 80% utilised: half of the requests took under 48.3 ms, 95% under 201.1 ms and 99% under 311.8 ms. The busiest server received 25% of the requests, where an even split would give 25%. No request failed. One shared queue, which no balancer can run, would give a 99th percentile of 142.4 ms.

Round robin: what each server did
ServerShare of requestsBusyWaiting in line99th percentile
Server 125%81%2.1348.4 ms
Server 225%83%2.3315.8 ms
Server 325%82%1.7291.1 ms
Server 425%81%1.8246.3 ms
The same requests, under every algorithm
AlgorithmMeanMedian95th percentile99th percentileFailedBusiest server
Round robin · shown above68.3 ms48.3 ms201.1 ms311.8 ms025%
Random103.8 ms73.7 ms301.9 ms433.8 ms025%
Least connections40.6 ms30.1 ms113.8 ms172.0 ms025%
Power of two choices48.9 ms37.4 ms135.0 ms193.8 ms025%
Consistent hash305.4 ms74.5 ms987.0 ms997.4 ms2,977 (15.2%)39%
One shared queue (benchmark)36.2 ms28.2 ms99.0 ms142.4 ms0—

Times are for the requests that were answered within a second. The last row is a benchmark and not an algorithm: it needs the servers to take work when they are free.

0 ms
Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%16016000 ms0 ms0 ms
Load balancerHealthy<1%16016000.03 ms0.15 ms0.23 ms
ServersSaturated80%160160027 ms95 ms138 ms
Traffic
160 req/s
Successful
160 req/s
Error rate
0%
Mean latency
35 ms

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
  • Requests arrive at random at a steady average rate. Each needs an exponentially distributed time on a server, 20 ms on average. Every algorithm in the table gets the identical requests, so the differences between its rows come from the algorithm and not from the luck of the draw.
  • Each server works on one request at a time, and has a waiting line of its own that holds up to 100 requests. A request that has not been answered after 1 second has failed, and a server does not work on one that has already given up.
  • The balancer knows only how many requests each server holds. It answers instantly, has room for any traffic, and never fails itself.
  • Each figure comes from one run of 120 simulated seconds, after 10 seconds of warm-up, with a fixed random seed. The run can be repeated exactly, but it is one run: a difference of a few per cent between two rows is within the noise.

Simplifications

  • Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
  • The diagram, and the table of components under it, come from the graph engine: they treat the servers as one pool with one shared queue and never put a request on one server. That is the benchmark in the table, not what any balancing algorithm achieves.
  • A server that fails stops answering without a word, so each request sent to it waits for its timeout. A server whose program has stopped but whose machine is up can refuse connections at once instead, and a balancer can try another server. The model shows neither the refusal nor the retry.
  • A health check here is a probe that fails only when the server is down. Real probes can also fail on a server that is merely busy, and a balancer can also learn from the requests that fail.
  • Sticky sessions: 200 sessions send the sticky share of the traffic, a few of them much more than the rest (a Zipf distribution with exponent 0.8). Each is pinned to one server at random. If its server is taken out, the session is placed again and loses what that server held. Real systems have more sessions, and other distributions.
  • Consistent hash gives each server 100 places on a ring and hashes the session, so a session always reaches the same server, whatever the sticky share. It places requests by session and not by load.
  • A deploy takes out the last server. The new version starts 30 seconds later and is ready at once. The balancer puts it back at once if it was told about the deploy, and at its next passing check if it was not. Real servers take time to start and warm up.
  • Only the first server can be slow, by the factor you set, on every request. Servers of different sizes, or that slow down only under load, are not modelled.
  • There is no network time, connection set-up, TLS or keep-alive, and no difference between layer 4 and layer 7: a request is placed on a server, and nothing is said about how.
  • The one shared queue is a benchmark, not an algorithm that a balancer can run, because it needs the servers to take work when they are free. It shows how much the choice of server costs.

Parameter values are illustrative. They are not benchmarks of any real product.

Now change the checks and read the Failed column for round robin.

Check interval Failures before removal Out of rotation after Round robin fails
1 s 2 1.5 s 40
5 s 3 14.5 s 385
10 s 3 29.5 s 773
30 s 2 59.5 s 1,536

The last row has the defaults of an Application Load Balancer, and it loses 12.5% of the requests of the run. The cost is in the delay, and the delay is the interval times the number of failures, give or take one interval.

Now look at the other rows of the table, at 5 seconds and 3 failures. Random fails 403 requests, as round robin does, because it also sends a quarter of the traffic to the dead server. Least connections fails 38 and two choices 54. The reason is in how the model treats a dead server: it stops answering without a word, so each request sent to it is held until its timeout of one second. A server that holds every request it receives soon holds more than the others, and the algorithms that count requests stop choosing it, on their own, long before the check does. The benchmark fails 1, because it has no detection delay.

That is a property of the model, and the model has a limit. A server whose program has stopped, on a machine that is still up, answers a connection attempt at once with a reset, as RFC 9293 says a closed connection does, so it holds no requests and looks idle to a count. nginx can pass a request on to the next server after an error or a timeout, which is its default, though not a request that is not idempotent, such as a POST. How a real balancer handles that case depends on its retry settings, and the lab shows neither the reset nor the retry.

What happens if the others cannot carry the load?

Section titled “What happens if the others cannot carry the load?”

Set Traffic back to 160. The table says “107% without the last server”: the three servers that remain can serve 150 requests a second, and 160 arrive. The health check does its job, and it takes the dead server out at 45 seconds, but the failures do not stop. Round robin fails 2,932 requests, 14.9%, and nearly all of them, 2,340, took more than a second in the lines of servers that were overloaded. Least connections fails 2,275, 11.6%. The benchmark, which detects a failure at once, fails the most, 4,153 or 21.2%, because from the moment the server fails it sends everything to the three that remain.

No health check can fix this. It decides which servers receive traffic, and it creates no capacity. The lesson on availability put it as headroom: a system at 80% cannot lose one of four servers and keep serving everything. AWS’s design takes the other side of the trade. If every target in a group is unhealthy, the Application Load Balancer sends requests to all of them regardless of their status, which AWS calls failing open. That is a choice to try servers that may still work rather than refuse every request.

A fast check has its own price, which the lab does not show. A probe can fail on a server that is merely busy or slow, and the faster the check and the lower the threshold, the more often a good server is taken out. Then the others are more loaded, and may start failing probes too. AWS lets the unhealthy threshold go no lower than 2, so that one failed probe never removes a target. That is a vendor’s choice, not a rule, and the right setting depends on how reliable the probe is.

The lab below shows the same case, with the delay taken out. It loses one API instance, and the instances that remain cannot carry the traffic. Its balancer notices the death at once, so what you see is only what a check cannot fix. Before you press Kill an API instance, write down what you expect to happen to the queue, to the latency and to the requests. Then try a fix and run the same failure again to compare: a spare instance restores the capacity, and dropping the requests that have waited too long keeps the system serving at the capacity it has.

Kill an API instance

The shop serves 900 requests a second. Three API instances can serve 1,200 a second, so the system is comfortable. You kill one instance. The two that remain can serve 800. Watch the queue, the latency and the requests, then decide what you would have built differently and run the same failure again.

The failure lab needs JavaScript. The healthy system is shown below.

1. Break it

The healthy system is shown first. When you press the button the failure strikes, and the run follows the 30 seconds after it.

What the failure does: One of the three API instances stops. The load balancer sends all the traffic to the two that remain.

0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIBusy75%CacheHealthy1%DatabaseHealthy20%
0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIBusy75%CacheHealthy1%DatabaseHealthy20%

Each dot stands for many requests. The numbers show the real rate.

No bottleneck: every component has headroom.

0 ms

Cause and effect

Each step leads to the next. A step that is changing right now is marked.

Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%90090000 ms0 ms0 ms
Load balancerHealthy<1%90090000.03 ms0.15 ms0.23 ms
APIBusy75%900900014 ms60 ms93 ms
CacheHealthy1%81081000.14 ms0.6 ms0.92 ms
DatabaseHealthy20%25225205.5 ms24 ms37 ms
Traffic
900 req/s
Successful
900 req/s
Error rate
0%
Mean latency
24 ms

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Requests arrive at random, as a Poisson process, at a constant average rate.
  • Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
  • Service times are random with the stated mean; by default they are exponentially distributed.
  • Each node is analysed separately, with the traffic it receives treated as Poisson.
  • The system starts healthy at 900 requests a second. Three API instances can serve 1,200 a second and the database about 1,250 operations a second. Nine in ten requests read through the cache, which answers four in five of those reads; the rest, and every write, go to the database.
  • A failure strikes at one instant and nothing repairs it during the run: no restart, no autoscaling, no operator. The load balancer notices a dead instance at once.
  • The instances of a component are identical, and each request is independent of the others.

Simplifications

  • Nothing brings the instance back during the run: no restart and no autoscaling, which in practice replace an instance after a delay. You see the failure at its full length.
  • The load balancer notices the dead instance at once. In practice there is a window in which it still sends requests to it.
  • A retry is a fresh, independent attempt with the same chance of failing as the last one. How long a real client library waits between attempts (backoff, jitter), and any retry budget or circuit breaker, are not simulated: attempts are counted, not scheduled.
  • A client gives up when its first timeout runs out, so the latency shown stops at what a client would wait: the timeout of the first hop, times its attempts. After that the request counts as a failure.
  • The cache's hit ratio is constant. Warming, stampedes and hot keys have their own labs.
  • Nothing here models garbage collection, connection pools on the client, DNS, TLS handshakes, CPU throttling, noisy neighbours or cold caches. A real system fails in more ways, and in messier ones, than this model does.
  • Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
  • Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
  • A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
  • A request calls its dependencies one after another. Parallel fan-out is not modelled.
  • When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
  • Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
  • A caller’s timeout counts the mean time its dependencies take, not their full distribution.
  • Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.

Parameter values are illustrative. They are not benchmarks of any real product.

  • An active check probes each server at an interval, and a passive check watches real requests. A server is taken out after a number of failures in a row.
  • The time to take a server out is more than (failures − 1) × interval and at most failures × interval. It is 4 to 6 seconds at the HAProxy defaults and 30 to 60 seconds at an Application Load Balancer’s.
  • Until then, round robin sends the dead server its share. At 100 requests a second on four servers, 25 a second, which is 385 failed requests at 5 seconds and 3 failures, 40 at 1 second and 2, and 1,536 at 30 seconds and 2.
  • Algorithms that count requests steer away from a server that holds its requests without answering: 38 failures for least connections, against 385 for round robin. A server that refuses at once would not give that signal.
  • A check cannot create capacity: at 160 requests a second, with three servers left, every algorithm fails 11.6% to 21.2% of the requests.
  • A faster check finds failures sooner and removes good servers more often.

Next: sticky sessions, and what pinning a client to a server costs.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.