AlgoViz

Introduction to DNS

easy 25 min

DNS is the distributed database that turns names such as www.example.com into addresses computers can connect to.

Video lesson · Chapter 1 of 6

Follow a lookup across authorities

0:00 / 4:29

DNS in 80 seconds

1:20Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Computers find each other by numeric address, such as 192.0.2.10. People remember names. The Domain Name System, or DNS, is the service that answers the question: which address belongs to this name?

It is not one big list in one place. It is a tree of names, and every branch is looked after by the organisation that owns it. The company that owns example.com runs the list for example.com, and nobody else has to be asked when that list changes.

The answers are called records. One kind holds an IPv4 address, another holds an IPv6 address, another names the mail server, and so on. Each record says how long it may be remembered.

Why it exists

Addresses change. A server is replaced, a provider is switched, a second data centre opens. If programs stored the address, each change would break them. If they store the name, the owner changes one record and everyone follows.

One central list could not keep up with the whole Internet, neither in size nor in the number of changes. Dividing the tree among its owners, and letting everyone cache answers for a while, is what made it grow.

Because every visit starts with a name, DNS is also the first place where traffic can be steered: to another server, another region or another company's network.

A short history

  1. Before 1983

    Every host on the network copied one file, HOSTS.TXT, from a central information centre. RFC 1034 describes why that stopped working.

  2. November 1983

    Paul Mockapetris publishes RFC 882 and RFC 883, the first description of domain names.

  3. November 1987

    RFC 1034 and RFC 1035 replace them. They are still the base of DNS today.

  4. April 1995

    RFC 1794 describes using DNS answers to spread requests over several servers.

  5. October 2003

    RFC 3596 settles the AAAA record for IPv6 addresses.

  6. 2010

    The root zone is signed, so DNSSEC can be checked from the top of the tree.

Words to know

Domain name
A list of labels separated by dots, read from right to left, such as www.example.com.
Zone
The part of the tree that one owner manages as a unit.
Record
One fact about a name: its type, its value and its TTL.
Resolver
The server that looks answers up for you and remembers them.
Authoritative server
A server that holds the real records of a zone.
TTL
Time to live: how many seconds an answer may be cached.
Delegation
A parent zone naming the servers that run a child zone.

How it works

Step through the idea, or press Play.

Step 1 of 3

Ask. A program asks a resolver for the records of a name

Understand it

Start here

Asking for a house by name

Imagine a village where every house has a number. You want to visit Noor, but you only know the name. So you ask the village clerk, who keeps a book of names and house numbers. DNS is that book for the Internet.

Finding Noor's house
A name, no number349Where is Noor?

The name stayed the same while the house changed. That is the whole point of a name.

Why

Life without DNS, and with it

With DNS

  • You remember example.com
  • The same name works for IPv4 and IPv6
  • The owner changes one record and clients follow
  • Answers can differ by health, weight or place

Without DNS

  • You memorise 192.0.2.10 for every site
  • IPv6 is worse: 2001:db8::1 is a short one
  • The address changes and you lose access
  • There is nowhere to steer traffic before a connection starts

From a name to a connection

A browser asks a resolver for www.example.com. The resolver asks, in order, a root server, a com server and the authoritative server of example.com, gets the address 192.0.2.10 and returns it. The browser then connects to the web server directly.
The resolver does the walking. Once the browser has the address it talks to the web server directly; DNS carries no page content.

How each step of that walk works is the subject of How DNS works. This page is about the database itself.

The tree

The DNS hierarchy

DNS is an upside-down tree. The root is at the top, and each level delegates responsibility for the names below it.

An inverted tree

com, org and net are generic top-level domains. Two-letter ones such as uk and io are country codes.

Anatomy of www.example.com.

PartCalledLooked after by
the final dotRoot (usually not typed)Root server operators
comTop-level domain (TLD)The com registry
exampleSecond-level domainWhoever registered it
wwwSubdomainThe same owner, in their own zone
Names are read from right to left. A label is at most 63 bytes and a whole name at most 255 (RFC 1035).

Parts

Who does what

  • Resolver

    Takes your question, finds the answer and caches it. Usually run by your Internet provider, your company or a public service.

  • Root server

    Knows which servers run each top-level domain. It never knows the final address.

  • TLD server

    Knows which servers run each domain under com, org, net, io or a country code.

  • Authoritative server

    Holds the actual records of a zone. It is the final source of truth.

One complete question, in seven steps

  1. 1

    User request

    The browser asks for the address of www.example.com.

  2. 2

    Local caches

    The browser and the operating system look at answers they already have. A home router may cache too.

  3. 3

    Recursive resolver

    The resolver checks its own cache and starts the lookup if needed.

  4. 4

    Root server

    It answers with the servers of com.

  5. 5

    TLD server

    It answers with the servers of example.com.

  6. 6

    Authoritative server

    It answers with the address itself, 192.0.2.10.

  7. 7

    Response cached

    The resolver and the machines below it keep the answer for its TTL.

Data

Record types

