Route users to the nearest healthy site
Global users should hit the closest region, and a dead region should be skipped.
Say it like this
Users should land in the nearest healthy region, so I resolve the hostname with latency-based DNS backed by health checks, and anycast the edge IPs so a dead site simply stops being routed to.
Basic solution
Use latency-based or geo DNS with health checks, or anycast the same IP from many sites.
Two layers of routing
Anycast delivers the packet to the nearest edge with no DNS involvement. Latency-based DNS with health checks decides which backend region that edge forwards to, and drops a region that fails its checks.
How it works
- A client resolves app.example.com by asking its DNS resolver (usually its ISP's or a public one such as 8.8.8.8). The resolver asks your authoritative DNS server, which looks at where the resolver is, or at the client's network if the resolver passes it along as an EDNS client subnet, and returns the IP address of the region with the lowest measured latency to that network.
- Each regional DNS record has a health check attached: a probe that hits that region's API every few seconds. When a region fails enough probes in a row, the DNS server stops returning that region's record, so new lookups get the next best region instead.
- The resolver caches the answer for the record's TTL (the number of seconds the record says it may be reused), so every new client behind that resolver gets the same region until the TTL expires and the resolver asks again.
- For anycast, every edge site announces the same IP prefix to the internet over BGP, the protocol routers use to share routes. Each router forwards a packet for that IP toward whichever announcing site is closest in network terms, so the client reaches a nearby edge without needing a special DNS answer at all.
- If an anycast site withdraws its announcement, or simply goes dark, the routers around it recompute their paths and send the next packets to the next nearest site. This takes seconds to a minute and involves no DNS caching.
Two layers, two speeds
- DNS steering decides which backend region a client talks to. A change here propagates only as fast as resolvers refresh, so it takes up to one TTL.
- Anycast decides which edge site absorbs each packet. A change here propagates at BGP speed, typically under a minute and often seconds, because no client cache is involved.
Worked example
Ride matching across regions
- Three regions: us-east, eu-west, ap-south. Riders in Sydney resolve to ap-south at ~30 ms instead of us-east at ~200 ms, which matters for a request-offer-accept loop that runs three round trips.
- Record TTL 60 s. If ap-south fails its health check, the longest a cached resolver keeps sending Sydney riders to the dead region is 60 s, plus the health-check interval (10 s × 3 failures = 30 s). Worst case about 90 s of errors for new sessions.
- Query volume: 10M daily riders, each resolving a handful of times a day through shared resolvers, so a few hundred DNS queries per second reach the authoritative servers. Trivial; DNS cost is per query at fractions of a cent per thousand.
- Anycast on the CDN edge: the TLS handshake and static assets terminate at a PoP 10 to 20 ms away, and that PoP forwards API calls to the chosen region over the provider backbone, which is faster and more stable than the public path.
- Session stickiness: a trip that started in ap-south keeps talking to ap-south because the app caches the resolved regional API host for the session; DNS only steers the first connection.
Variants and when each wins
Latency-based DNS
- The DNS provider keeps a map of measured latency from networks to your regions and answers each resolver with the fastest region for its network. It wins for user experience, as long as the provider's map is accurate and resolvers sit near their users.
Geo DNS
- The DNS server answers by the resolver's country or continent instead of measured latency. It wins when you need predictable placement, for example to keep EU users on EU servers for data-residency rules, even though network paths do not always follow geography.
Weighted or failover DNS
- Records carry fixed weights, or a passive record is returned only when the primary record fails its health checks. It wins for canary releases and simple active-passive setups, but failover is slow because every cached answer has to expire first.
Anycast
- One IP address is announced from every site and routers deliver each packet to the nearest one. It wins for fast failover and for absorbing floods at the edge, but a long-lived connection can break if routes change and a client's packets start arriving at a different site that has no state for it.
Recommendation: anycast at the CDN and edge, latency-based DNS with health checks to pick the backend region, and short TTLs on anything that must fail over.
Failure modes
Slow failover because of TTL caching
- A region is dead, but some users keep landing on it for minutes after the health check withdrew its record. Resolvers keep serving the cached answer until its TTL expires, and some resolvers ignore TTLs and cache longer than told. Keep failover records at a 30 to 60 second TTL, and have the client retry against a second regional hostname when the first one errors, so a cached answer is not the end of the story.
Health check passes while the service is broken
- DNS keeps sending traffic to a region that is returning errors to real users. The health probe only hits a shallow endpoint, such as a static page, that stays green even when the region's database is down. Probe an endpoint that exercises a real dependency path while keeping it cheap, and alert on the region's error rate separately so a green probe cannot hide a broken region.
Resolver far from the user
- Users in one country are routed to a region on another continent. Their public resolver is located elsewhere and does not pass the client's subnet along, so the DNS server judges distance from the resolver, not from the user. Anycast the edge so the first hop is always near the user regardless of DNS, and treat DNS geography as a hint rather than the only steering mechanism.
Anycast route flap breaking long connections
- WebSocket sessions drop in a burst during a BGP change. The packets of an established TCP flow start arriving at a different anycast site, which has no record of that connection and resets it. Keep anycast for stateless layers such as DNS, the TLS edge and the CDN, and terminate long-lived connections on unicast regional endpoints that sit behind the edge.
Single DNS provider outage
- The whole site is unreachable even though every server is healthy. The authoritative DNS provider is down, so no resolver can learn any of your addresses. Run two authoritative DNS providers with synchronised zone data and list both at the registrar, so resolvers can fall back to the second when the first stops answering.
Interviewer follow-ups
Answer out loud before you open each one.
Why is failover through DNS slower than through a load balancer?
A load balancer sees every request and can stop sending to a bad backend immediately. DNS answers are cached by resolvers and operating systems for the TTL, and some clients cache longer than told. You can only shorten the window, not remove it, which is why multi-region designs also retry at the client.
How does anycast help against a DDoS attack?
Attack traffic is spread across every site that announces the prefix, because each source is routed to its nearest site. A 1 Tbps flood split across 200 PoPs is 5 Gbps per PoP, which each can absorb and filter. With a single unicast IP the whole flood lands on one link.
What TTL would you pick for the main hostname, and what is the trade-off?
Around 60 s for records that may need to fail over, and minutes to hours for stable records like the CDN hostname. Short TTLs mean more queries to your DNS provider and slightly slower first connections after expiry; long TTLs mean slow failover. The failover case usually decides it.
Should a request that starts in one region be allowed to move to another?
Reads can move freely if the data is replicated. Writes should stay in the region that owns that user or trip to avoid conflicts, so the client pins the regional endpoint for the session and the edge forwards to it. A full region failure is the exception where the client re-resolves and accepts a brief inconsistency.
Connections
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon Route 53 | Authoritative DNS | Cloud DNS | Azure DNS, Traffic Manager | Cloudflare DNS, NS1 |
| Amazon CloudFront | Anycast edge PoP | Cloud CDN, Media CDN | Azure Front Door | Cloudflare, Fastly, Akamai |
| Amazon EC2 Auto Scaling | ap-south API, us-east API | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |