Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Computer and Network Foundations

System Design HLD-01.04

DNS in depth

How a name becomes an IP address through resolvers, root, TLD and authoritative servers, what each record type does, and how TTLs trade failover speed for load.

Beginner 12 min Last reviewed

The big idea

Computers connect to IP addresses like 203.0.113.7; people type names like shop.example.com. The Domain Name SystemDomain Name System: the internet's distributed directory that turns names such as example.com into IP addresses and other records. (DNS) translates between them. It is often called the phonebook of the internet, but no single book exists. The directory is split across millions of servers, each responsible for one slice of the names, and heavily cached so that most lookups never travel far.

For system designers, DNS is more than plumbing. It is the very first step of every request, it decides which data center a user reaches, and it is a classic cause of outages. A wrong record or an expired domain can take a whole company offline while every server is perfectly healthy.

Think of asking a librarian for a book. The librarian does not know every book, but knows which floor holds which subject, the floor knows which shelf, and the shelf has the book. Next time anyone asks for that book, the librarian remembers.

How it works

Step 1: The four kinds of server

A lookup involves four roles:

  • Recursive resolver: run by your ISP, your company or a public service. It does the legwork for you and caches what it learns.
  • Root name servers: the top of the tree. They do not know example.com, but they know who runs .com.
  • TLD name servers: responsible for a top-level domainTop-level domain: the last part of a name, such as .com, .org or .uk. such as .com. They know which name servers are responsible for example.com.
  • Authoritative name servers: run by the domain owner or their DNS provider. They hold the actual records and give the final answer.

Step 2: Walk the tree

With nothing cached, a lookup takes these hops. Press Next to step through:

Resolving shop.example.com through the DNS hierarchy The browser asks a recursive resolver. The resolver asks a root server, which refers it to the .com TLD servers. The resolver asks the .com servers, which refer it to example.com's authoritative servers. The resolver asks them and gets the IP address, which it returns to the browser and caches. shop.example.com? ask .com servers ask example.com NS A 203.0.113.7 Browser Recursive resolver caches answers Root server . (dot) .com TLD server Authoritative example.com Figure 1. A full lookup with nothing cached: the resolver walks from the root to the authoritative server, then answers and caches the result for its TTL. Most real lookups stop at a cache long before the root.
  1. Ask the resolver The browser checks its own cache and the operating system's, then asks the recursive resolver.
  2. Root The resolver asks a root server. The root does not know the answer, but refers it to the servers for .com.
  3. TLD The .com servers refer the resolver to the name servers responsible for example.com.
  4. Authoritative example.com's authoritative server returns the record: shop.example.com is 203.0.113.7, valid for its TTL.
  5. Answer and cache The resolver returns the address to the browser, and both cache it. The next lookup for this name, by anyone using this resolver, is answered instantly.

The resolver also caches the referrals. After one lookup it knows where .com lives for days, so the root is rarely consulted. Caching happens in layers: the browser, the operating system's resolver, then the recursive resolver. Each keeps an answer for as long as its TTLTime to live: how many seconds a DNS answer may be cached before it must be looked up again. allows.

Step 3: Record types

A DNS zone holds several kinds of record:

  • A: name to IPv4 address. shop.example.com A 203.0.113.7
  • AAAA: name to IPv6 address.
  • CNAME: name to another name, an alias. www.example.com CNAME example.com. Often used to point at a provider, such as a CDN host name.
  • MX: which servers receive email for the domain.
  • TXT: free text, used to prove domain ownership and for email security settings.
  • NS: which name servers are authoritative for the domain.

Step 4: DNS as a traffic tool

Because the authoritative server chooses the answer, it can answer differently for different people:

  • Round robin: return several addresses and rotate them, spreading users across servers.
  • Geo routing: return the address of the region closest to the user's resolver.
  • Failover: health-check the servers and stop returning the address of one that is down.

The catch is caching. A changed answer only reaches clients when their cached copy expires, so DNS failover is never faster than the TTL.

