Sub topic 2
Detecting Failure and Moving Traffic
Assume you know basic client-server architecture, load balancers, DNS, timeouts, and health checks. This chapter shows how a system detects failure and directs requests elsewhere.
Detecting failure with health checks and heartbeats
A health check is a request or test that asks whether a component can serve traffic. A load balancer might request /health every five seconds. The service should check critical dependencies, such as its database, but it should respond quickly.
A heartbeat is a regular signal that says, “I am still alive.” Cluster members often exchange heartbeats. Missing several heartbeats suggests failure, but it does not prove that the process is dead. A network partition can block heartbeats while both processes continue running.
Detection settings trade speed against false positives:
- Short intervals and low failure counts detect failure quickly but may react to temporary network problems.
- Longer intervals and more required failures reduce false positives but extend the outage.
- A check should test useful readiness, not only whether a process accepts TCP connections.
The failover sequence
Failover is the controlled move from a failed component to a healthy component:
- Detect failure with checks, heartbeats, or error reports.
- Decide whether the failure is real and select an eligible target.
- Switch traffic or promote a replica.
- Verify successful responses, latency, and data correctness.
- Alert operators and record the event.
The decision stage prevents an unhealthy or stale target from receiving requests.
Load balancers, DNS, and clients
A load balancer can remove an unhealthy server quickly. DNS failover changes the address returned to clients, but DNS caching means the change may take longer than its configured time to spread.
upstream app {
server 10.0.0.11:8080 max_fails=3 fail_timeout=10s;
server 10.0.0.12:8080 max_fails=3 fail_timeout=10s;
server 10.0.0.13:8080 backup;
}
server {
listen 80;
location / {
proxy_pass http://app;
proxy_next_upstream error timeout http_502 http_503;
}
}Clients should also use bounded timeouts and retry another endpoint. Retries need limits and backoff so a failure does not multiply load.
import urllib.request
ENDPOINTS = ["http://primary:8080", "http://secondary:8080"]
def fetch(path, timeout=1.0):
last_error = None
for base in ENDPOINTS:
try:
with urllib.request.urlopen(base + path, timeout=timeout) as response:
return response.read()
except OSError as error:
last_error = error
raise RuntimeError("all endpoints failed") from last_errorExercise
Why can DNS failover be slower than load-balancer failover?
Show answer
DNS responses can remain in client and resolver caches until their time-to-live expires.