System Design Fundamentals
Vote

0% completed

​

DNS Resolution Process

What DNS Resolution Is

Two Ways to Ask: Recursive and Iterative Queries

Recursive Query

Iterative Query

Both Styles in One Lookup

Caching

TTL: How Long an Answer Is Kept

Why Some Customers Saw the Old Server

Changing an IP Address Safely

The TTL Trade-off

Negative Caching

Keeping Resolution Safe

Key Takeaways

Practice Questions

A team moves its website to a new server at 10:00 in the morning. They update the DNS record, test the site, and everything works.

But for the rest of the day, some customers still reach the old server. Other customers see the new one. Nothing is broken. DNS is doing exactly what it was designed to do.

To understand why, we need to look at how a DNS lookup actually runs. This lesson explains the two ways a DNS server can be asked a question. It also covers the caches that make most lookups fast, and the rules that decide how long an answer is kept.

What DNS Resolution Is

DNS resolution is the process of converting a domain name into its IP address. The previous lesson showed who takes part: the stub resolver on your device, a recursive resolver, and the root, TLD, and authoritative servers.

Two things make resolution fast enough that users rarely notice it.

  • How queries are asked. Different servers do different amounts of the work.
  • Caching. Most answers are stored, so most lookups finish without asking many servers.

Two Ways to Ask: Recursive and Iterative Queries

A DNS query comes in two styles. The difference is who is responsible for finding the answer.

Recursive Query

In a recursive query, the client asks the server for the complete answer.

If the server has the answer, it replies. If it does not, the server takes on the job of contacting other DNS servers until it finds the answer, and only then replies. The client just waits.

Recursive queries put more responsibility on the server.

Iterative Query

In an iterative query, the resolver asks the server for the best answer it has right now.

If the server does not have the complete answer, it replies with a referral, which tells the resolver about another server that knows more. The resolver then sends a new query to that server. It repeats this until it gets the complete answer.

Iterative queries put more responsibility on the resolver.

Authoritative servers usually answer iteratively. When asked about a subdomain they do not manage, they reply with a referral to the name server that does.

Both Styles in One Lookup

A single lookup uses both styles, as shown below.

  1. Your device's stub resolver sends one recursive query to its recursive resolver, like 1.1.1.1: "Give me the full answer for www.example.com."
  2. The recursive resolver sends an iterative query to a root server. The root server replies with a referral to the .com TLD servers.
  3. The resolver sends an iterative query to a .com TLD server. The TLD server replies with a referral to the authoritative servers for example.com.
  4. The resolver sends an iterative query to an authoritative server, which replies with the IP address.
  5. The recursive resolver returns the full answer to your device.
The device sends one recursive query to its resolver, which then sends iterative queries to the root, TLD, and authoritative servers
The device sends one recursive query to its resolver, which then sends iterative queries to the root, TLD, and authoritative servers
Recursive queryIterative query
What is asked forThe complete answerThe best answer available now
If the server does not knowIt finds the answer itselfIt returns a referral
Who does the extra lookupsThe serverThe resolver
Typical useA device asking its resolverA resolver asking root, TLD, and authoritative servers

Caching

DNS caching means storing the results of earlier queries, so the same question can be answered again without asking other servers.

When a resolver receives a query, it checks its cache first. If a fresh answer is there, it replies immediately, without contacting any other server. This saves time and reduces network traffic. It is also why most lookups never reach a root server.

Caches exist at several layers, from nearest to farthest.

  1. The browser keeps its own short-lived cache of recent lookups.
  2. The operating system keeps a cache, used by the stub resolver.
  3. The home router often acts as a caching-only resolver for every device in the home.
  4. The recursive resolver keeps a large cache shared by all of its users. A popular resolver has almost every common domain name cached already.

The lookup stops at the first layer that has a fresh answer.

A lookup checks the browser cache, then the operating system cache, then the recursive resolver, and asks the authoritative servers only if no cache has a fresh answer
A lookup checks the browser cache, then the operating system cache, then the recursive resolver, and asks the authoritative servers only if no cache has a fresh answer

Here is the difference in numbers. An answer from a cache usually takes a few milliseconds or less. A full lookup with no cached data needs several round trips to the root, the TLD, and the authoritative servers. That can take from tens to hundreds of milliseconds.

