Sub topic 5
Finding the server with DNS
This chapter assumes you know what a URL is, what an IP address looks like, and that a browser talks to servers over a network. It covers how the browser turns a host name such as example.com into an IP address. The next chapter uses that address to open a TCP connection.
Why DNS exists
Networks route packets by IP address. People remember names. The Domain Name System (DNS) is a distributed database that maps names to records, and the most common record maps a host name to an IP address.
The browser takes the host from the URL, for example example.com in https://example.com:8443/docs/intro. It cannot connect until it has an address for that host. If the URL already contains an IP literal such as http://93.184.216.34/, the browser skips DNS.
The name hierarchy
A host name is read right to left. Each dot-separated label is a level in a tree.
| Part of `www.example.com.` | Level | Run by |
|---|---|---|
| `.` (trailing dot, usually hidden) | Root | Root server operators |
| `com` | Top-level domain (TLD) | The TLD registry |
| `example` | Second-level domain | The domain owner, through a registrar |
| `www` | Subdomain or host | The domain owner |
No single server knows every name. Each level knows only which servers are responsible for the level below it. This is called delegation.
Who does the lookup
Three kinds of party take part.
- Stub resolver. A small client built into the operating system (and sometimes the browser). It sends one question and waits for a final answer.
- Recursive resolver. A server that does the work of finding the answer. It is usually run by your ISP, your company, or a public provider. It also caches answers.
- Authoritative name servers. Servers that hold the real records for a zone, such as the servers for
example.com.
The stub resolver does not walk the hierarchy itself. It asks the recursive resolver, and the recursive resolver asks the authoritative servers.
The lookup path
Before any network traffic, the browser and operating system check their own caches. Only on a miss does the query leave the machine.
The steps for a cold lookup of www.example.com are:
- The browser checks its own DNS cache.
- The operating system checks its cache and the local
hostsfile. A line inhostsoverrides DNS completely. - The stub resolver sends a query to the configured recursive resolver, with the "recursion desired" flag set.
- If the resolver has no cached answer, it asks a root server. The root does not know the address. It replies with a referral: the names and addresses of the
comTLD servers. - The resolver asks a TLD server. It replies with a referral to the authoritative servers for
example.com. - The resolver asks an authoritative server. It returns the answer, such as an
Arecord. - The resolver caches the answer, returns it to the stub resolver, and the browser gets the IP address.
In practice, the resolver usually already has the root and TLD server addresses cached, so most lookups need only one or two outbound queries.
Record types you will meet
| Type | Holds | Example use |
|---|---|---|
| `A` | An IPv4 address | `example.com` points to `93.184.216.34` |
| `AAAA` | An IPv6 address | Same host over IPv6 |
| `CNAME` | An alias to another name | `www.example.com` is an alias for `example.com` |
| `NS` | The name servers for a zone | Delegation to authoritative servers |
| `MX` | Mail servers | Not used for web requests |
| `TXT` | Free text | Domain ownership checks, email policy |
A browser normally asks for both A and AAAA. If both exist, it tries to pick the faster path using a technique called Happy Eyeballs, which starts connections to both address families with a short stagger and keeps whichever succeeds first.
A CNAME adds a step. The resolver must follow the alias and look up the target name. A CNAME cannot coexist with other records at the same name, which is why the bare domain (the zone apex) usually cannot be a CNAME.
TTL and caching
Every record carries a time to live (TTL), in seconds. Caches may keep the record for that long and no longer. A TTL of 300 means five minutes.
- A short TTL lets you change servers quickly, but it increases query load and lookup latency.
- A long TTL reduces lookups, but a changed address takes longer to reach every client.
When you move a site to a new IP, old answers can survive in browser, operating system and resolver caches until their TTL runs out. Lower the TTL before a planned migration, then raise it again afterward.
Failed lookups are cached too. A response that says the name does not exist (NXDOMAIN) is cached for a period set by the zone, so a name you just created may keep failing for a while.
Observing DNS with dig
dig queries DNS directly and shows what a resolver returns.
dig +short example.com A
dig example.com NSThe first command prints only the IPv4 addresses. The second prints the full response. In that output, look at these parts:
status:in the header.NOERRORis success.NXDOMAINmeans the name does not exist.- The
ANSWER SECTION, where each line has the name, the TTL, the class (IN), the type and the data. - The
Query time, which is short when a cache answered and longer when the resolver had to walk the hierarchy. - The
SERVERline, which shows which resolver answered.
Run the same dig twice. The TTL value in the answer counts down on the second run if the resolver cached it. That countdown is the cache at work.
To see the walk through the hierarchy yourself, add +trace. This makes dig act like a recursive resolver and query the root, TLD and authoritative servers in turn.
dig +trace example.comTo query a specific resolver, put its address after @:
dig @1.1.1.1 example.com AComparing two resolvers is a quick way to check whether a record change has propagated.
Where DNS shows up in the browser
The Navigation Timing API reports how long DNS took for the current page load.
const [nav] = performance.getEntriesByType('navigation');
console.log('DNS ms:', nav.domainLookupEnd - nav.domainLookupStart);If the answer came from a cache, this value is usually 0 or close to it. A large value on a first visit points to a cold resolver cache or a slow resolver. In the DevTools Network panel, the same time appears as the "DNS Lookup" segment in a request's Timing tab.
Common errors and gotchas
- Stale cache after a change. The record is correct at the authoritative server, but a cache still holds the old one. Check the TTL, and query with
dig @<authoritative-server>to see the source of truth. DNS_PROBE_FINISHED_NXDOMAINor "server not found". The name does not exist, is misspelled, or the zone is not delegated properly. Rundigand check forNXDOMAIN.hostsfile overrides. A forgotten entry makes the browser go to a different server thandigreports.digignoreshosts, so it can disagree with what the browser does.- Different answers by location. Large sites return different addresses depending on where the query comes from, so your answer may not match someone else's.
- Round-robin and multiple addresses. A name can have several
Arecords. Resolvers may rotate their order, and clients may try the next address if the first fails. - Unencrypted queries. Classic DNS travels in plain text over UDP port 53 (and TCP for large answers). DNS over HTTPS (DoH) and DNS over TLS (DoT) encrypt the query so that others on the network cannot read it. The resolver you choose still sees every name you look up.
What the browser has after DNS
The result of this stage is one or more IP addresses for the host. The port still comes from the URL, or from the scheme's default (443 for https, 80 for http). With an address and a port, the browser can open a TCP connection, which is the subject of the next chapter.
Exercise
You change the A record for shop.example.com to a new IP address, but some users still reach the old server an hour later. The record's TTL is 86400. Why does this happen, and what should you have done before the change?
Show answer
Resolvers and operating systems cached the old record, and a TTL of 86400 seconds allows them to keep it for up to 24 hours. Before the migration you should have lowered the TTL (for example to 300), waited at least one old-TTL period so caches adopted the short value, made the change, and raised the TTL again afterward.
Exercise
A site loads for you through curl --resolve but fails in the browser, and dig +short shows the expected IP. Name two DNS-related places on your own machine that could make the browser use a different address.
Show answer
The hosts file (which overrides DNS and is ignored by dig) and a stale browser or operating system DNS cache. The browser's own DoH settings pointing to a different resolver are also a valid answer.