DNS
How a name becomes an IP address, what caching and TTLs do to every change you make, and where DNS-based load balancing and failover stop working.
The problem
In the early 1980s every host on the ARPANET found the others through one text file, HOSTS.TXT, kept by hand at the Network Information Center at SRI. Administrators emailed in their changes and every site fetched a fresh copy by FTP. That was fine for a few hundred machines. As the network grew, the file was out of date somewhere at all times, two sites could claim the same name, and one office was a bottleneck for every change. In 1983 Paul Mockapetris designed the replacement, the Domain Name System: a naming database with no central file, in which each organisation answers for its own names and everyone else caches the answers.
That design choice, delegation plus caching, is why DNS scales to the whole internet. It is also behind a problem you will meet in interviews. Suppose you run shop.example.com in two regions. One region fails at 14:00 and by 14:01 you have pointed the DNS record at the healthy one. At 14:15 some users are still getting errors. Nothing is wrong with your change. To see why they are stuck, you need the resolution path.
How a name is resolved
Step 1 of 8
The browser needs an IP address for shop.example.com. Its own cache and the operating system's cache are empty, so the stub resolver sends one query to the recursive resolver it was configured with and waits.
Read the steps as text
- The browser needs an IP address for shop.example.com. Its own cache and the operating system's cache are empty, so the stub resolver sends one query to the recursive resolver it was configured with and waits.
- The resolver has nothing cached either, so it starts at the top. A root server does not know the answer, but it knows who runs dot com, and replies with a referral to the dot com name servers.
- The resolver repeats the same question to a dot com server. It does not know the address either, but it knows which name servers example.com registered, and refers the resolver to them.
- The third query goes to the domain's authoritative server, which holds the real records. It answers with an A record and a time to live of 300 seconds.
- The resolver hands the address to the laptop and keeps a copy for 300 seconds. It also cached the two referrals, so its next lookup for any dot com name skips the root.
- Only now does the browser open a connection to the web server. DNS told it where to go and then stepped aside. No user traffic flows through DNS.
- Anyone else who uses the same resolver and asks within the next five minutes gets the cached answer in a millisecond or two. This is why most lookups are fast, and why the root servers are not overwhelmed.
- The same cache is the catch. If you change the record now, this resolver keeps handing out the old address until its copy expires. A DNS change reaches users one expiring cache at a time, never all at once.
Four kinds of server take part.
- The stub resolver is the small client in the operating system. It knows the address of one or two recursive resolvers and does nothing clever.
- The recursive resolver does the work. It is run by an ISP, a company, or a public service such as Google’s 8.8.8.8 or Cloudflare’s 1.1.1.1. It asks other servers on the user’s behalf and caches what it learns.
- The root servers know which servers run each top-level domain. There are 13 root server names, a to m, and each is an anycast address served from many sites around the world. The TLD servers for
.comor.ukknow which name servers each registered domain uses. - The authoritative server for your domain holds the actual records. This is the part you control, usually through a managed provider.
Each of the root and TLD servers answers with a referral, not an address, and the resolver follows the chain. A lookup from a cold cache costs a few network round trips, commonly tens of milliseconds and sometimes more. A cached lookup costs almost nothing. Queries normally travel over UDP on port 53 and fall back to TCP when an answer is too large, for the reasons covered in TCP vs UDP.
Records that matter
| Record | What it maps | Worth knowing |
|---|---|---|
| A, AAAA | Name to IPv4 or IPv6 address | Several records for one name give several answers |
| CNAME | Name to another name | Cannot share a name with other records, so it is not allowed at the bare domain |
| NS | Zone to its authoritative servers | This is how delegation works |
| MX | Domain to mail servers | Has a priority per server |
| TXT | Name to free text | Domain verification, SPF and DKIM |
| SRV | Service to host and port | Carries priority and weight; used by some service discovery |
| CAA | Domain to permitted certificate authorities | Limits who may issue your certificates |
The CNAME rule matters in practice. You cannot make example.com itself a CNAME to a CDN or load balancer hostname, because the bare domain must also hold SOA and NS records. Providers work around this with ALIAS records or CNAME flattening, where the provider resolves the target and returns ordinary A records.
TTLs and caching
Every record carries a time to live in seconds. A cache may reuse the record for that long without asking again, and caches exist at every level: the browser, the operating system and the recursive resolver.
A long TTL, such as a day, means fewer queries, faster page loads and protection if your authoritative servers are briefly unreachable. It also means a change takes up to a day to reach everyone. A short TTL, such as 60 seconds, reverses both. The detail that catches people out is that a cache honours the TTL it was handed. If the record had a one-day TTL yesterday, lowering it now does nothing for resolvers that already hold a copy. Before a planned migration, lower the TTL at least one old TTL in advance.
Failed lookups are cached too. If someone looks up a name before you create it, the “no such name” answer can be reused for a while, for a period set in the zone’s SOA record.
DNS load balancing and GeoDNS
The simplest load balancing is several A records for one name. Resolvers hand back the set, often in rotated order, and clients tend to try the first. Managed DNS services add weighted answers, latency-based answers, health checks that remove dead addresses, and GeoDNS, which returns a different answer depending on where the query comes from.
All of these share two limits. First, the authoritative server sees the recursive resolver, not the user. A user in Paris who uses a resolver in Virginia looks American. The EDNS Client Subnet extension lets a resolver pass along part of the user’s address, but not every resolver sends it, and some public resolvers leave it out on purpose for privacy. Second, once an answer is cached, you have no further say. One large resolver may send every one of its users to the same address until the TTL runs out, and DNS never learns that the server behind that address is overloaded.
So use DNS for coarse decisions: which region, which CDN. Leave per-request decisions to a load balancer.
Anycast
With anycast, many sites announce the same IP address through BGP, the protocol that routers use to exchange routes. Each router forwards packets towards the nearest announcement, so a user in Tokyo and a user in London reach different machines at the same address. If a site fails, it stops announcing and its traffic flows to the next nearest site. The address never changes, so there is no cached DNS answer to wait for.
Anycast is how the root servers, the public resolvers and most CDNs spread load and absorb attacks. It has limits. BGP chooses by routing policy and path length, which is not always the lowest latency, and it knows nothing about load. A route change in the middle of a TCP connection can send later packets to a site that has never heard of that connection. Running it yourself needs your own address space and BGP sessions, so most teams get anycast by using a CDN or a cloud provider’s global load balancer.
Why DNS failover is slow
Back to 14:15. Failover through DNS takes at least the detection time plus the TTL. With health checks every 10 seconds, three failures required and a 60 second TTL, that is about 90 seconds for well-behaved clients. Then comes the tail:
- Open connections. A client with a keep-alive connection or a connection pool does not look the name up again until the connection breaks.
- Client caches. Some runtimes, HTTP libraries and mobile apps resolve a name once and keep the result far longer than the TTL.
- Resolvers that stretch TTLs. Some enforce a minimum, so very low TTLs are not always honoured.
The DNS provider can itself be the failure. On 21 October 2016 a denial-of-service attack on the managed DNS provider Dyn made many large sites unreachable for hours, although their own servers were healthy. Using two DNS providers is the standard defence.
When to choose it and when not to
Use DNS for naming, for steering users to a region or a CDN, and for failover when a recovery time of a few minutes is acceptable. Do not use it as the only failover mechanism when you need recovery in seconds, or to balance load between individual servers. Inside a region, load balancers with health checks react in seconds. Across regions, anycast or an active-active design, in which losing a region only removes capacity, avoids waiting on caches. Fast-changing sets of backend instances need proper service discovery, not public DNS records.
In the interview
DNS comes up in two places. The first is “what happens when you type a URL”, where the resolution path is the opening third of the answer. The second is any multi-region design, when you say how users reach the right region and what happens when one fails.
A strong one-minute answer: the client’s stub asks a recursive resolver, which walks root, TLD and authoritative servers and caches by TTL. I would use GeoDNS or anycast to pick a region and load balancers within it. DNS failover costs detection time plus TTL plus a tail of clients that hold on to old answers, so for fast failover I would rely on anycast or on both regions already serving traffic.
Key takeaways
- A stub resolver asks a recursive resolver, which walks the root, TLD and authoritative servers and caches every answer on the way.
- The TTL is a promise you make to caches. A change cannot be relied on to take effect faster than the TTL that was in force before it.
- DNS load balancing is coarse. The authoritative server sees the resolver rather than the user, and has no say once an answer is cached.
- DNS failover takes detection time plus the TTL, and clients that hold connections open or ignore TTLs take longer still.
- Anycast announces one IP address from many sites and lets BGP choose, which moves failover out of DNS caches and into routing.
- DNS is a dependency of everything you run, so plan for the failure of the DNS provider itself.
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
What are the four kinds of server in a DNS lookup?
The stub resolver on the device, a recursive resolver that does the work and caches, then root, TLD and authoritative name servers that the recursive resolver queries in turn.
You plan to move a service to a new IP on Friday. What do you do with the TTL?
Lower it (for example to 60 seconds) at least one old TTL before the move, make the change, confirm it, then raise it again. A cache honours the TTL it was handed, not the new one.
Why is DNS round robin a weak load balancer?
It knows nothing about server load or health unless the provider adds checks, clients and resolvers cache answers, and one big resolver can send many users to the same address for a whole TTL.
What limits the accuracy of GeoDNS?
The authoritative server sees the recursive resolver's IP address, not the user's. EDNS Client Subnet helps when the resolver sends it, and some public resolvers choose not to.
What is anycast?
The same IP address announced by BGP from many locations. Each packet goes to the nearest announcing site by routing distance. Root servers, public resolvers and CDNs use it.
Roughly how long does DNS failover take, and why?
Detection time (check interval times failure threshold) plus up to one TTL, so a minute or two at best. Open connections, client-side caches and resolvers that stretch TTLs add a long tail.