Skip to content
Tutorials Center
My learning

Topics

Lesson 46 of 79 ยท Application Layer: Names and Web

DNS: The Internet's Phone Book

Learn how the Domain Name System turns names into IP addresses: the hierarchy, recursive and iterative resolution, caching with TTL, and the main record types.

People remember names like example.com; routers forward packets by IP address. The Domain Name System (DNS) is the global, distributed database that translates one into the other. Before your browser sends its first HTTP request, before an email is delivered, before almost any connection is made, a DNS lookup happens. When DNS breaks, the Internet seems broken even though every link is fine.

DNS is not one big server. It is a worldwide hierarchy of servers run by thousands of organizations, plus caches everywhere that make it fast. This lesson explains who is in that hierarchy, how a lookup walks through it, why caching and the TTL matter so much, and what the common record types mean. You will run a small DNS of your own in Python.

A hierarchy of names

A domain name is read from right to left. In www.example.com. (the final dot is the invisible root), the labels form a tree:

flowchart TB
    ROOT["root (.)"] --> COM["com"]
    ROOT --> ORG["org"]
    ROOT --> UK["uk"]
    COM --> EX["example.com"]
    COM --> OTHER["other.com"]
    EX --> WWW["www.example.com"]
    EX --> MAIL["mail.example.com"]
    UK --> COUK["co.uk"]

Each dot in a name is a step down the tree. Control of each part is delegated to a different organization.

  • The root is served by 13 named server identities (a.root-servers.net to m.root-servers.net), each really a fleet of machines spread worldwide using anycast. Roots do not know every name; they only know who runs each top-level domain.
  • Top-level domains (TLDs) are com, org, net, country codes such as de or jp, and hundreds of newer ones. A TLD server knows which servers are responsible for each domain below it.
  • Authoritative servers hold the real records for a domain such as example.com. They give the final answer.

The tree is also an administrative structure. The owner of example.com can create www.example.com, mail.example.com or a.b.c.example.com without asking anyone; handing a subtree to another party is called delegation.

Who does the work: stub, recursive and authoritative

Three roles take part in a typical lookup:

Role What it does Example
Stub resolver The small client in your operating system. It asks one configured server and trusts the answer. Your laptop
Recursive resolver Does the full lookup on the client's behalf and caches results. Your ISP's resolver, or a public one such as 1.1.1.1 or 8.8.8.8
Authoritative server Owns the records of a zone and answers from them. The servers named in example.com's NS records

Your device learns its resolver's address from DHCP or from manual configuration.

Recursive versus iterative resolution

The client asks the recursive resolver one question and gets one final answer: this is a recursive query ("do all the work for me"). The resolver, if it does not already know the answer, then makes a series of iterative queries: it asks a server, and the server either answers or refers it to a better server ("I do not know, but ask these").

sequenceDiagram
    participant C as Stub resolver
    participant R as Recursive resolver
    participant Root as Root server
    participant T as .com TLD server
    participant A as example.com server
    C->>R: www.example.com A?
    R->>Root: www.example.com A?
    Root-->>R: ask the .com servers
    R->>T: www.example.com A?
    T-->>R: ask ns1.example.com
    R->>A: www.example.com A?
    A-->>R: 203.0.113.10
    R-->>C: 203.0.113.10

Three iterative queries behind one recursive query. A warm cache often skips the first two.

Why not make everything recursive? A server that does recursion for anyone needs lots of memory and can be abused for attacks, so the root, TLD and authoritative servers answer only iteratively. Only resolvers meant for clients allow recursion.

The program below is a miniature DNS: a root server, a .com TLD server and an authoritative server, each with only the knowledge that a real one has, and an iterative resolver that follows referrals. Run it, then read the trace. The addresses of the root and .com servers are real; the example.com data uses documentation addresses.

Python Iterative resolution with referrals and a CNAME

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at what happened:

  1. The root server did not know www.example.com, but it knew who runs .com and referred the resolver there. The TLD server did the same for example.com. Only the authoritative server returned data.
  2. www.example.com turned out to be an alias (a CNAME record) for web.example.com, so the resolver started a new lookup for the target. In practice the authoritative server usually sends the CNAME and the final address together when it holds both, saving a step.
  3. The name nothing.example.com produced NXDOMAIN, the reply meaning "this name does not exist". That differs from "the name exists, but has no record of this type", which is called NODATA.

Caching and TTL

Walking the whole tree for every lookup would overload the root and TLD servers and slow every page. So every resolver caches what it learns. Each record carries a TTL (time to live, in seconds) set by the zone owner: the resolver may reuse the answer until the TTL runs out and must then ask again.

