Sub topic 11
Resolving a Name: Iterative Versus Recursive Queries and Caching
Prerequisites and the resolution goal
This chapter assumes that you know the layered network model, IP addresses, ports, latency, and basic Python. It also assumes that you know that DNS maps names such as www.example.com to resource records, such as IPv4 (A) or IPv6 (AAAA) addresses.
A program usually starts with a name, not an address. The operating system's resolver must find an address before the program can open a TCP connection. A typical lookup for www.example.com follows this path:
- The application asks the local stub resolver.
- The stub resolver asks a recursive DNS resolver.
- The recursive resolver contacts the root, top-level-domain (TLD), and authoritative name servers.
- The answer travels back to the application.
- The result is cached for its time to live (TTL).
import socket
answers = socket.getaddrinfo(
"example.com",
80,
type=socket.SOCK_STREAM,
)
for family, socktype, protocol, canonname, address in answers:
print(family.name, address)getaddrinfo asks the operating system's resolver. The resolver may answer from a local cache or contact a configured recursive resolver. The returned addresses can include both IPv4 and IPv6 results.
Iterative and recursive queries
A recursive query asks a server to obtain the final answer on the client's behalf. The client expects either an answer or an error. A stub resolver normally sends a recursive query to a recursive resolver.
An iterative query asks a server for the best information it currently has. If that server is not authoritative for the name, it returns a referral to another server. The requester then contacts the next server itself.
For www.example.com, an iterative lookup can work as follows:
- The resolver asks a root server for
www.example.com. - The root server refers it to the
.comTLD servers. - The resolver asks a
.comserver. - The TLD server refers it to the authoritative servers for
example.com. - The resolver asks an authoritative server.
- The authoritative server returns the record.
The root and TLD servers usually do not return the final address. They provide referrals. The authoritative server stores the zone's official records.
Caching and TTL
DNS caching avoids repeating the same lookup. A cache entry has a TTL measured in seconds. Each second, the remaining TTL decreases. When it reaches zero, the resolver normally refreshes the entry.
Caching can occur in several places:
- A browser or application cache.
- The operating system's cache.
- A recursive resolver's shared cache.
- Intermediate DNS infrastructure.
A cached answer can reduce delay and DNS traffic. However, it also means that a change may not be visible immediately. A record with a TTL of 300 can remain cached for up to five minutes after it was received. DNS also supports negative caching: a response saying that a name does not exist can be cached for a limited time.
| Query type | Typical result | Main use |
|---|---|---|
| Recursive | Final answer or error | Stub resolver to recursive resolver |
| Iterative | Answer or referral | Recursive resolver to DNS hierarchy |
| Cached | Previously stored answer | Avoids an upstream query |
GATE-style reasoning
For a first lookup with no useful cache, count the required DNS exchanges. For a cached lookup, count only the exchange needed to reach the cache, or zero if the client cache answers locally. Always distinguish DNS lookup delay from the later TCP connection delay.
Exercise
A stub resolver sends a recursive query to a resolver. The resolver has no cached answer. It contacts a root server, a TLD server, and an authoritative server. How many DNS request-response exchanges occur in total, assuming one exchange per server contact?
Show answer
Four exchanges occur: one between the stub and recursive resolver, followed by one each between the recursive resolver and the root, TLD, and authoritative servers.