TTL: How Long an Answer Is Kept

A cached answer cannot be kept forever. If it were, a website could never move to a new IP address.

Every DNS record has a Time To Live (TTL) value, which specifies how long the record should be stored in a cache. TTL is measured in seconds. When the TTL runs out, the cached copy is removed, so outdated answers are not served forever.

For example, a record with a TTL of 3600 may be cached for one hour. A record with a TTL of 300 may be cached for five minutes.

Why Some Customers Saw the Old Server

This explains what happened at the start of the lesson. The team's record had a TTL of 86400, which is one day. Resolvers that looked up the site just before 10:00 cached the old IP address for up to 24 hours. Until those copies expired, their users kept reaching the old server.

People often call this delay DNS propagation. But the new record does not travel anywhere. The delay comes only from old cached copies whose TTL has not expired yet.

Changing an IP Address Safely

TTL is a direct control on how quickly a change reaches users. Teams use it like this when they move a service to a new IP address.

  1. Lower the TTL in advance. For example, change it from 86400 to 300. Do this at least one old TTL (here, one day) before the move, so every cached copy with the long TTL has expired.
  2. Change the IP address in the record. Cached copies of the old address now expire within five minutes, so resolvers pick up the new address quickly.
  3. Keep the old server running for a while, for any users who still have the old address.
  4. Raise the TTL again once the move is complete, to reduce the number of lookups.
To move a service safely, lower the TTL well before the change, change the IP address, then raise the TTL again
To move a service safely, lower the TTL well before the change, change the IP address, then raise the TTL again

Some resolvers and applications keep answers a little longer than the TTL says. So a team should always expect some delay after a change.

The TTL Trade-off

  • A short TTL means changes reach users faster, but resolvers must ask the authoritative servers more often. That adds load and a little latency.
  • A long TTL means fewer lookups and faster responses, but changes take longer to reach everyone.

Many records use a TTL between five minutes and one day. Records that may need to change quickly, like the address of a service that can fail over, usually use short TTLs.

Negative Caching

Caching an answer is useful. Caching the fact that there is no answer is useful too.

Negative caching means caching the fact that a domain name or record does not exist.

When a resolver is asked for a name that does not exist, it asks the root, TLD, and authoritative servers as usual. The authoritative server replies with NXDOMAIN, the response code that means "this name does not exist". The resolver stores that negative answer. The next time someone asks for the same name, the resolver answers from its cache, instead of asking all the servers again.

How long a negative answer is cached is set by the zone's SOA record (start of authority), which holds settings for the whole zone.

The first lookup of a name that does not exist asks the servers and caches NXDOMAIN, so later lookups are answered from the cache
The first lookup of a name that does not exist asks the servers and caches NXDOMAIN, so later lookups are answered from the cache

This matters more than it seems. Typos, old links, and badly configured apps create many requests for names that will never exist. Without negative caching, every one of those requests would go through the full lookup. With negative caching, they stop at the first resolver. This reduces the load on DNS servers and avoids repeated failed lookups.

Keeping Resolution Safe

Caching creates a risk. Suppose an attacker puts a fake answer into a resolver's cache. Every user of that resolver is then sent to the wrong address until the TTL expires. This attack is called DNS cache poisoning, or DNS spoofing.

Two protections are common.

  • DNSSEC (DNS Security Extensions) adds digital signatures to DNS records. A resolver that checks the signatures can reject a fake record.
  • Encrypted DNS, like DNS over HTTPS (DoH) and DNS over TLS (DoT), encrypts the queries between your device and the resolver. Others on the network cannot read or change them.

HTTPS also helps. Even if DNS returns a fake address, the fake server cannot show a valid certificate for the real domain. So the browser shows a warning.

Key Takeaways

  • DNS resolution converts a domain name into an IP address, using a mix of recursive and iterative queries.
  • In a recursive query, the server takes responsibility for finding the full answer. In an iterative query, the server returns its best answer or a referral, and the resolver keeps asking.
  • A device sends one recursive query to its resolver. The resolver then sends iterative queries to the root, TLD, and authoritative servers.
  • Caches in the browser, the operating system, the router, and the resolver let most lookups finish in milliseconds.
  • TTL, in seconds, decides how long a record stays cached. Lower the TTL before changing an IP address, so the change reaches users quickly.
  • Negative caching stores the fact that a name does not exist, which stops repeated failed lookups.
  • DNSSEC protects against fake answers. Encrypted DNS stops others from reading your DNS queries.