TypeSaysExample
AThe IPv4 address of a nameexample.com is 192.0.2.10
AAAAThe IPv6 address of a nameexample.com is 2001:db8::10
CNAMEThis name is an alias of another namewww is an alias of example.com
MXThe mail server, with a preference number10 mail.example.com
TXTFree text: SPF, DKIM, DMARC, ownership proofsv=spf1 and the allowed senders
NSThe name servers of a zonens1.example.com
SOAFacts about the zone, such as its serial numberOne per zone
PTRAddress back to name (reverse lookup)192.0.2.10 is example.com
SRVHost and port of a servicePort 5060 on sip.example.com
CAAWhich authorities may issue certificatesOne named authority
A lower MX number is preferred. Two MX records with the same number share the mail.

Which record do I need?

Ownership

Zones and delegation

A zone is what one owner manages. A parent zone does not copy the child's records. It only holds NS records that say who runs the child. That is why you can change your own records without asking com, and why com must be told when you move to another DNS provider.

Each zone names the next one

The root zone delegates com to the com zone. The com zone delegates example.com to the zone of example.com, which holds the A, MX and TXT records and delegates the name lab.example.com to a separate zone run by another team.
An arrow is a delegation: NS records in the parent. Only the last zone holds the address.

Changing your DNS provider safely

Copy first, switch second, remove the old one last.

TTL

How long to remember

Choosing a TTL

TTLMeansUse it for
86,400 s24 hoursRecords that hardly ever change, such as NS
3,600 s1 hourOrdinary records
300 s5 minutesRecords you may have to change today
60 s1 minuteFailover, where a fast change is worth more queries
These are common choices, not rules. Route 53 recommends 60 to 172,800 seconds.

What a TTL costs

queries per second≤NTTL100,000 resolvers300 s≈333100,00060≈1,667\text{queries per second} \le \frac{N}{TTL} \qquad \frac{100{,}000\ \text{resolvers}}{300\ \text{s}} \approx 333 \qquad \frac{100{,}000}{60} \approx 1{,}667
Assumption: N independent caches all keep one record in use, so each asks at most once per TTL. Five times shorter means five times more questions.

How long the old answer can live

tlast old answer≤tchange+TTLold12:00+1 h=13:00t_{\text{last old answer}} \le t_{\text{change}} + TTL_{\text{old}} \qquad 12{:}00 + 1\ \text{h} = 13{:}00
The TTL that counts is the one that was handed out before the change. Lower it at least one old TTL in advance.

Uses

What teams do with DNS

  • Spread load

    Return several addresses for one name, often in changing order (round-robin DNS).

  • Geographic routing

    Give users in Europe the European address and users in America the American one (GeoDNS).

  • Mail safety

    SPF, DKIM and DMARC are TXT records that let receivers check who may send mail for a domain.

  • Joining a CDN

    static.example.com is a CNAME to a name that the content network controls.

  • Proving ownership

    A certificate authority or a service asks you to publish a TXT record only the owner can create.

  • Finding services

    Inside a cluster, names point at services instead of at single machines.

Round-robin DNS and a dead server

Plain DNS does not know that a server died. Either the client tries the next address, or a provider with health checks stops returning the dead one.

Limits

What DNS is bad at

Good at

  • Giving a stable name to a moving thing
  • Coarse steering between sites
  • Working at enormous scale through caching
  • Surviving the loss of some servers

Bad at

  • Instant changes
  • Even load: one resolver may stand for thousands of users
  • Knowing how busy a server is
  • Moving connections that are already open

For per-request decisions put a load balancer behind the name.

Public resolvers

Resolvers anyone may use

ServiceIPv4 addressNotes
Cloudflare1.1.1.1Does not pass your network on to the owner's server, with one documented exception
Google Public DNS8.8.8.8 and 8.8.4.4Supports EDNS Client Subnet
Quad99.9.9.9Run as a free security and privacy service

In code

Good habits

Do

  • Cache answers for their TTL, and no longer
  • Configure at least two resolvers
  • Back off and retry with limits when a lookup fails
  • Validate names that users give you
  • Measure how long resolution takes

Do not

  • Hard-code IP addresses
  • Ignore lookup failures
  • Set a TTL of 0: every request becomes a query
  • Block a hot path on a synchronous lookup
  • Trust an answer as proof of identity. Use TLS for that

Summary

The whole topic in one minute.

DNS maps names to records. The tree starts at the root, continues with top-level domains such as com, and is delegated label by label to the owner of each name, whose authoritative servers hold the truth.

Use A and AAAA for addresses, CNAME for aliases, MX for mail, TXT for proofs and policies and NS for delegation. Choose the TTL by asking how fast you need to change the answer, and remember that DNS steers new lookups only: it cannot move a connection that is already open.

  • DNS maps names to records through a tree of zones, each run by its owner.
  • Root, TLD and authoritative servers each know only the next step.
  • A and AAAA for addresses, CNAME for aliases, MX for mail, TXT for proofs, NS for delegation.
  • The TTL decides how long an old answer can survive a change.
  • DNS steers new lookups. Per-request decisions belong to a load balancer.

Sources