Health Checks and Failover
You'll learn how load balancers keep traffic flowing to healthy servers only, automatically routing around failures.
The Core Problem
Servers crash, freeze, or become overloaded. Without detection, a load balancer would keep sending users to broken instances, causing failed requests and frustrated users. Health checks solve this by continuously monitoring server status.
How Health Checks Work
A load balancer periodically sends requests to each backend server—typically a simple HTTP GET to a /health or /ping endpoint. The server must respond within a timeout (e.g., 2 seconds) with a success status code (like 200 OK).
Think of it like a lifeguard doing head counts: every 30 seconds, they verify each swimmer is okay. If someone doesn't respond, immediate action is needed.
Common Health Check Strategies
- Shallow checks: "Is the server running?" (responds to TCP connection)
- Deep checks: "Can it handle real work?" (queries database, checks dependencies)
- Thresholds: Mark unhealthy after 2-3 consecutive failures, not just one blip
Failover in Action
When a server fails health checks, the load balancer immediately stops routing new requests to it. Existing connections may be drained gracefully or terminated. Traffic flows only to the remaining healthy instances.
If the server recovers and passes health checks again, it's automatically returned to the pool—no manual intervention needed. This self-healing behavior is crucial for high availability.
The Trade-off
More frequent health checks detect failures faster but create extra load. Typical intervals: 5-30 seconds. Balance detection speed against overhead.
Key Takeaway: Health checks give load balancers the intelligence to automatically route around failing servers, ensuring users only reach healthy instances without manual failover procedures.