AlgoViz

How DNS works

medium 25 min

Resolution turns a name into an address by checking caches first and then following referrals from the root to the owner's servers.

Video lesson · Chapter 1 of 5

A name the laptop has never seen

0:00 / 3:34

A DNS lookup in 90 seconds

1:30Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

DNS resolution is the work of turning a name into an answer. Your computer does very little of it. It asks one server, the recursive resolver, and waits.

The resolver starts from what it has cached. If it knows nothing, it asks a root server, which says who runs the top-level domain. That server says who runs the domain, and the domain's own server gives the answer. Each server either answers or points to a server that is closer to the answer.

Why it exists

No server could hold every name, and no owner wants to ask a central office for each change. Referrals let each server know only its own part.

Walking the whole path for every page would be slow and would flood the root. So every answer, including "this name does not exist" and every referral, carries a time to live and is cached. Most lookups never leave the nearest cache.

DNS was designed without proof of who is answering and without privacy. Both were added later, and knowing which protection does what is a favourite interview question.

A short history

  1. November 1987

    RFC 1034 describes recursive and non-recursive service, referrals and caching.

  2. March 1998

    RFC 2308 defines how long a negative answer may be cached.

  3. March 2005

    RFC 4033 introduces the current DNSSEC, which signs records and deliberately does not hide them.

  4. January 2009

    RFC 5452 asks resolvers to use unpredictable source ports, which makes forged answers much harder.

  5. May 2016

    RFC 7858 specifies DNS over TLS, on port 853.

  6. October 2018

    RFC 8484 specifies DNS over HTTPS.

  7. March 2020

    RFC 8767 describes serving stale answers when the owner's servers cannot be reached.

  8. November 2021

    RFC 9156 describes sending each server only the part of the name it needs.

Words to know

Stub resolver
The small DNS client in your operating system. It asks and does not walk.
Recursive resolver
The server that does all the work and caches the result.
Referral
An answer that says: ask these servers instead.
Glue
The address of a name server, sent with the referral so that it can be reached.
NXDOMAIN
The name does not exist.
SERVFAIL
The resolver could not get a usable answer.
RRSIG
The DNSSEC signature over a set of records.
DoH, DoT
DNS over HTTPS and DNS over TLS: encryption between you and your resolver.

How it works

Step through the idea, or press Play.

Step 1 of 3

Check caches. Browser, operating system, perhaps a router, then the resolver

Understand it

Start here

Asking from station to station

Imagine you need the platform of the Lake train. You ask one person, the master of your home station. That master does not know, but knows whom to ask, and sends a message from station to station until somebody knows.

The Lake train
One questionHOME STATIONno note yetHEADBLUELAKELake train: platform?

Notice who sent every message: the home master. Asha asked only once.

The walk

Eleven steps for www.example.com

#StepWhat happens
1User requestYou type www.example.com
2Browser cacheThe browser looks at recent answers
3System cacheThe operating system looks at its hosts file and its cache
4Ask the resolver"What is the address of www.example.com?"
5Ask rootThe resolver asks a root server
6Root refers"Ask a.gtld-servers.net", a com server
7Ask comThe resolver asks that server
8com refers"Ask ns1.example.com", with its address
9Ask the ownerThe resolver asks ns1.example.com
10Answer192.0.2.10 with a TTL of 3600
11Cache and returnThe resolver keeps it and replies to you
Steps 5 to 9 are skipped as far as the caches allow. A busy resolver nearly always knows the com servers already.

The same walk as messages

With query name minimisation the resolver sends root only com, and com only example.com.

Two kinds of question

Recursive and iterative

One recursive question, three iterative ones

A client sends one recursive question to a recursive resolver, which does all the work. The resolver sends iterative questions to the root, com and example.com servers. Root and com reply with referrals and example.com replies with the answer.
The client wants the final answer. The authoritative servers only answer for their own part.
RecursiveIterative
In wordsGive me the final answerGive me the answer or point me closer
Who does the workThe resolverThe one who asks
ReplyAnswer or errorAnswer or referral
Used betweenClient and resolverResolver and authoritative servers
Good forSimple clients, one shared cacheServers that stay simple and cheap
It is not a choice of speed or safety. A normal lookup uses both.

Caches

Where answers are remembered

Caches in the order they are asked

A question passes the browser cache and the system cache inside your computer, then the home router, then a shared resolver used by many people, and only on a miss in all of them reaches the DNS servers.
Each miss goes one step further. The shared resolver is where one person's lookup helps everybody else.

Four cache layers

LayerKeepsFor how long
1. BrowserRecent lookups of this browserIts own rule, often about a minute
2. Operating systemLookups of every programThe TTL, if it caches at all
3. RouterLookups of the home or officeThe TTL; some only pass questions on
4. ResolverLookups of all its usersThe TTL, within its own limits
Durations in the browser depend on the browser and version. Do not design around them.

Is the answer still good?

Serving a stale answer during an outage is optional (RFC 8767). It trades freshness for staying up.

The average lookup

