DNS, part 1: how it actually works
There's an old sysadmin haiku.

It's funny because it's true. DNS sits underneath almost everything on the internet. It mostly works. Then something breaks: the website is "down", the servers are perfectly healthy, and somehow the requests aren't reaching the right place.
This is the first post in a short series on DNS. This post covers how it works. The next few are hands-on tutorials with dig, host and the Windows tools, then DNSSEC, and the last two cover how DNS gets attacked and what you can do about it.
What DNS is for
Computers talk to each other using IP addresses, like 192.0.2.10 or 2001:db8::10. People are rubbish at remembering those, so we use names like example.com instead. DNS, the Domain Name System, is the thing that turns one into the other.
You'll hear it called the phone book of the internet. That's fine as far as it goes, but it hides the interesting part. There isn't one phone book. There are millions of small ones, each owned by someone different, and a chain of referrals that tells you which one to look in. DNS is less a book and more a very polite game of "I don't know, but I know who does."
The cast
Four kinds of server do the work. It helps to meet them before watching them in action.
The stub resolver lives on your laptop or phone. It's a small piece of the operating system that doesn't do much on its own. It asks someone else and waits for the answer.
The recursive resolver does the legwork. It's usually run by your ISP, your company, or a public service like 1.1.1.1, 8.8.8.8 or 9.9.9.9. It takes your question, goes off to find the authoritative servers, gets the answer, then caches it so the next person who asks gets it straight away.
The authoritative servers hold the actual answers. The .com servers are authoritative for .com, and they know which servers are authoritative for example.com. Those, in turn, hold the records the owner of example.com has published. When people talk about "hosting DNS" for a domain, this is the bit they mean.
The root servers sit at the top of the tree. There are 13 named root server identities, a.root-servers.net through to m.root-servers.net, but each one is really a fleet of machines spread around the world behind a shared address. The root doesn't know where example.com is. It knows who runs .com.
A lookup, step by step
Here's what happens when you type www.example.com into a browser and nothing is cached anywhere.
- Your browser asks the stub resolver on your machine.
- The stub resolver asks your recursive resolver.
- The recursive resolver asks a root server. The root replies with a referral: "I don't know, try the
.comservers, here are their names and addresses." - The recursive resolver asks a
.comserver. That's another referral: "Tryns1.example.netandns2.example.org, they're authoritative forexample.com." - The recursive resolver asks one of those. This time it gets a real answer:
www.example.comis192.0.2.10, and you can keep that for 3600 seconds. - The recursive resolver caches the answer and hands it back to your machine, which hands it to the browser.
That sounds like a lot of round trips, and the first time it is. In practice the recursive resolver already has the root and .com referrals cached, so most lookups skip straight to step 5 or never leave the cache at all.
Quick sidebar: what if the nameserver lives inside the domain itself, like ns1.example.com for example.com? It's a chicken and egg problem, and the fix is glue. The .com servers hand over the nameserver's IP address along with its name, so the resolver doesn't have to ask.
Records
A zone is the set of records for a domain. Each record has a name, a type, a TTL and some data. You'll meet the same handful of types over and over.
A maps a name to an IPv4 address. AAAA does the same for IPv6. Four As because an IPv6 address is four times the size. Somebody was pleased with that.
CNAME says "this name is an alias, go and look up that other name instead". It's handy for pointing www at a hosting provider. It has one important rule: a name with a CNAME can't have any other records. That's why you can't put a plain CNAME on the bare domain, example.com itself, because the bare domain always needs SOA and NS records. Many providers offer a workaround with names like ALIAS, ANAME or CNAME flattening, which resolve the target behind the scenes and serve the result as an A record.
MX tells the world where to deliver email for the domain, with a priority number so you can have backups.
TXT holds free text, and these days it mostly carries proof of ownership and email security policy such as SPF and DMARC.
NS lists the authoritative nameservers for a zone.
SOA, the start of authority, holds the zone's housekeeping or administrative details. We'll come back to that one, because it matters more than it looks.
PTR goes the other way, from an IP address back to a name. That's reverse DNS, and it lives in a special tree under in-addr.arpa for IPv4 and ip6.arpa for IPv6.
CAA says which certificate authorities are allowed to issue certificates for your domain.
SRV tells clients which host and port to use for a particular service.
TTLs and caching
Every record has a TTL, a time to live, in seconds. It's the record's owner telling resolvers how long they're allowed to cache the answer before asking again.
This is where a lot of "DNS is broken" tickets come from. If your A record has a TTL of 86400 and you change it, some resolvers will keep handing out the old address for up to a day. Nothing's wrong. They're doing exactly what you told them to.
The trick is to plan ahead. A day or so before a migration, drop the TTL to something short like 300. Make the change, check it, then put the TTL back up once you're happy.
Failures get cached too. If a name doesn't exist, the resolver remembers that for a while as well, and the SOA record decides for how long. Create a record a minute after someone looked for it and they may still be told it doesn't exist.
Primary and secondary
Every domain should have more than one authoritative nameserver, and many registries insist on it. Take this site. Here are its NS records:
drewbell.net. 903 IN NS ns1.digitalocean.com.
drewbell.net. 903 IN NS ns2.digitalocean.com.
drewbell.net. 903 IN NS ns3.digitalocean.com.
Three nameservers, so if one falls over, the other two keep answering. There's a catch, though. All three belong to the same provider. That protects me from a server failing, but not from the whole provider having a bad day.
That's not a theoretical worry. On 21 October 2016, Dyn, one of the big DNS providers, was hit by a huge DDoS attack, much of it from the Mirai botnet: hacked cameras, video recorders and home routers that still had their factory passwords. It came in three waves across the day. Twitter, Reddit, Netflix, GitHub, Spotify and dozens more were unreachable for hours. Their servers were fine, but nobody could look them up. Wikipedia has the full story.
The fix is to list nameservers from more than one provider. Here are GitHub's NS records today:
github.com. 2722 IN NS dns1.p08.nsone.net.
github.com. 2722 IN NS dns2.p08.nsone.net.
github.com. 2722 IN NS dns3.p08.nsone.net.
github.com. 2722 IN NS dns4.p08.nsone.net.
github.com. 2722 IN NS ns-421.awsdns-52.com.
github.com. 2722 IN NS ns-520.awsdns-01.net.
github.com. 2722 IN NS ns-1283.awsdns-32.org.
github.com. 2722 IN NS ns-1707.awsdns-21.co.uk.
Four nameservers at NS1 and four at Amazon's Route 53. If one provider goes down, resolvers give up on its servers and ask the other provider's instead. The first lookup is a little slower, but the site stays up.
It's worth knowing there's no failover switch here. Resolvers don't treat that list as a main server and a backup. They're all equal. A resolver spreads its queries across all of them, usually favouring whichever answers fastest, and tries another one when a server doesn't reply. Every one of those nameservers answers real requests every day, not just in an emergency.
It also has to be in place before you need it. The NS records for your domain are cached for a long time, two days for .com, so adding a second provider in the middle of an outage helps slowly, if at all. Catchpoint's write-up of the Dyn attack says they had a secondary provider on standby, and their domain was still unreachable for 30 minutes until they brought it in. Standby isn't the same as listed.
So where do primary and secondary come in? The primary is where the zone is edited. It holds the master copy. The secondaries hold read-only copies and serve them to the world, often from a different provider entirely. Resolvers can't tell which is which.
Copying the zone across is called a zone transfer. A full copy is an AXFR. An incremental one, just the changes, is an IXFR. Transfers run over TCP, unlike most DNS queries, which use UDP. Some setups skip transfers altogether and push every change to each provider through its own API, which technically makes them all primaries. To a resolver, it looks exactly the same.
The SOA record is what keeps track of everything. Here's one:
example.com. 3600 IN SOA ns1.example.net. hostmaster.example.com. (
2026112601 ; serial
7200 ; refresh
3600 ; retry
1209600 ; expire
300 ; minimum
)
The serial is a version number for the zone. Every time the primary changes, the serial has to go up. A common convention is the date plus a two-digit counter, like the one above. Secondaries compare their serial with the primary's, and if the primary's is higher they fetch a fresh copy.
Refresh is how often a secondary checks for a new serial. Retry is how soon it tries again if the primary didn't answer. Expire is the dead man's switch: if a secondary can't reach the primary for that long, it stops answering for the zone, on the grounds that stale data is worse than no data. The last number is the one I mentioned earlier, how long resolvers should cache a "that doesn't exist" answer.
Waiting for the refresh timer is slow, so primaries also send a NOTIFY message to their secondaries when the zone changes. The secondary checks the serial and pulls the update straight away. Transfers should also be locked down, either to known IP addresses, or better, with TSIG, a shared secret that signs each transfer so a secondary knows it's talking to the real primary.
Some people go one step further and run a hidden primary. The primary isn't listed in the NS records at all and only talks to the secondaries. The public never queries it, which makes it a much smaller target.
When it goes wrong, it's usually one of three things. Someone edited the zone and forgot to bump the serial, so the secondaries never noticed. The secondary isn't allowed to transfer, so it quietly serves old data until expire, then stops altogether. Or the NS records at the registry, the parent zone, don't match the NS records in the zone itself. The next post shows how to spot all three in about a minute.
UDP, TCP and port 53
Most DNS happens over UDP on port 53. It's quick, one packet out and one back. Answers used to be capped at 512 bytes. EDNS, an extension that almost everything now supports, lets clients ask for bigger ones.
When an answer still won't fit, the server sets a truncated flag and the client retries over TCP, also on port 53. Zone transfers always use TCP.
In closing
DNS is a distributed set of small databases with a chain of referrals between them, and caching everywhere to make it fast. Most problems come down to one of a few things: a stale cache, a TTL that was longer than you thought, authoritative servers that disagree with each other, or every nameserver sitting with one provider.
In the next post, we'll stop talking about it and start poking it with dig. The whole series is on the DNS from the ground up course page.
It was DNS. It's always DNS.