Caches exist at several layers: the browser, the operating system, the router, and the ISP's recursive resolver. Delegation records (the NS records for com, for example) have very long TTLs, which is why resolvers rarely contact the root at all.

TTL is a trade-off the owner controls:

  • Long TTL (hours or a day): few queries, fast lookups, low load, but changes spread slowly.
  • Short TTL (30 to 300 seconds): changes spread quickly and failover is fast, but there are more queries and a bit more delay.

The simulation shows what a resolver does over time, and what happens when a site owner changes the address while the old answer is still cached.

Python A resolver cache and the TTL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For the five minutes after the change, some users still reached the old address, because their resolver had cached it and kept obeying the old TTL. This is why careful operators lower the TTL a day before a planned move, and raise it again afterwards.

Resolvers also cache negative answers (NXDOMAIN), with a lifetime taken from the zone's SOA record, so a typo does not trigger repeated full lookups.

Record types

A DNS answer is made of resource records. Each has a name, a type, a TTL and data. These are the ones you will meet most:

Type Meaning Example data
A Name to IPv4 address 203.0.113.10
AAAA Name to IPv6 address 2001:db8::10
CNAME Alias to another name web.example.com.
MX Mail servers for the domain, with a priority (lower is tried first) 10 mail.example.com.
NS Name servers that are authoritative for a zone ns1.example.com.
TXT Free text, used for SPF, DKIM and DMARC and ownership checks "v=spf1 mx -all"
PTR Address to name (reverse lookup) 10.113.0.203.in-addr.arpa.
SOA Zone metadata: primary server, serial number, refresh and negative-cache times
SRV Host and port for a named service 0 5 5060 sip.example.com.

A few rules are easy to forget. A CNAME cannot coexist with other records at the same name, so you cannot put one at the zone apex (example.com) where NS, SOA and usually MX records already live. An MX record must point to a name, never directly to an IP address. And a name may have several A records: resolvers return them all, and clients usually try them in order, which spreads load crudely (see DNS in practice).

Reverse lookups use a special tree: to find the name for 203.0.113.10, you ask for a PTR record at 10.113.0.203.in-addr.arpa., with the octets reversed, so that the hierarchy follows the delegation of address blocks.

What DNS runs on

DNS queries are normally sent in one UDP datagram to port 53 and answered with one datagram, which is faster than opening a TCP connection. If the answer is too big or the server asks for it, the client retries over TCP port 53. Zone transfers between servers always use TCP. Newer variants encrypt DNS, and are described in the next lesson.

Every message starts with a 16-bit ID that matches replies to queries. You will see in attacks and defenses why that ID alone is not enough protection.

Seeing DNS from your side

Command-line tools show all of this on a real network. The tools are covered in essential network tools; the output below is read-only sample output of dig, with the answer section trimmed to the essentials:

TEXT
$ dig www.example.com A

;; ANSWER SECTION:
www.example.com.   3600  IN  CNAME  web.example.com.
web.example.com.    300  IN  A      203.0.113.10
web.example.com.    300  IN  A      203.0.113.11

Read each row as name, remaining TTL, class (IN means Internet), type, data. Running the same command again after a few seconds shows a smaller TTL, proof that the answer came from the resolver's cache.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a root server typically return for a query about www.example.com?

    Root servers do not hold individual hostnames. They know which servers are responsible for each top-level domain and refer the resolver to them.
  2. 2. A resolver has cached an A record with a TTL of 300 seconds. The owner changes the record. What is the longest time some users may still see the old address?

    A cache may serve the record until its TTL expires. The new value appears only when the resolver asks again.
  3. 3. Which statement about CNAME records is true?

    A CNAME points a name at another name, and the resolver continues the lookup there. It cannot share a name with other records, so it does not fit at the zone apex.
  4. 4. What is the difference between a recursive and an iterative query?

    Stub resolvers send recursive queries to their resolver, which then sends iterative queries to root, TLD and authoritative servers, following referrals.
  5. 5. Which record type lists the servers that accept email for a domain?

    MX records name the mail servers and give each a priority. Lower numbers are tried first.

Summary

  • DNS is a distributed, hierarchical database mapping names to addresses and other data. The tree runs from the root to TLDs to authoritative servers.
  • A stub resolver asks a recursive resolver, which performs iterative queries and follows referrals, then caches the results.
  • Every record has a TTL. Caching makes DNS fast but means changes take up to one TTL to spread, so lower TTLs before planned changes.
  • Key record types: A, AAAA, CNAME, MX, NS, TXT, PTR, SOA and SRV. NXDOMAIN means the name does not exist.
  • DNS usually uses UDP port 53, with TCP for large answers and zone transfers.

Next, look at how zones are written and operated, and how DNS is secured, in DNS in Practice: Zones, Security and Privacy.