HTTPfy
How DNS Works - A Beginner's Guide to Resolvers, Records and TTLs

How DNS Works - A Beginner's Guide to Resolvers, Records and TTLs

What DNS does

Computers connect to each other by IP address, such as 192.0.2.10 or 2001:db8::10. People type names like example.com. The Domain Name System (DNS) maps one to the other.

DNS also tells mail servers where to deliver your email, lets you prove domain ownership to services like Google Workspace, and lets CDNs send you to a nearby server. If you run a website or manage email for a domain, you will edit DNS records sooner or later, and it helps to know what happens after you hit save.

The servers involved

Four kinds of servers take part in a typical lookup.

  1. Stub resolver. This is the small DNS client built into your operating system. It doesn't search for answers itself. It asks a recursive resolver and waits.

  2. Recursive resolver. Your router, your ISP, or a public service such as Cloudflare (1.1.1.1), Google (8.8.8.8) or Quad9 (9.9.9.9) runs this server. It does the legwork of finding an answer and caches what it learns.

  3. Root and TLD servers. The root servers sit at the top of the hierarchy. They don't know where example.com lives, but they know which servers handle .com. There are 13 root server identities, lettered A through M, run by 12 operators. Each identity is a single IP address that hundreds of machines share through anycast, so the system adds up to about 2,000 server instances worldwide. The TLD servers for .com, .org, .uk and the rest know which name servers handle each domain under them.

  4. Authoritative name servers. These hold the real records for a domain. When you edit DNS at Cloudflare, Route 53 or your registrar, you edit the zone on that provider's authoritative servers.

A lookup, step by step

A laptop's DNS query hopping through root, TLD and authoritative name servers to reach the web server

Say you type shop.example.com into a browser with nothing cached.

  1. The browser checks its own cache, then asks the operating system. The OS checks its cache and the hosts file.
  2. On a miss, the stub resolver sends one recursive query to the configured resolver: "give me the final answer for shop.example.com."
  3. The resolver asks a root server. The root replies with a referral: "ask the .com servers, here are their addresses."
  4. The resolver asks a .com TLD server. That server refers it to the authoritative name servers for example.com.
  5. The resolver asks one of those authoritative servers and gets the A or AAAA record for shop.example.com.
  6. The resolver caches the answer for as long as its TTL allows and returns it to your machine. The browser opens a connection to that IP.

Steps 3 to 5 are iterative queries. Each server answers with the best thing it knows, either the record or a referral to a server closer to it. The resolver follows the referrals. Only the stub-to-resolver query in step 2 is recursive.

In practice the resolver skips most of these steps. It caches the .com referral for up to two days, so a busy resolver rarely talks to the root servers at all. A full uncached lookup takes tens of milliseconds. A cached one comes back in a few.

You can watch the whole chain yourself with dig +trace:

dig +trace shop.example.com

Record types you will run into

Stack of cards with icons for mail, web address, alias, verification and name server records

Each record has a name, a type, a TTL and a value. These six cover most of what site owners touch:

  • A holds an IPv4 address, such as 192.0.2.10.
  • AAAA holds an IPv6 address, such as 2001:db8::10.
  • CNAME makes the name an alias of another name, such as shops.myplatform.net.
  • MX names a mail server and its priority, such as 10 mail.example.com.
  • TXT holds free text. SPF, DKIM, DMARC and ownership checks all live here, for example v=spf1 include:_spf.google.com ~all.
  • NS lists the authoritative name servers for a zone, such as ns1.dnsprovider.com.

A few rules trip people up:

  • A name with a CNAME can't hold any other record. That's why you can't put a plain CNAME on the bare domain (example.com), which already needs NS and SOA records. Many DNS providers offer a workaround called ALIAS, ANAME or CNAME flattening.
  • An MX record must point to a hostname that has an A or AAAA record. It can't point to a CNAME or a bare IP.
  • DKIM keys are TXT records too. They sit under a name like selector._domainkey.example.com.

To check what a domain publishes right now, query it with dig:

dig example.com A +short
dig example.com MX +short
dig example.com TXT +short

Or use our DNS lookup tool from the browser.

Why changes take time to show up

Every record carries a TTL (time to live) in seconds. A resolver that caches a record with a TTL of 3600 keeps serving the cached copy for up to an hour. When you change the record, resolvers that already cached the old value keep handing it out until their copy expires. After that, the next query fetches the new value.

People call this "propagation," but the new record doesn't get pushed anywhere. Each resolver's cached copy expires on its own schedule. A few habits make changes painless:

  • Lower the TTL ahead of a planned change. If a record has a TTL of 86400 (one day), drop it to 300 at least a day before you switch IPs. After the move, raise it again.
  • Remember negative caching. If someone queried a name before you created it, resolvers may cache the "doesn't exist" answer. The SOA record's minimum field sets how long, often an hour.
  • Check more than one resolver. Your own machine may show stale data. Our DNS propagation checker queries resolvers in different regions at once.
  • Changing name servers is slower. When you move a domain to a new DNS provider, the .com servers hand out the NS change with a TTL of up to 48 hours.

Keeping DNS secure

DNS was designed without authentication, so attackers have tried to forge answers and poison resolver caches. Two separate fixes exist:

  • DNSSEC adds cryptographic signatures to records. A validating resolver checks the signatures and rejects forged answers. DNSSEC proves the answer is authentic. It doesn't hide your queries.
  • DNS over HTTPS (DoH) and DNS over TLS (DoT) encrypt the link between your device and the resolver, so someone on your Wi-Fi network can't read or tamper with your lookups.

On the management side, audit your zone once in a while. Delete records that point at services you no longer use. A CNAME left pointing at a deleted cloud bucket or app is a common way attackers take over subdomains.

What DNS does beyond websites

  • Email routing. Sending servers look up your MX records to find where to deliver mail, and receiving servers check your SPF, DKIM and DMARC TXT records to decide whether to trust it.
  • Load distribution. Publish several A records for one name and resolvers will hand them out in rotating order. This is round-robin DNS. It spreads load but has no idea whether a server is up.
  • Geo routing and CDNs. Managed DNS providers and CDNs answer with different IPs depending on where the resolver sits, so users in Tokyo and Berlin reach different servers.
  • Failover. Some providers run health checks and swap the answer to a backup IP when the primary fails. Failover only works as fast as your TTL allows, so keep TTLs short on records that use it.

When a site won't load or email bounces, DNS is one of the first places to look. Start with dig or a lookup tool, compare the answer to what you expect, and check the TTL before assuming a change failed.