Try it

This interactive needs JavaScript. The diagrams and text above cover the same idea.

Try this:

  1. At 5 minutes, all clients that respect the TTL have moved within 5 minutes of a change. Fine for most failovers.
  2. Move to 1 day. A bad record now lingers for up to a day, and you cannot recall it.
  3. Move to 30 seconds. Failover is quick, but the lookup rate is 10 times higher than at 5 minutes, and every one of those lookups adds latency for a user.

Going deeper

Lower the TTL before a migration

A common practice: a day or two before moving a service to new addresses, lower the record's TTL from hours to a minute or so. Once the old long TTL has expired everywhere, make the change; clients follow within a minute. Then raise the TTL again.

Anycast

Large DNS providers announce the same IP address from many locations at once, a technique called anycastAnnouncing one IP address from many locations, so the network routes each user to the nearest one.. The network routes each query to the nearest location, which makes DNS fast worldwide and absorbs attacks by spreading them out. CDNs use the same trick, and Global traffic: GeoDNS, anycast and GSLB covers it along with geo DNS.

Not everyone respects TTLs

Some resolvers and applications cache longer than the TTL says, and some clients hold connections open for hours. Treat DNS failover as gradual, and keep old servers running until traffic to them actually stops.

Trade-offs

OptionWins whenLoses when
Short TTL (30 s to 5 min) Records you may need to change fast: failover, migrations, blue-green switches Lookup load and extra latency for users whose caches expire often
Long TTL (hours to a day) Stable records: fewer lookups, faster typical page loads Mistakes and failovers, which take hours to reach everyone
DNS-based load balancing Spreading users across regions cheaply, before traffic reaches you Fine-grained or instant control: caching makes it coarse and slow

Common mistakes

  • Expecting a DNS change to take effect immediately. It takes up to the old TTL, sometimes longer.
  • Letting a domain or its DNS provider account lapse. The servers are fine, but nobody can find them.
  • Pointing a CNAME at the bare domain (example.com). The DNS rules do not allow a CNAME at the zone apex; providers offer special alias records instead.
  • Treating DNS as a health check. It cannot remove a failed server faster than the TTL.

In the wild

Cloudflare

What is DNS? (opens in a new tab)

Cloudflare's learning center walks through a lookup in eight steps when nothing is cached: the browser asks a recursive resolver, which asks a root server, is referred to the .com TLD server, is referred to the domain's name server, gets the IP address and returns it to the browser. It compares the resolver to a librarian, the root to an index, and the TLD server to a specific rack of books. It also describes the caching layers this lesson covers, from the browser to the operating system's stub resolver to the ISP's resolver, each keeping records for the time set by the TTL.

Check yourself

  1. Which server holds the actual A record for shop.example.com?

    Show answer

    C. example.com's authoritative name server Root and TLD servers only refer the resolver onward; the authoritative server holds the domain's records.

  2. You change an A record whose TTL is 1 hour. When will all clients that respect the TTL use the new address?

    Show answer

    B. Within about 1 hour Cached copies may be up to one TTL old, so after about an hour every respecting cache has refreshed.

  3. Which record makes www.example.com an alias for another name?

    Show answer

    C. CNAME A CNAME points one name at another name, which is then resolved in turn.

  4. Why is the root server rarely contacted in practice?

    Show answer

    B. Resolvers cache the referrals to TLD servers for a long time Resolvers cache where .com and other TLDs live, usually for days, so they skip the root.

Recap

  • DNS turns names into IP addresses through a resolver, root, TLD and authoritative servers.
  • Answers are cached at every layer for their TTL, so most lookups are instant.
  • A, AAAA, CNAME, MX, TXT and NS records cover addresses, aliases, mail, verification and delegation.
  • DNS can spread and steer traffic, but changes only spread as caches expire.
  • Short TTLs fail over fast but cost lookups; long TTLs are cheap but slow to change.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy