DNS in Computer Networks: How Name Resolution Works

How DNS turns names into IP addresses: the root, TLD and authoritative hierarchy, recursive and iterative lookups step by step, record types, caching and TTL.

What is DNS and how does it work?

DNS (Domain Name System) is the internet's distributed directory that translates domain names such as www.example.com into IP addresses. Your computer asks a recursive resolver, which asks a root server, then the top-level domain's servers, then the domain's authoritative server, and caches the answer for its TTL. DNS normally runs over UDP port 53, switching to TCP for large answers and zone transfers.

Computers route packets by IP address, but people remember names. DNS is the system that maps one to the other, and it runs every time you open a website, send an email or call an API. In the early ARPANET, every host downloaded one shared file of names and addresses; that stopped scaling, and DNS replaced it in the 1980s with a distributed, hierarchical, cached database (its core specification is RFC 1034 and 1035, from 1987). No single server knows every name, yet any name can be found in a handful of queries.

Why DNS is distributed

DNS does more than turn names into addresses. It also gives one server many names (aliases), tells senders where to deliver a domain's email (MX records), spreads load by returning different addresses to different clients, and holds verification text for services. A single central database would be a single point of failure, a traffic bottleneck and an administrative nightmare. So DNS splits the namespace into zones, each managed by its owner's authoritative name servers, and lets everyone cache answers.

The DNS hierarchy

Names form a tree read from right to left. A fully qualified domain name (FQDN) ends with a dot for the root: www.example.com.

roottop levelsecond levelhostswwwmailexamplecomenwikipediaorgindiagovin.www.example.com. from the right: . → com → example → www
The DNS namespace is a tree. Each label of a name is one level of the tree, read from the right: the unnamed root, then 3 top-level domains here, then the names under them. Each level delegates the one below, so the root only knows who runs com, and com only who runs example.com.
LevelExampleWho runs its servers
Root. (the empty label)12 organisations run the 13 root server identities, A to M
Top-level domain (TLD)com, org, in, ukRegistry operators; in is India's country-code TLD
Second-level domainexample.comThe domain owner, usually through a DNS hosting provider
Subdomain (host)www.example.com, mail.example.comThe domain owner

TLDs are generic (gTLDs: .com, .org, .net, and newer ones such as .app) or country-code (ccTLDs: .in, .jp, .uk). Each level delegates the level below it: the root zone lists the name servers for com, the com zone lists the name servers for example.com, and example.com's own servers hold its records. Each root identity is served from many places at once through anycast, where the same IP address is announced from many sites and routing picks the nearest.

Who takes part in a lookup

  • Stub resolver: the small DNS client in your operating system. It asks one question and expects a final answer.
  • Recursive resolver: the server that does the work, run by your ISP, your company or a public service such as 8.8.8.8 or 1.1.1.1. Its address usually arrives through DHCP.
  • Root, TLD and authoritative servers: each answers only for its own part of the tree.

Worked example: resolving www.example.com

Assume every cache is empty and the website's address is 203.0.113.10. The browser checks its own cache, then the operating system its cache and the hosts file; all miss, so the stub resolver asks the recursive resolver. A server that does not hold the answer returns a referral: the NS records of the servers one level down, plus their IP addresses (called glue) so the resolver can reach them.

Your PCResolverRootcom TLDexample.comA? www.example.comsame queryNS for comsame queryNS for example.comsame queryA 203.0.113.10A 203.0.113.10recursiveiterative
Resolving www.example.com from empty caches. Example: www.example.com, every cache empty
  1. Every cache is empty. Your computer's stub resolver sends one recursive query, recursion desired, to its resolver: give me the A record for www.example.com, the final answer and nothing less.
  2. The resolver starts at a root server. The root does not know the answer but returns a referral: the name servers for com, with their addresses (glue).
  3. It asks a com TLD server the same question and gets another referral, to the servers for example.com. Each of these queries is iterative: the server answers from what it holds and the resolver does the chasing.
  4. The zone's authoritative server, ns1.example.com, answers: www.example.com A 203.0.113.10, TTL 3600.
  5. The resolver caches the answer and both referrals, and returns the address to your computer, which caches it too: one question from your computer cost 3 queries upstream.
StepFromToReply
1BrowserBrowser and OS caches, hosts fileMiss
2Stub resolverRecursive resolverFinal answer, after steps 3 to 5
3Recursive resolverRoot serverReferral to the com servers
4Recursive resolvercom TLD serverReferral to the example.com servers
5Recursive resolverexample.com authoritative serverA record: 203.0.113.10, TTL 3600
6Recursive resolverStub resolverThe answer; both cache it

