DNS: how shop.example becomes 203.0.113.10

Computers connect to numbers; people type names. Watch one question walk from your browser to the root and back, and see why the next lookup is much shorter.

Level: BasicWatch: 15 steps · ~75 secondsLook a name up yourselfRecall: 3 questions · 8 cardsCompare 4 caches · 5 recordsdig · Python

What you'll take away

  • Trace a lookup through the browser, the OS, a resolver, the root, the TLD and the owner
  • Tell a recursive resolver from an authoritative server, and who keeps which answer
  • Lower the TTL before moving a server, and know why a change takes about one TTL to land
  • Read A, AAAA, CNAME, MX and TXT records, and see what DNS really answers with dig

The 1-minute version

Tap a point and the picture below plays that step.

4Compare them all
shop.example A?nothing cached anywhere
browser
OS
resolver empty
root .
TLD .example
ns1A 203.0.113.10
t = 0 s (simulated)
browser → OS: shop.example A? → …browser → OS → …
Step 1 / 15 · step by step
0trips over the network
~0 mstime waited (model)
…the answer

The picture: your computer at the front left (the browser and the OS, two small houses), the resolver in the middle, and at the back the root, the .example TLD server and ns1, the name's own server in red. A light is one message; the bold line under the picture says what it carried once it lands. A teal slab on a roof is a kept answer and its bar the TTL left; the gold slabs are NS records the resolver remembers. Faint lines are trips already made.

Step 4 · Every option at once, on one clock

Compare them all: DNS

The same question, shop.example A?, with the answer cached in a different place. See how far it walks and how long it waits.

0.0 s
nothing cached anywherenetwork trips 0 · ~0 ms

Use it when The first time this resolver sees the name. Rare, because resolvers keep the TLD's servers a long time (often two days)

Skip it when Every request of a page: each new domain a page pulls from may wait for its own lookup

A model: each round trip is a round number picked for the picture, not measured. The code below asks real DNS servers running on this machine. Press any tile to see it large above, with when it fits.

Six stops, near to far

StopWho runs itWhat it does
browser cachethe browserremembers names it looked up lately, for at most their TTL
the OSyour computerreads the hosts file and its own cache, then asks the resolver it was given (usually by the router)
recursive resolveryour ISP, your company, or a public one like 1.1.1.1 or 8.8.8.8does the walk for you and keeps every answer for its TTL, for everyone who uses it
root servers13 names, run by 12 organizations, copied to more than a thousand placesknow only who runs each TLD
TLD serversthe registry: Verisign for .com, THNIC for .thknow which servers each domain under them uses
authoritative serverwhoever hosts the domain's DNSholds the real records and answers only for its own names

The answer comes back with a TTL in seconds. Until it runs out, the resolver, the OS and the browser may answer from their copy, which is why a changed record reaches people slowly. Before moving a server, lower the TTL at least one old TTL ahead.

The records you'll meet

TypeHoldsExample
Aan IPv4 addressshop.example → 203.0.113.10
AAAAan IPv6 addressshop.example → 2001:db8::10
CNAMEanother name this one is an alias ofwww.shop.example → shop.example
MXwhere the domain's email goes, lowest number first10 mail.shop.example
TXTtext: SPF, DKIM keys, proof you own the domain"v=spf1 mx -all"
NSthe servers that answer for a domainns1.shop.example

A CNAME can't share its name with any other record (RFC 1034), so the bare domain, which must hold SOA and NS records, can't be a CNAME. DNS hosts offer ALIAS or "CNAME flattening" for that.

Why UDP, and when TCP

A question and its answer usually fit in one UDP packet each, so a lookup is one round trip with no connection to open. When an answer is too big, the server marks it truncated and the resolver asks again over TCP; copying a whole zone uses TCP too. Every DNS server must support TCP (RFC 7766). EDNS(0) lets UDP answers grow past the old 512 bytes, and 1232 bytes is the usual size today.

What DoH hides, and what it doesn't

Classic DNS is plain text: anyone on the same Wi-Fi, and your ISP, can read every name you look up. DNS over HTTPS (RFC 8484) and DNS over TLS (RFC 7858) encrypt the trip to the resolver. The resolver still sees every name, the walk from it to the root and onward is still mostly plain, and the site's IP address is visible when you connect. DNSSEC is a different thing: it signs answers so changes can be detected, but hides nothing.

