Video in progress
How does a load balancer know a server is alive, and what happens if the health check is written wrong?
The load balancer regularly sends a check request to each server (for example, GET /actuator/health). If there is no answer several times in a row, it takes the server out of rotation. A badly written health check can pull a healthy server out or keep a dead one in.
📖 Answer
Spring Boot has a ready-made endpoint:
management:
endpoint:
health:
probes:
enabled: true # /actuator/health/liveness and /readiness
Typical settings: check every 10 seconds, remove after 3 failures in a row, bring back after 2 successes.
Two kinds of mistakes are common:
- The health check is too shallow: it just returns
200 OK, even when the database connection is broken. The server looks alive but returns 500 on every request. - The health check is too deep: it checks the database, Redis and an external API every time. When the external API slows down, all servers suddenly look "dead" and the whole service goes offline.
In practice, a readiness check covers things you can't work without, like your own database, and does not check external services.
Sign up to mark studied questions and save your progress.