Most of the speed of DNS comes from not asking at all, and TTL decides how long "not asking" is safe. The next lesson, DNS Load Balancing and High Availability, shows how DNS can also choose which server each user reaches.

Practice Questions

Try each question first, then open the answer.

1. Your laptop asks 1.1.1.1 for www.example.com. The resolver then contacts a root server, a .com TLD server, and an authoritative server. Which of these queries are recursive, and which are iterative?

<details> <summary>Show answer</summary>

The laptop's query to 1.1.1.1 is recursive. The resolver's queries to the root, TLD, and authoritative servers are iterative. The laptop asks for the complete answer, so the resolver takes responsibility for finding it. Each server the resolver asks returns its best answer, usually a referral, and the resolver keeps asking.

</details>

2. A record has a TTL of 86400. The team changes the IP address at 10:00 without lowering the TTL first. For how long might some users still reach the old server, and what should the team have done?

<details> <summary>Show answer</summary>

Up to 24 hours. A resolver that cached the record just before 10:00 may keep the old address for the full TTL of 86,400 seconds. That is one day. The team should have lowered the TTL, for example to 300, at least one day before the change. Then cached copies would have expired within about five minutes of the change.

</details>

3. Why not set every DNS record to a TTL of 5 seconds, so that changes always reach users quickly?

<details> <summary>Show answer</summary>

Because caching would almost stop working. Resolvers would have to ask the authoritative servers again every few seconds. That adds load on the DNS servers and extra latency for users, and it can raise costs with a DNS provider. Some resolvers also keep answers longer than a very short TTL anyway. Short TTLs are best for records that really need fast changes.

</details>

4. A badly configured app looks up the misspelled name api.exmaple.com 1,000 times per minute. How does negative caching help?

<details> <summary>Show answer</summary>

Only the first lookup goes to the DNS servers. The authoritative server answers NXDOMAIN, which means the name does not exist, and the resolver caches that answer. The other lookups are answered from the cache until the negative answer expires. This saves load on the servers and avoids almost all of the 1,000 failed lookups each minute.

</details>

5. An attacker tricks a resolver into caching a fake IP address for bank.example.com. What is this attack called, and what protects users against it?

<details> <summary>Show answer</summary>

DNS cache poisoning. DNSSEC protects against it, because signed records let the resolver detect and reject a fake answer. HTTPS also protects users. The attacker's server cannot show a valid certificate for bank.example.com, so the browser warns the user instead of connecting.

</details>
Vivek Ramaswamy

Vivek Ramaswamy

· 11 days ago

What if I randomly keep changing the request URL, such that all the requests are bogus, and increase the number of requests, can't I overwhelm the DNS system even though the negative caching exists. How does the DNS systems protect against such an attack

Ricardo Franco

Ricardo Franco

· 3 years ago

Negative caching is the process of caching the non-existence of a DNS record. When a resolver receives a query for a non-existent domain or record, it caches this information as a negative response, preventing repeated queries for the same non-existent resource. This reduces the load on DNS servers and improves overall performance.

How a resolver can prevent a query from arriving on it?

I think the resolver uses negative caching to prevent querying DNS servers for non-existent domains. In this case, the text should be "When a resolver receives a response for a non-existent domain". Am I correct?

Show 3 replies
Ricardo Franco

Ricardo Franco

· 3 years ago

Who decides when to use recursive or iterative queries?

Show 1 reply

Reading Progress

0%


Vote for new content

On This Page

What DNS Resolution Is

Two Ways to Ask: Recursive and Iterative Queries

Recursive Query

Iterative Query

Both Styles in One Lookup

Caching

TTL: How Long an Answer Is Kept

Why Some Customers Saw the Old Server

Changing an IP Address Safely

The TTL Trade-off

Negative Caching

Keeping Resolution Safe

Key Takeaways

Practice Questions