FjellSystem Design Lab
Lesson 3.1• 5 min read

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, .org and 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., 90%90\% to the current cluster, 10%10\% 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

High TTL (e.g., 86,400s / 24h)  ⟹  fewer lookups, but changes and failover take effect slowly.\text{High TTL (e.g., 86,400s / 24h)} \implies \text{fewer lookups, but changes and failover take effect slowly}.
Low TTL (e.g., 30s–60s)  ⟹  faster failover and traffic shifting, but more lookups and query cost.\text{Low TTL (e.g., 30s--60s)} \implies \text{faster failover and traffic shifting, but more lookups and query cost}.

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

Client regionResolved destinationDNS RTTHealth
North America (East)
POP-US-East
12 ms
Healthy
Europe (Frankfurt)
POP-EU-Central
14 ms
Healthy
Asia Pacific (Tokyo)
POP-AP-Tokyo
18 ms
Healthy
South America (São Paulo)
POP-SA-SP
22 ms
Healthy

Latency-based DNS: With resources in several regions, the DNS service answers each query with the region that offers the user the best latency.

Figure 1: Anycast Geo-DNS routing global client queries to topologically closest edge regions.
  • 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

Adapted from The System Design Primer by Donne Martin, licensed under CC BY 4.0. Changes were made.