Many resolvers send each server only the part of the name it needs (QNAME minimisation), so the root sees "com" rather than the full name.

Recursive vs iterative queries

AspectRecursive queryIterative query
PromiseThe server must return the final answer or an errorThe server returns the best it has, often a referral
Who chases referralsThe server that was askedThe asker
Typical pairStub resolver to recursive resolverRecursive resolver to root, TLD and authoritative servers
Load on the server askedHigherLower
Header flagRD (recursion desired) setReferral returned without recursion
Recursive: each server chases for youaskerserver 1server 2server 3123456Iterative: the asker chases, step by stepasker123456server 1server 2server 3
Recursive and iterative queries. In a recursive query the asker sends 1 message and waits while each server asks the next, 6 messages in all. In an iterative one each server only refers, so the asker sends 3 queries itself. Your stub asks recursively; resolvers ask iteratively.

Root and TLD servers never resolve recursively; they would be overwhelmed.

DNS record types

TypeMapsExample
AName to IPv4 addresswww.example.com. A 203.0.113.10
AAAAName to IPv6 addresswww.example.com. AAAA 2001:db8::10
CNAMEAlias to canonical nameblog.example.com. CNAME www.example.com.
MXDomain to mail server, with a preference (lower is preferred)example.com. MX 10 mail.example.com.
NSZone to its authoritative name serversexample.com. NS ns1.example.com.
TXTName to free text: SPF, DKIM, site verificationexample.com. TXT "v=spf1 mx -all"
PTRAddress to name, for reverse lookups10.113.0.203.in-addr.arpa. PTR www.example.com.
SOAThe zone's administrative dataPrimary server, admin mailbox, serial, timers, negative-cache TTL

Two rules catch people out. A name with a CNAME may have no other records, so the zone apex (example.com itself, which must have SOA and NS) cannot be a CNAME. And MX and NS records must point to names with address records, not to aliases. Other types you may meet are SRV (the host and port of a service) and CAA (which certificate authorities may issue for the domain).

A small zone file

$TTL 3600
example.com.  IN SOA   ns1.example.com. admin.example.com. (
                       2026100501 ; serial
                       7200       ; refresh
                       900        ; retry
                       1209600    ; expire
                       300 )      ; negative-caching TTL
example.com.  IN NS    ns1.example.com.
example.com.  IN NS    ns2.example.com.
example.com.  IN MX    10 mail.example.com.
www           IN A     203.0.113.10
www           IN AAAA  2001:db8::10
blog          IN CNAME www.example.com.
mail          IN A     203.0.113.25
ns1           IN A     203.0.113.53
ns2           IN A     198.51.100.53

In the SOA, admin.example.com. is the mailbox admin@example.com. Secondary servers copy the zone from the primary and use the serial to tell whether it changed; the refresh, retry and expire timers (in seconds) govern how often they check.

Caching and TTL

Every record carries a TTL in seconds. The recursive resolver, the operating system and the browser may reuse an answer until its TTL runs out, and a cached referral lets the resolver skip the levels above it.

0 s1,000 s2,000 s3,000 s4,000 swww: 3 queriesmail: 1 querywww: 0 querieswww: 1 queryresolver's cacheTTL (s)left at 4,000 scom NS172,800168,800 sexample.com NS172,800168,800 swww.example.com A3,6003,600 smail.example.com A3,600200 s
A resolver's cache over an hour and more. Example: lookups at 0 s, 600 s, 1,800 s and 4,000 s; www and mail have TTL 3600
  1. At 0 s the cache is empty, so looking up www.example.com takes 3 queries: root, com and example.com. The answer and both referrals are cached, each with its own TTL.
  2. At 600 s mail.example.com is new, but the referral to example.com's servers is still cached, so the resolver goes straight there: 1 query instead of three.
  3. At 1,800 s www.example.com is asked again. Its A record has 1,800 of its 3,600 seconds left, so the resolver answers from memory without sending a query.
  4. At 4,000 s the record expired 400 seconds ago, so the resolver asks example.com's server again, still holding the referral, and the fresh answer starts a new hour.

With TTL 3600, a change to www's address can take up to an hour to reach users whose resolvers cached the old one. What people call "DNS propagation" is just caches expiring. Before moving a site to a new server, administrators lower the TTL (say, to 300 seconds) a day ahead, so the switch takes effect within minutes.

Failures are cached too: a "no such domain" answer (NXDOMAIN) is kept for the negative-caching time in the zone's SOA, so a typo does not hit the authoritative servers on every retry.

