Sub topic 3
Finding the server: DNS
This chapter assumes you can read a URL and split it into scheme, host, port, and path. It also assumes you can run commands in a terminal. Everything here concerns the host part of the URL, such as www.example.com.
Why the web needs DNS
Computers send packets to IP addresses, not names. A browser that sees https://www.example.com/ must turn www.example.com into an address like 93.184.216.34 (IPv4) or 2606:2800:220:1:248:1893:25c8:1946 (IPv6) before it can open a connection. The Domain Name System (DNS) is the distributed database that answers this question.
DNS runs before TCP, TLS, and HTTP. A slow or failing lookup delays everything after it, and the page never starts loading.
The players in a lookup
A lookup involves several roles:
- Stub resolver: a small piece of code in the operating system or browser. It asks one configured server for an answer and does no searching itself.
- Recursive resolver: a server, usually run by your ISP, company, or a public provider, that does the searching for you and caches what it finds.
- Root servers: they do not know
www.example.com. They know which servers handle each top-level domain such as.com. - TLD servers: they know which servers are responsible for each domain under that TLD, such as
example.com. - Authoritative servers: they hold the actual records for a domain and give the final answer.
The domain name is read right to left. www.example.com. has an invisible trailing dot for the root, then com, then example, then www. Each level delegates to the next.
Most lookups do not follow this full path. The resolver usually already has the .com referral cached, and often has the final answer too.
Record types
A DNS answer is a resource record. Each record has a name, a type, a time to live (TTL), and data. These types matter most for web work:
| Type | Purpose | Example data |
|---|---|---|
| A | Name to IPv4 address | `93.184.216.34` |
| AAAA | Name to IPv6 address | `2606:2800:220:1::1` |
| CNAME | Alias to another name | `www.example.com` points to `example.com.cdn.net` |
| NS | Which servers are authoritative for a zone | `ns1.example.com` |
| MX | Where to deliver email | `10 mail.example.com` |
| TXT | Free-form text, used for domain verification and email policy | `v=spf1 ...` |
A CNAME says "this name is an alias; look up that name instead." The resolver follows the chain until it reaches A or AAAA records. A CNAME cannot coexist with other records at the same name. That is why you usually cannot put a CNAME on a bare domain like example.com, which must also carry NS and SOA records. Some DNS providers offer non-standard "ALIAS" or "flattened CNAME" features to work around this.
TTL and the layers of caching
Every record carries a TTL in seconds. It tells caches how long they may reuse the answer without asking again. A lookup can be answered from several caches, checked in roughly this order:
- The browser's own DNS cache.
- The operating system's cache, after the hosts file is consulted.
- The recursive resolver's cache.
A cache hit costs microseconds or a few milliseconds. A full recursive walk can cost tens to hundreds of milliseconds.
Caching has a consequence you will meet in practice. When you change a record, caches keep serving the old value until their copy expires. If a record had a TTL of 86400 (one day), some users may see the old address for up to a day. Before a planned migration, lower the TTL well in advance, wait for the old TTL to pass, make the change, and raise the TTL afterwards.
Failures are cached too. A resolver that learns a name does not exist may remember that for a while (negative caching, governed by the zone's SOA record). A name you just created can therefore still appear missing for a short time.
Inspecting DNS with dig
The dig tool queries DNS directly. These commands work on Linux and macOS, and dig is available on Windows through BIND tools or WSL:
# Full answer section for the A record
dig example.com A +noall +answer
# Just the values
dig example.com AAAA +short
dig example.com MX +short
# Ask a specific resolver instead of the system default
dig @1.1.1.1 example.com A +noall +answer
# Walk the delegation chain from the root, as in the diagram
dig +trace www.example.comIn the +answer output, each line has the name, the remaining TTL, the class (IN), the type, and the data. Run the same command twice against a caching resolver. The TTL counts down, which shows you are reading a cached copy. The first command to try when a site is unreachable is dig +short yourname. An empty result means the problem is DNS, not HTTP.
Resolving names from code
Programs usually do not talk to DNS directly. They call the operating system, which applies the hosts file and system caching. Node.js exposes both paths, and they can give different answers. Save this as dns-demo.mjs so top-level await works:
import { lookup, resolve4 } from "node:dns/promises";
const host = "example.com";
// Uses the operating system resolver (hosts file, OS cache, system settings).
const viaOs = await lookup(host, { all: true });
console.log("lookup:", viaOs);
// Sends a DNS query for A records, bypassing the hosts file.
const viaDns = await resolve4(host, { ttl: true });
console.log("resolve4:", viaDns);Run it with node dns-demo.mjs. The lookup output lists addresses with a family of 4 or 6. The resolve4 output lists IPv4 addresses with their remaining TTL. If you add 127.0.0.1 example.com to your hosts file, lookup returns the loopback address while resolve4 still returns the real one. Browsers and curl follow the lookup path, so the hosts file affects them.
Common errors and what they mean
| Symptom | Likely cause |
|---|---|
| `NXDOMAIN` | The name does not exist. Check for a typo, a missing record, or a domain that has not been registered or delegated. |
| `SERVFAIL` | The resolver could not get a valid answer. Causes include broken authoritative servers, an expired DNSSEC signature, or a bad delegation. |
| `NOERROR` with an empty answer | The name exists, but not with that record type. Asking for AAAA on an IPv4-only host does this. |
| Old address after a change | A cache is still holding the previous record until its TTL runs out. |
| Works on one network, fails on another | Different resolvers, or split-horizon DNS that returns internal addresses only inside a company network. |
| Browser and `dig` disagree | The browser used its own cache, the hosts file, a VPN resolver, or DNS over HTTPS settings that `dig` bypasses. |
What DNS hands to the next step
When DNS succeeds, the browser holds one or more IP addresses for the host. It combines an address with the port from the URL, 443 for https and 80 for http by default. The next chapter covers how it opens a TCP connection to that pair.
Exercise
You change the A record for shop.example.com from 203.0.113.10 to 203.0.113.20. The old record had a TTL of 86400. An hour later, some users still reach the old server and others reach the new one. Why does this happen, and what should you have done before the change?
Show answer
Recursive resolvers and operating systems cached the old record and may keep serving it for up to 86400 seconds (24 hours) after they last fetched it. Users whose resolver cache had expired, or who use a different resolver, got the new address, so the population is split. Before the change, you should have lowered the TTL (for example to 300 seconds) at least one old-TTL period ahead, so caches would already be refreshing quickly. Then make the change, and raise the TTL again once the new address is stable.