DNS, step by step
You type a name, and your computer needs a number before it can connect. Here is every server it asks, in order, and why the second lookup costs almost nothing.
01 The question your computer asks
Computers connect to IP addresses, not names. DNS, the Domain Name System, turns a name into records. The record a browser wants first is an A record, an IPv4 address, or an AAAA record for IPv6.
Names are read from right to left. shop.example.com. ends in a dot that you normally leave out: the root. Below the root sits com, a top-level domain (TLD), then example.com, then shop. Each level knows only who is in charge of the level below it, which is why a lookup is a walk down this tree.
- the name we want
shop.example.com and 203.0.113.7 are example values from ranges reserved for documentation, so the walk below is a toy. The servers and steps are the real ones.
02 The servers involved
| Who | Where it runs | What it knows |
|---|---|---|
| Stub resolver | inside your OS | which recursive resolver to ask, plus a small cache |
| Recursive resolver | your ISP or office, or a public one such as 1.1.1.1 or 8.8.8.8 | nothing at first; it does the walk and caches what it learns |
| Root servers | 13 names, a. to m.root-servers.net, each run as many copies around the world | which servers handle each TLD |
| TLD servers | run by the registry, e.g. Verisign for .com | which servers are authoritative for each domain below it |
| Authoritative server | the domain owner’s DNS host | the actual records, e.g. shop.example.com → 203.0.113.7 |
Only the last one knows the answer. The root and TLD servers never look up anything for you; they just point to the next server. The recursive resolver does all the running around, and it remembers what it learns.
03 The first lookup, one message at a time
- answering or asking
- link in use
- just sent or cached
- has the answer
The browser needs the address of shop.example.com. Its own cache, the OS cache and the resolver’s cache are all empty.
Two details make real lookups a little different. Many resolvers now use QNAME minimisation: they ask the root only about com and the .com servers only about example.com, so each server learns no more of the name than it needs. And a busy resolver hardly ever reaches the root, because it already has the .com referral cached.
04 Caching on the way back
Every record carries a TTL (time to live) in seconds, set by whoever owns it. Each cache keeps an answer until its TTL runs out, then forgets it. After the walk above, the resolver holds three entries, each with its own clock.
example.com, so it goes straight to ns1.host.net..com, then the authoritative server, one network round trip each.Failures are cached too. If a name does not exist, the authoritative server answers NXDOMAIN, and resolvers keep that negative answer for a time set in the zone’s SOA record.
05 Watch it with dig
dig sends DNS queries and prints the raw replies. Ask a root server directly, with recursion turned off, and you get a referral: no answer, only the servers to ask next.
ANSWER: 0 and no aa (authoritative answer) flag mark a referral. 172800 is the TTL in seconds, two days. The ADDITIONAL section carries the IP addresses of those servers, called glue, so the resolver does not need another lookup to find them.
+trace makes dig act as the resolver and prints each referral on the way down. Running a plain dig twice through your normal resolver shows the cache: the TTL in the answer is lower the second time, because it is counting down in the resolver’s cache, and the Query time usually drops.
06 When DNS bites
Before you move a site to a new IP address, lower the record’s TTL (say from a day to 5 minutes) at least one old TTL ahead of the move. Otherwise resolvers keep handing out the old address for up to a day after you change it, and you can’t make them stop.
shop.example.com. 86400 IN AThe old address after a changeWhat people call “propagation” is caches expiring. Each resolver keeps the old record until its TTL runs out.status: NXDOMAINLooked up before it existedIf someone queried the name before you created it, resolvers may keep the “does not exist” answer for the SOA’s negative TTL.127.0.0.1 shop.example.comA local overrideOn most systems the hosts file (/etc/hosts) wins over DNS, so one machine can see a different address from everyone else.resolve once at start-upPrograms that never look againSome long-running programs and connection pools resolve a name once and keep the IP until restart, whatever the TTL says.;; flags: qr tcLarge answers need TCPA reply too big for UDP comes back truncated (the tc flag), and the client retries over TCP port 53. A firewall that blocks TCP 53 breaks those names.