tˉ=h1t1+(1−h1)[h2t2+(1−h2)t3]0.8⋅0+0.2 [0.9⋅20+0.1⋅100]=5.6 ms\bar t = h_1 t_1 + (1-h_1)\left[h_2 t_2 + (1-h_2) t_3\right] \qquad 0.8\cdot 0 + 0.2\,[0.9\cdot 20 + 0.1\cdot 100] = 5.6\ \text{ms}
Assumed: 80% local hits at about 0 ms, then 90% resolver hits at 20 ms, and 100 ms for a full walk. Your numbers will differ.

No is an answer too

Negative caching

"This name does not exist" is cached as well. The time is the smaller of two numbers in the zone's SOA record: the record's own TTL and its last field, called minimum (RFC 2308). That is why a name you created a moment after asking for it can stay invisible.

How long a missing name stays missing

TTLnegative=min⁡(TTLSOA, MINIMUMSOA)min⁡(3600, 900)=900 sTTL_{\text{negative}} = \min(TTL_{SOA},\ MINIMUM_{SOA}) \qquad \min(3600,\ 900) = 900\ \text{s}
Create the record first and test second.

Three answers that are not addresses

ReplyMeansTypical cause
NXDOMAINThe name does not existTyping error, or an expired domain
NOERROR, no dataThe name exists, not with this typeAsked AAAA where only A exists
SERVFAILThe resolver failedServers unreachable, or a bad signature

Transport

UDP, TCP and size

Questions normally travel in one UDP packet to port 53. The original limit is 512 bytes. A longer reply is cut and marked truncated, and the client asks again over TCP. EDNS lets the client announce that it can take larger UDP replies. So both UDP and TCP port 53 must be open.

A reply that does not fit

Threats

What can go wrong on purpose

  • Cache poisoning

    A false record gets into a resolver's cache and misleads all its users until it expires.

  • Spoofing

    Somebody forges a reply that looks as if it came from the right server.

  • Denial of service

    Servers are flooded so that names can no longer be found.

  • Loss of privacy

    Plain DNS lets the network, for example your provider, read every question.

Which protection does what

ProtectionStopsDoes not stop
DNSSECFalse or changed recordsReading the question
DoT and DoHReading or changing it on the way to the resolverA resolver that lies, or that keeps logs
Random source portsGuessing a forged replyAn attacker who sees the traffic
Response rate limitingUse of your server as an amplifierLarge floods by themselves
Anycast and many serversLoss of one siteA mistake copied to all of them

Proof

How DNSSEC works

A chain of trust

The root zone, whose key is trusted, holds a DS record for the key of com. The com zone holds a DS record for the key of example.com. The example.com zone sends signed records to a validating resolver, which checks the whole chain.
DS is a fingerprint of the child's key, kept and signed by the parent.
  1. 1

    Sign the zone

    The owner signs each set of records with a private key.

  2. 2

    Publish the signature

    An RRSIG record travels with the answer, and the public key is in a DNSKEY record.

  3. 3

    Link to the parent

    The parent holds a DS record, a fingerprint of the child's key.

  4. 4

    Verify

    The validating resolver checks signatures up to the root key. If one fails, it answers SERVFAIL.

Change

Propagation is expiry

Nothing is pushed through the Internet when you edit a record. Your own servers change at once, and every cache keeps the old answer until its copy expires. What people call propagation is many clocks running down.

Three caches, one change at noon

The same change reaches users at different times, up to one old TTL later.

Tools

Troubleshooting

Shell
nslookup example.com            # short question and answerdig example.com +short          # only the valuesdig example.com MX +short       # another record typedig example.com @9.9.9.9        # ask one particular resolverdig +trace example.com          # walk root, TLD, owner yourselfdig example.com SOA             # serial and negative TTL
Reading tools. Names under example.com are safe to try.

Empty the local cache

SystemCommand
Windowsipconfig /flushdns
macOSsudo dscacheutil -flushcache and sudo killall -HUP mDNSResponder
Linux with systemd-resolvedresolvectl flush-caches
This empties your own machine only. Browsers and resolvers keep theirs.

Common problems

You seeLikely causeTry
NXDOMAIN in the browserThe name does not existSpelling, registration, negative cache
SERVFAILServers unreachable or DNSSEC brokenAnother resolver, then the owner's server
Slow first visitDistant or overloaded resolverMeasure, then a nearer resolver
Old server still usedAn old answer in a cacheCompare TTLs, flush, wait
Only some users failBroken AAAA or split DNSAsk for A and AAAA separately

Is it DNS?

Summary

The whole topic in one minute.

Clients ask recursively: give me the final answer. Resolvers ask iteratively: tell me the answer or who is closer. The order is browser cache, system cache, resolver cache, root, top-level domain, authoritative server, and each step stops as soon as something is known.

DNSSEC proves that records are genuine but hides nothing. DoH and DoT hide the question on the way to the resolver but do not prove the record. To debug, compare the answer from your resolver with the answer from the authoritative server, and look at the TTL.

  • The client asks once, recursively. The resolver asks many times, iteratively.
  • Order: browser, system, resolver, root, TLD, authoritative.
  • Answers, referrals and "does not exist" are all cached.
  • DNSSEC proves, DoH and DoT hide. They solve different problems.
  • Propagation is old answers expiring, one cache at a time.
  • To debug, compare the resolver's answer with the owner's.

Sources