Sub topic 4
Routing users to regions: DNS, anycast and global load balancing
This chapter assumes you know how DNS resolution works (resolvers, records, TTLs), what a Layer 4 or Layer 7 load balancer does, and the basic active-active idea of several regions serving live traffic. It covers the three main ways to send each user to a region, what each one gets wrong, and how to choose.
What the routing layer has to do
In an active-active design, something must decide which region handles each request. That decision has three inputs:
- Proximity. Send the user to a region that gives low latency.
- Health. Never send the user to a region that cannot serve them.
- Policy. Honor rules such as data residency, a gradual rollout, or a cap on how much traffic one region receives.
Every technique below trades these off differently. The main difference is where the decision is made: in DNS answers, in network routing, or in a proxy that sees each request.
Approach 1: DNS-based routing
With DNS routing, the authoritative name server returns a different answer depending on who asks. The client then connects directly to the returned address.
Common routing policies:
- Latency-based. The answer is the region with the lowest measured latency to the requester.
- Geolocation. The answer depends on the requester's country or continent. This suits residency rules more than speed.
- Weighted. Each region gets a share of answers, such as 90/10. This suits canary releases and draining a region.
- Failover. A primary answer is used while healthy, and a secondary otherwise. This is active-passive routing, so active-active designs usually avoid it.
The key detail is that the name server sees the recursive resolver's address, not the user's. A user on a corporate network or a public resolver may be located far from their resolver, so "nearest" can be wrong. EDNS Client Subnet reduces this problem, but not every resolver sends it.
A latency record with a health check
This Terraform defines a health check and a latency record for one region. You add an equivalent pair for each other region, using the same name and type but a different set_identifier and region.
variable "zone_id" {
type = string
}
variable "us_endpoint_ip" {
type = string
}
variable "us_endpoint_fqdn" {
type = string
}
resource "aws_route53_health_check" "us" {
fqdn = var.us_endpoint_fqdn
port = 443
type = "HTTPS"
resource_path = "/healthz"
request_interval = 10
failure_threshold = 3
}
resource "aws_route53_record" "api_us" {
zone_id = var.zone_id
name = "api.example.com"
type = "A"
ttl = 60
records = [var.us_endpoint_ip]
set_identifier = "us-east-1"
health_check_id = aws_route53_health_check.us.id
latency_routing_policy {
region = "us-east-1"
}
}When the health check fails, Route 53 stops returning that record and answers with the next-best healthy region.
Where DNS routing is weak
- Slow failover. Total time is roughly
request_interval × failure_thresholdfor detection, plus the TTL, plus any extra caching. With the values above that is about 30 seconds of detection plus 60 seconds of TTL. In practice it is longer. - Uncontrolled caching. Some resolvers, operating systems, and runtimes cache longer than the TTL. Some clients hold one address for the life of the process.
- Coarse control. Traffic splits happen per DNS answer, not per request. A single large resolver can send a big population to one region.
The sequence below shows why failover takes time.
Approach 2: Anycast
With anycast, many locations advertise the same IP address using BGP. The internet's routers send each packet toward whichever advertising location is closest by routing policy, usually the shortest path in network hops.
Advantages:
- No DNS dependency. The client uses a fixed IP, so there are no stale cached answers.
- Fast failover. If a location stops advertising the address, routers converge on another one, often in seconds.
- Stable addresses. You can give partners two static IPs to put on an allowlist.
Costs and gotchas:
- Closest is not fastest. BGP picks by path policy, not measured latency. A user can be routed to a farther region.
- Route changes mid-connection. If routing shifts during a long TCP connection, packets may arrive at a different location that has no state for it, and the connection resets.
- Coarse region control. You steer by changing advertisements, which is blunt. Managed services add traffic dials and weights on top.
A managed example is AWS Global Accelerator. It gives you two static anycast IPs. Traffic enters the AWS network at an edge location near the user and travels the AWS backbone to a healthy regional endpoint. It supports endpoint health checks, per-region traffic dials, and endpoint weights.
Approach 3: Global load balancing at the edge
A global load balancer or CDN accepts the connection at an edge location, usually reached by anycast. It terminates TLS there and opens its own connection to an origin region. Because it sees each HTTP request, it can decide per request:
- Route by path, header, cookie, or tenant ID.
- Retry a failed request against a second region.
- Keep a user on one region with session affinity.
- Shed load from an overloaded region.
The cost is an extra hop and a new dependency. The provider's edge fleet is now in your critical path. It also needs its own health checking of your origins, and you must make sure origins accept traffic only from the edge, so users cannot bypass your routing rules.
Comparing the approaches
| Property | DNS routing | Anycast | Edge global load balancer |
|---|---|---|---|
| Decision made at | Name resolution | Network routing | Each request |
| Failover speed | Tens of seconds to minutes | Seconds | Seconds |
| Client IP stability | Changes with answers | Fixed | Fixed (often anycast) |
| Routing control | Per answer, coarse | Coarse | Fine, per request |
| Extra network hop | No | No (or backbone entry only) | Yes |
| Main risk | Caching you do not control | Mid-connection route changes | Provider is a dependency |
Most production systems combine them. A common pattern is anycast static IPs in front of an edge proxy, with DNS used only to point a name at those IPs.
Choosing a routing approach
Use these questions in order:
- What failover time can you accept? If your RTO is under a minute, DNS alone is not enough.
- Do you need per-request decisions? Tenant pinning, path-based routing, and cross-region retries need an edge load balancer.
- Do clients need fixed IPs? Allowlists point toward anycast.
- Do rules depend on geography? Residency requirements point toward geolocation policies, which you should enforce again in the application.
Common errors
- Same address for all regions in one record set. Latency and weighted policies need a distinct
set_identifierper record. Without it, the records do not form a routing set. - No health check on one of the records. Unhealthy endpoints keep receiving answers.
- Failover that overloads the survivor. If a region fails and the others cannot absorb the extra load, routing moves the outage rather than ending it. Chapter on capacity planning covers sizing for this.
- Sticky clients. Long-lived clients such as mobile apps and gRPC channels keep using one endpoint after routing changes. Add reconnect logic and a maximum connection age.
Exercise
A service uses Route 53 latency routing with a health check that probes every 10 seconds and needs 3 failures, and the record TTL is 60 seconds. Estimate the best-case time before a client with a normal, TTL-respecting resolver stops using the failed region. Name one way a real client could take longer.
Show answer
Detection takes about 10 × 3 = 30 seconds. A client that just cached the answer then waits up to 60 more seconds, so the best-case worst-timed client sees about 90 seconds before it gets a new answer. A real client could take longer if its resolver, operating system, or runtime caches records beyond the TTL, or if it holds an open connection to the failed region and never re-resolves.
Exercise
You must give a partner two fixed IP addresses for their firewall allowlist, and failover must complete in seconds. Which routing approach fits, and what risk should you plan for?
Show answer
Anycast static IPs, for example through a service such as AWS Global Accelerator, fit both requirements. The risk to plan for is that routing can change mid-connection and reset long-lived TCP connections, so clients need retry and reconnect logic. Also note that anycast picks the closest region by network path, which is not always the lowest-latency one.