Skip to content

DNS, step by step

BasicsUnder the hood

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 tree, read from the root down
DNS names (a small slice)
.root
comTLD
example.comdomain
shopA record
other.com
orgTLD
  • 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 answers what
WhoWhere it runsWhat it knows
Stub resolverinside your OSwhich recursive resolver to ask, plus a small cache
Recursive resolveryour ISP or office, or a public one such as 1.1.1.1 or 8.8.8.8nothing at first; it does the walk and caches what it learns
Root servers13 names, a. to m.root-servers.net, each run as many copies around the worldwhich servers handle each TLD
TLD serversrun by the registry, e.g. Verisign for .comwhich servers are authoritative for each domain below it
Authoritative serverthe domain owner’s DNS hostthe 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

Resolving shop.example.com with cold caches
Step 1 of 9
Who talks to whom
Your laptop
Resolver
Root
.com TLD
ns1.host.net
Messages so far
Empty
Resolver cache
Empty
  • 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.

0 messages

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.

The resolver’s cache after the first lookup
Resolver cache
com NS→a.gtld-servers.netTTL 2 daysexample.com NS→ns1.host.netTTL 2 daysshop.example.com A→203.0.113.7TTL 300 s
Same name within 300 sNo servers askedAnswered from the resolver’s cache, or even sooner from your OS or browser.
api.example.com nextOne server askedThe resolver still knows who serves example.com, so it goes straight to ns1.host.net.
Cold cacheThree servers askedRoot, .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.

Shell
dig +norecurse @a.root-servers.net com NS
Text
;; flags: qr; QUERY: 1, ANSWER: 0, AUTHORITY: 13, ADDITIONAL: 27
;; AUTHORITY SECTION:
com. 172800 IN NS a.gtld-servers.net.
com. 172800 IN NS b.gtld-servers.net.
...

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.

Shell
dig +trace example.com # do the whole walk yourself, from the root
dig example.com # run it twice and compare

+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.

07 Check yourself

Question

With every cache empty, which server gives the resolver the actual IP address of shop.example.com?

Question

An hour ago you changed an A record whose TTL was 86400. Some users still reach the old server. Why?

Question

Right after resolving shop.example.com, the same resolver is asked for api.example.com. Who does it ask?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?