How a name becomes an address

A packet needs an address, but people type names. DNS is a chain of questions, and every answer is kept in a cache until its time limit ends, so a changed record does not change everywhere at once.

Part 2 of 9 · Networking4 min read
example.com?ask the resolvernot the whole internetroot.comowner104.20.23.154a chain of officesonly the last gives the address104.20.23.154kept for the TTLthen asked againdig +trace example.comask each officedig +trace, step by steprecord changed?caches still say the old one

In part 1, every packet needed an address like 104.20.23.154. Nobody types that. You type example.com, and something must turn the name into a number before the first packet can leave. That something is DNS.

Back at the post office, Bub needs the address for a name. Bub does not carry a book of every name. Bub asks the clerk at the front desk, who is the resolver. If the clerk does not know, the clerk asks three other offices in turn. Then the clerk writes the answer in a notebook, so the next question needs no trip.

example.com?104.20.23.154resolvernotebook: keeps every answeruntil its TTL endsonly if the notebook is emptyother offices
The resolver asks other servers for Bub and keeps each answer in a notebook.

You ask a resolver

Your machine does not search the internet for a name. It sends the question to one server named in its settings, the resolver. Ask for example.com from a small container:

Shell
docker run --rm alpine sh -c 'apk add -q bind-tools &&
grep nameserver /etc/resolv.conf && 
dig +noall +answer example.com'  
nameserver 192.168.65.7
example.com.		130	IN	A	104.20.23.154
example.com.		130	IN	A	172.66.147.243

Here is what each marked part does:

  1. 1grep nameserver /etc/resolv.conf shows the resolver that the machine uses.
  2. 2dig is a tool that asks DNS questions. The image does not have it, so apk add bind-tools installs it.
  3. 3+noall +answer hides everything except the answer section.

The first line is the resolver, set in /etc/resolv.conf. The next two lines are the answer: two addresses for one name. The number 130 is the TTL, the seconds this answer may still be kept. The letter A in each line is the record type: an A record connects a name to an IPv4 address. In the next section we meet an NS record, which names the servers that run a domain.

The resolver asks a chain of offices

A resolver with an empty cache asks in a fixed order. dig +trace does the same steps itself and prints each one. We keep only the lines that matter. The server names can differ on your machine:

Which server finally gives the address, and which servers only say who to ask next?

Shell
docker run --rm alpine sh -c 'apk add -q bind-tools &&
dig +trace +nodnssec example.com | grep -E "^;; Received|^example.com"'  
;; Received 420 bytes from 192.168.65.7#53(192.168.65.7) in 2 ms
;; Received 836 bytes from 198.97.190.53#53(h.root-servers.net) in 95 ms
example.com.		172800	IN	NS	hera.ns.cloudflare.com.
example.com.		172800	IN	NS	elliott.ns.cloudflare.com.
;; Received 359 bytes from 192.41.162.30#53(l.gtld-servers.net) in 80 ms
example.com.		300	IN	A	172.66.147.243
example.com.		300	IN	A	104.20.23.154
;; Received 72 bytes from 108.162.192.162#53(hera.ns.cloudflare.com) in 99 ms

Here is what each marked part does:

  1. 1dig +trace follows the chain by itself and does not use the resolver's cache. The option +nodnssec hides signature records.
  2. 2grep -E "^;; Received|^example.com" keeps only the lines that start with ;; Received or with example.com.

Each Received line names the server that sent the records printed above it. The first line is our resolver, which hands over the list of root servers. A root server does not know example.com. It only knows who runs .com, and it says so. A .com server, l.gtld-servers.net, does not know the address either. It says that example.com is run by two servers at cloudflare.com. Only the last server, which owns the name, gives the address.

root serverexample.com?ask the .com servers.com serverexample.com?ask its own serversits ownerexample.com?104.20.23.154

The resolver asks a root server for example.com. The root server answers: ask the .com servers.

Only the last server is authoritative for example.com. The other two give directions. Every record in this chain carries its own TTL: two days for the list of servers that run example.com, five minutes for the address.

Every answer waits in a notebook

Asking three servers for every request would be slow, so the resolver keeps each answer until its TTL ends. This is the cache. A changed record does not change everywhere at once, because each cache keeps the old answer. One container can show this. It has a source that owns shop.test, a resolver with a cache in front of it, and a record that changes at the source.

The record changed at the source. What will the cache say?

Shell
docker run --rm alpine sh -c '
apk add -q bind-tools dnsmasq
D="dnsmasq -C /dev/null -k --no-resolv --no-hosts -z" 
$D -a 127.0.0.2 --address=/shop.test/10.0.0.1 --local-ttl=60 -x /tmp/a.pid & 
$D -a 127.0.0.1 --server=127.0.0.2 --cache-size=100 -x /tmp/c.pid &
sleep 1
dig +noall +answer shop.test @127.0.0.1 
kill $(cat /tmp/a.pid) 
$D -a 127.0.0.2 --address=/shop.test/10.0.0.2 --local-ttl=60 -x /tmp/a.pid &
sleep 6
dig +noall +answer shop.test @127.0.0.2
dig +noall +answer shop.test @127.0.0.1'
shop.test.		60	IN	A	10.0.0.1
shop.test.		60	IN	A	10.0.0.2
shop.test.		54	IN	A	10.0.0.1

Here is what each marked part does:

  1. 1dnsmasq is a small DNS server. We start two. The first listens on 127.0.0.2 and owns shop.test: this is the source. The second listens on 127.0.0.1, asks the source, and keeps the answers: this is the cache.
  2. 2--address=/shop.test/10.0.0.1 gives the name its address, and --local-ttl=60 sets the TTL to 60 seconds.
  3. 3dig ... @127.0.0.1 asks that one server directly.
  4. 4kill $(cat /tmp/a.pid) stops the source. We start it again with the new address 10.0.0.2.

The first answer is the old address, and the cache now keeps it. We then change the record at the source. Asking the source gives 10.0.0.2, but asking the cache still gives 10.0.0.1, with 54 seconds left. Users behind that cache reach the old server until the TTL reaches zero. A command that asks the source directly, as the second dig did, shows a change that these users do not see yet.

source10.0.0.1cache10.0.0.1TTL 60the cache keeps itsource10.0.0.2cache10.0.0.1TTL 54nobody tells the cachesource10.0.0.2cache10.0.0.1TTL 54the user getsthe old one

The cache asks the source and keeps 10.0.0.1 for 60 seconds.

Key points

  • A machine asks a resolver for the address of a name. It does not search the internet itself.
  • The resolver follows a chain: a root server, then the .com server, then the server that owns the name. Only the last one gives the address.
  • Every answer has a TTL, and the resolver keeps the answer until the TTL ends.
  • A changed record reaches users one cache at a time. Asking the source shows the change before the users behind a cache see it.
  • Lower the TTL before you change a record.

Next, in part 3: what happens after the address is known, when the machine opens a connection.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS