DNS and global traffic routing
The DNS hierarchy, latency and weighted routing, failover health checks, and anycast.
What you will learn
- Trace how a name is resolved, from a recursive resolver down to an authoritative nameserver.
- Choose between latency-based, geolocation, weighted and failover routing.
- Explain anycast, and the trade-off a record's TTL makes.
The Domain Name System (DNS)
Before a client can send an HTTP request or establish a TCP handshake, it must translate a human-readable domain name (such as api.fjell.dev) into an IP address (198.51.100.42).
DNS is hierarchical:
- Recursive resolvers, provided by your ISP or a public service (for example Google Public DNS at
8.8.8.8), walk the hierarchy on the client's behalf and cache what they learn. - Root servers (
.): 13 named root server identities (A to M), run by 12 independent organizations and served from about 2,000 instances worldwide. - Top-level domain (TLD) servers for
.com,.io,.organd so on. - Authoritative nameservers hold a domain's records: A (name to IPv4 address), AAAA (IPv6), CNAME (name to another name), NS (which servers are authoritative) and MX (mail servers).
Lower-level servers, browsers and operating systems all cache answers, for a period set by the record's time to live (TTL). Cached answers can go stale until it expires.
Global traffic routing policies
Managed DNS services such as Route 53 and Cloudflare can choose the answer per query:
1. Latency-based routing
For resources in several regions, return the region that offers the user the best latency (Route 53 routes to "the Region that provides the best latency"). A resolver in Frankfurt typically gets the European address; one in California gets US West.
2. Geolocation routing
Answer based on where the user is, for example to keep EU users in the EU.
3. Weighted round robin and canary routing
Split answers in set proportions (e.g., to the current cluster, to a canary). Also used to drain servers before maintenance, balance clusters of different sizes, and run A/B tests.
4. Failover with health checks
Health checkers probe each endpoint; after a number of consecutive failures you configure, DNS stops returning that address and answers with a standby instead. Route 53, for example, runs checkers in many locations and treats an endpoint as unhealthy when 18% or fewer of them see it as healthy.
Anycast: one address, many places
With unicast, an IP address belongs to one location. With anycast, the same address is announced from many locations using BGP, and the Internet's routing system delivers each packet to one of them, normally the nearest in routing terms. Several root server operators, and public resolvers such as 8.8.8.8, use anycast to spread one address across many sites. One caveat: the nearest location in routing terms is not always the one with the lowest round-trip time, so anycast does not guarantee the fastest path.
The DNS TTL trade-off
Even a low TTL is not instant: an answer cached before you lowered the TTL lives out its old TTL, which is why you lower it well before a planned change.
DNS has costs too: each lookup adds a little delay (mitigated by caching), and DNS services have themselves been taken down by DDoS attacks, making sites unreachable by name.
Anycast and global routing policies
Latency-based DNS: With resources in several regions, the DNS service answers each query with the region that offers the user the best latency.
- US Users: 1 × Clients & Users
- EU Users: 1 × Clients & Users
- Route53 Anycast DNS: 2 × Anycast DNS / Route53
- CDN Edge (us-east): 2 × CloudFront / CDN Edge
- CDN Edge (eu-central): 2 × CloudFront / CDN Edge
- Origin Load Balancer: 2 × Application Load Balancer
- US Users sends requests to Route53 Anycast DNS
- EU Users sends requests to Route53 Anycast DNS
- Route53 Anycast DNS sends requests to CDN Edge (us-east)
- Route53 Anycast DNS sends requests to CDN Edge (eu-central)
- CDN Edge (us-east) sends requests to Origin Load Balancer
- CDN Edge (eu-central) sends requests to Origin Load Balancer
Key takeaways
- Resolvers, browsers and operating systems cache answers for the record's TTL, so answers can be stale until it expires.
- Managed DNS can choose each answer: by latency, by location, by weight (for canaries and draining) or by health checks for failover.
- Anycast announces one address from many places and lets routing pick one, usually but not always the fastest.
- A low TTL speeds up failover at the cost of more lookups; lower it well before a planned change, because old answers live out their old TTL.
Sources
- System Design Primer: Domain name system
- Root Server Technical Operations Association (root-servers.org)
- Amazon Route 53: Choosing a routing policy
- Amazon Route 53: How health checks determine health
- RFC 4786: Operation of Anycast Services
- Google Public DNS FAQ
- RFC 3596: DNS Extensions to Support IPv6 (AAAA)
Adapted from The System Design Primer by Donne Martin, licensed under CC BY 4.0. Changes were made.