Sub topic 8
Failing over traffic with load balancers and DNS
This chapter assumes you know what a load balancer and a DNS record are, and that you have read the earlier chapters on health checks and the failover sequence. Here you will see two common ways to move traffic away from a failed server, and where each one breaks.
Two places to redirect traffic
Failover at the traffic level means clients stop reaching a failed server without anyone editing client code. There are two common layers for doing it.
- A load balancer sits in front of the servers. It receives every request and picks a healthy server. Clients keep using one address.
- DNS answers the question "which IP address belongs to this name?" Failover changes the answer, so new connections go somewhere else.
The load balancer reacts in seconds. DNS reacts as fast as caches allow, which is usually much slower.
Load balancer failover
A load balancer runs health checks against each server. When a server fails enough checks in a row, the balancer stops sending it traffic. This is the detect-decide-switch sequence from the previous chapter, done automatically.
The NGINX configuration below shows the main settings.
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;
}
}max_fails=3 fail_timeout=10smarks a server unavailable for 10 seconds after 3 failed attempts. After that, NGINX tries it again.backupmeans server 13 receives traffic only when both active servers are unavailable.proxy_next_upstreamretries a failed request on another server, so the client often never sees the error.
Retrying has a cost. Only retry requests that are safe to repeat, such as GET. Retrying a payment POST can charge a customer twice unless the operation is idempotent (repeating it has the same effect as doing it once).
DNS failover
With DNS failover, a health checker watches your endpoints. When the primary fails, it changes the record to point at the standby address.
The limit is caching. Resolvers and clients keep an answer for the record's TTL (time to live). If the TTL is 300 seconds, some clients keep using the dead address for up to five minutes after you change the record. Some resolvers and applications hold answers longer than the TTL.
| Aspect | Load balancer | DNS |
|---|---|---|
| Typical switch time | Seconds | TTL plus detection time, often minutes |
| Client sees one address | Yes | No, address changes |
| Works across regions | Needs a global balancer | Yes, commonly used for this |
| Main weakness | The balancer can fail | Caching you do not control |
A lower TTL speeds up failover but increases query load and makes you dependent on your DNS provider being reachable. Common practice is a TTL of 30 to 60 seconds on records you expect to fail over.
Choosing between them
Use a load balancer for failover inside a region, where you need fast switching. Use DNS to move traffic between regions or between balancers, and accept the slower switch. Many designs combine them: DNS picks a region, and a load balancer in that region picks a server.
Exercise
A DNS record has a TTL of 300 seconds. The primary fails and the health checker updates the record 20 seconds later. What is the longest a client with a well-behaved resolver may keep reaching the dead primary, and what can you change to shorten it?
Show answer
Up to about 320 seconds after the failure, which is 20 seconds of detection plus the 300-second TTL. Lower the TTL, for example to 30 to 60 seconds, or put a load balancer in front so clients keep one address and the switch happens at the balancer.