Sub topic 12
Global load balancing with DNS and anycast
This chapter assumes you know how a single load balancer spreads traffic across backends, what a health check is, and how DNS resolves a name to an IP address. It covers how to spread traffic across several regions, which a single load balancer cannot do.
Why a regional load balancer is not enough
A load balancer sits in one site. If that site loses power or its network link, the load balancer goes down with every backend behind it. Users far from the site also pay a latency cost on every request, because packets travel a long way.
Global load balancing solves both problems. It sends each user to a healthy site, and usually to a nearby one. There are two main mechanisms: DNS-based steering and anycast routing.
DNS-based global load balancing
With DNS steering, the authoritative DNS server returns different answers depending on who asks and which sites are healthy. The user gets the IP address of a regional load balancer, then connects to it directly.
Common routing policies:
- Failover: return the primary site while it is healthy, otherwise a secondary.
- Geographic: return the site assigned to the user's country or region.
- Latency-based: return the site with the lowest measured latency to the user's resolver.
- Weighted: return each site in a set proportion, useful for gradual migrations.
The DNS provider runs its own health checks against each site and removes failed sites from answers.
Limits of DNS steering
- Caching and TTL: Resolvers cache answers for the time to live (TTL). After a site fails, some users keep receiving its address until the TTL expires. A short TTL, such as 30 seconds, reduces this but increases DNS query volume.
- Ignored TTLs: Some resolvers and client software hold records longer than the TTL says.
- Resolver location: The authoritative server sees the resolver's address, not the user's. A user on a distant public resolver may be steered to the wrong region. The EDNS Client Subnet extension passes part of the user's address to help, but not every resolver sends it.
Anycast
With anycast, several sites announce the same IP address through BGP (Border Gateway Protocol, the routing protocol between networks). Routers send each packet along the shortest path in their routing table, so a user usually reaches the topologically nearest site.
Failover is fast. If a site stops announcing the address, or withdraws it after a failed health check, routers converge on the remaining sites in seconds. No DNS cache is involved.
Anycast has its own gotchas:
- Route changes mid-connection: If routing shifts during a TCP connection, packets may reach a different site that has no state for it, and the connection resets. This is why anycast suits short requests and UDP services such as DNS, and why long-lived connections need care.
- Nearest by hops, not by speed: BGP picks paths by policy and path length, not measured latency or site load.
- Uneven load: You cannot easily control how much traffic each site receives.
Choosing between them
| Concern | DNS steering | Anycast |
|---|---|---|
| Failover speed | Bounded by TTL and resolver behavior | Seconds, at routing convergence |
| Traffic control | Fine-grained policies, weights | Coarse, depends on BGP |
| Long TCP connections | Safe, address stays fixed | Can break on route change |
| Operational needs | A DNS provider | Your own IP space and BGP peering, or a provider that offers it |
Many large services combine both. DNS returns an anycast address, or anycast fronts an edge network that then proxies to regional load balancers.
Checking DNS behavior
Use dig to see what a resolver returns and how long it will cache the answer:
dig +noall +answer app.example.comThe second column of each line is the remaining TTL in seconds. Repeat the query from different networks to see geographic steering at work.
Exercise
A service uses DNS failover with a TTL of 300 seconds. The primary site fails. Roughly how long can some users keep being sent to it, and what two approaches would shorten that time?
Show answer
Some users can reach the failed site for up to about 300 seconds, or longer if their resolver ignores the TTL. You can lower the TTL on the record, or switch to anycast so that the failed site's route is withdrawn and routers redirect traffic within seconds.
Exercise
Why is anycast a poor fit for a service built on long-lived TCP connections that carry per-connection state?
Show answer
If routing changes mid-connection, packets can arrive at a different site that has no state for that connection, so the connection resets.