DNS over UDP and TCP, port 53

  • Queries and answers normally use UDP port 53: one small datagram each way, no handshake. The client retries if no answer arrives.
  • Classic DNS limited UDP answers to 512 bytes. A larger answer comes back truncated (the TC flag set) and the client repeats the query over TCP port 53. The EDNS(0) extension lets UDP carry larger answers.
  • Zone transfers from a primary to a secondary server (AXFR for the whole zone, IXFR for changes) use TCP port 53.
  • Encrypted variants hide queries from on-path observers: DNS over TLS (TCP 853) and DNS over HTTPS (port 443).
nslookup www.example.com      # quick answer from your configured resolver
dig www.example.com A         # full answer with TTL and header flags
dig +trace www.example.com    # follow the referrals from the root yourself
dig -x 203.0.113.10           # reverse lookup (PTR)

Classic DNS answers are not authenticated, which allows cache poisoning: an attacker races the real answer with a forged one so the resolver caches a wrong address. Random query IDs and source ports make guessing hard, and DNSSEC lets resolvers verify records with digital signatures. See network security basics.

Common mistakes

  • Saying the root servers know every domain's address: they only refer resolvers to the TLD servers.
  • Mixing up who sends which query: the stub's query is recursive, the resolver's queries are iterative.
  • Saying DNS uses only UDP: TCP is used for zone transfers and truncated answers.
  • Putting a CNAME at the zone apex or pointing an MX record at a CNAME.
  • Thinking a DNS change "propagates" by being pushed: old answers simply live in caches until their TTL expires.
  • Reading MX preference backwards: the lowest number is tried first.

Interview questions

Walk me through what happens when a domain name is resolved. The browser and OS caches are checked first. On a miss, the stub resolver asks the recursive resolver, which asks a root server (referral to the TLD servers), a TLD server (referral to the domain's authoritative servers) and finally an authoritative server, which returns the record. Every party caches the answer for its TTL.

What is the difference between an authoritative server and a recursive resolver? An authoritative server holds the original records for a zone and answers only for that zone. A recursive resolver holds no zones of its own; it finds answers on behalf of clients by following referrals, and caches what it learns.

Why can a DNS change take hours to show up? Resolvers and clients keep cached copies of the old record until its TTL expires. If the TTL was a day, some users keep seeing the old address for up to a day. Lowering the TTL before a planned change shortens that window.

What is an MX record, and what does its preference number do? An MX record names the mail server that accepts email for a domain. A domain can list several; senders try the one with the lowest preference number first and fall back to higher numbers if it is unreachable.

When does DNS use TCP instead of UDP? For zone transfers between primary and secondary servers, and when an answer is too large for a UDP response: the server sets the truncation flag and the client repeats the query over TCP. DNS over TLS also runs on TCP.

What is a reverse DNS lookup, and where is it used? It maps an IP address back to a name through a PTR record under in-addr.arpa (ip6.arpa for IPv6). Mail servers check that a sending server's address has a sensible reverse name, and logging and diagnostic tools show names instead of numbers.

What is DNS cache poisoning, and how is it prevented? It is tricking a resolver into caching a forged record, so its users are sent to an attacker's address. Defences include randomised query IDs and source ports, which make forged replies hard to match, and DNSSEC, which signs records so resolvers can verify them.

Next, read HTTP and HTTPS, and test yourself with the Computer Networks (Basic) skill test.

Common questions

What is the difference between recursive and iterative DNS queries?

In a recursive query the server asked must return the final answer, chasing it down itself; your computer sends this kind to its resolver. In an iterative query the server replies with the best it knows, often a referral to other servers, and the asker continues; resolvers query root, TLD and authoritative servers this way.

What is the difference between an A record and a CNAME record?

An A record maps a name directly to an IPv4 address. A CNAME record says a name is an alias for another name, so the resolver must then look up the target's A or AAAA record. A name that has a CNAME cannot have any other records, so a CNAME cannot sit at a zone's apex.

What is TTL in DNS?

TTL (time to live) is the number of seconds a resolver may cache a record before asking again. A TTL of 3600 means an answer can be reused for up to an hour. Long TTLs cut lookup traffic and delay; short TTLs let changes, such as a new server address, take effect sooner.

How many DNS root servers are there?

There are 13 named root server identities, a.root-servers.net to m.root-servers.net, run by 12 independent organisations. Each identity is served from many machines around the world using anycast, so the 13 names stand for far more than 13 physical servers.

What is a reverse DNS lookup?

It finds the name for an IP address using a PTR record. The address's octets are reversed under in-addr.arpa, so 203.0.113.10 is looked up as 10.113.0.203.in-addr.arpa. Mail servers use reverse lookups to judge senders, and logs use them to show readable host names.

Test yourself

← TCP and UDP · HTTP and HTTPS →