In real code

dig.sh
#!/bin/sh
# dig asks dns.py's three servers on 127.0.0.1, one at a time, the way a resolver walks: root, then the TLD, then
# the name's own server. +norecurse asks each one only what it knows itself (no recursion), which is how you see a
# referral. Against the real internet, `dig +trace shop.example` does this walk for you (every server on port 53).
# Run: sh dig.sh   (needs dig and python3; nothing leaves this machine)
# needs: dig python3
set -eu
tmp=$(mktemp -d)
python3 -I dns.py serve > "$tmp/ports" &
pid=$!
trap 'kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null || :; rm -rf "$tmp"' EXIT
i=0
until [ -s "$tmp/ports" ] || [ "$i" -ge 50 ]; do sleep 0.1; i=$((i + 1)); done
read -r root tld shop < "$tmp/ports"

q() { # server port, then dig's own arguments; tabs squeezed to one space
  port=$1; shift
  dig @127.0.0.1 -p "$port" +norecurse +noall "$@" | tr -s '\t' ' '
}

echo "root says: ask the .example servers"
q "$root" shop.example A +authority
echo "the .example server says: ask shop.example's server"
q "$tld" shop.example A +authority
echo "ns1.shop.example answers: it holds the records"
q "$shop" shop.example A +answer
q "$shop" www.shop.example A +answer
for t in AAAA MX TXT; do echo "$t: $(q "$shop" shop.example "$t" +short)"; done
Run: sh dig.sh (every server runs on 127.0.0.1 on your machine; no internet needed)It printed:
root says: ask the .example servers
example. 172800 IN NS a.nic.example.
the .example server says: ask shop.example's server
shop.example. 172800 IN NS ns1.shop.example.
ns1.shop.example answers: it holds the records
shop.example. 300 IN A 203.0.113.10
www.shop.example. 3600 IN CNAME shop.example.
shop.example. 300 IN A 203.0.113.10
AAAA: 2001:db8::10
MX: 10 mail.shop.example.
TXT: "v=spf1 mx -all"

dns.py's three servers on 127.0.0.1, and dig asking each with +norecurse, the way a resolver walks: the root points to .example, .example points to shop.example's server, and that one answers. Real servers all use port 53; here each has its own port. Really run; below is what it printed.

Common questions

What is DNS?

The system that turns a name like shop.example into the IP address machines really connect to. The data isn't in one place: it's split into layers, the root, TLDs like .com or .th, and each domain's own servers.

Read: six stops, near to far

What's the difference between a recursive resolver and an authoritative server?

The resolver does the asking for you, from the root down, and caches answers for everyone who uses it (your ISP's, or one like 1.1.1.1). An authoritative server holds a domain's real records, answers only for its own names and asks nobody else.

What's a TTL, and why does a DNS change take time?

The TTL is how many seconds each stop may keep an answer. Until it runs out, anyone holding the old answer keeps using it. Before moving a server, lower the TTL at least one old TTL ahead, then change the IP.

What are A, AAAA, CNAME, MX and TXT records?

A is an IPv4 address, AAAA an IPv6 one, CNAME says this name is an alias of another, MX says which server takes the domain's email, and TXT holds text such as SPF or a code proving you own the domain.

Table: the records you'll meet

Does DNS use UDP or TCP?

Mostly UDP port 53: questions and answers are small, one round trip does it, and there's no connection to open first. An answer too big to fit, or a whole zone being copied, goes over TCP.

Read: why UDP

Is DNS private? What does DoH change?

Classic DNS travels as plain text: others on the Wi-Fi or your ISP can see every name you look up. DoH (DNS over HTTPS) wraps the questions in HTTPS so nobody on the way can read them, but the resolver you send them to still sees every name.

How do I see what DNS really answers right now?

Use dig: dig shop.example A asks your resolver and prints the record with its remaining TTL; dig @ns1.shop.example shop.example A +norecurse asks the name's own server directly, past every cache. On Windows, nslookup does the job.

Code: dig asking a real server

Next lesson

TCP and UDPLevel: Intermediate

With the IP in hand, the browser connects; next, TCP's handshake, what happens when a piece is lost, and why DNS itself rides UDP.