Sub topic 11
Resolving a Name: Iterative Versus Recursive Queries and Caching
Prerequisites and scope
This chapter assumes you know the layered network model and the basics of UDP and sockets. It also assumes you have seen the DNS hierarchy of root, TLD (top-level domain), and authoritative servers. Here we follow one lookup from start to finish. We compare the two query styles and look at how caching shortens later lookups. The chapter ends with the counting patterns that GATE questions use.
The actors in a lookup
A lookup involves four kinds of parties.
- The stub resolver is a small library inside the client's operating system. It asks one question and expects a final answer. In Python,
socket.getaddrinfocalls it. - The local (recursive) resolver is a name server run by an ISP, a company, or a public provider. It does the searching on the client's behalf and keeps a cache. It is also called the local DNS server or default name server.
- The root, TLD, and authoritative servers hold pieces of the hierarchy. A root server knows the TLD servers. A TLD server such as the one for
comknows the authoritative servers of each domain under it. An authoritative server holds the final records for its own zone.
import socket
for family, socktype, proto, canonname, sockaddr in socket.getaddrinfo(
"example.com", 80, type=socket.SOCK_STREAM):
print(family.name, sockaddr)This program asks the stub resolver for addresses. It never sees the root or TLD servers. All of that work is hidden behind the local resolver.
Recursive and iterative queries
A recursive query tells the server: "Get me the final answer, and do whatever work is needed." The server must return either the answer or an error. A server that receives a recursive query takes on the work of contacting others.
An iterative query tells the server: "Give me the best answer you have, even if it is only a referral." The server replies with either the answer or the address of a server that is closer to it. The asker then contacts that server itself.
In the common setup, the client sends one recursive query to its local resolver. The local resolver then sends iterative queries to the root, the TLD server, and the authoritative server.
Step-by-step trace
- The client's stub resolver sends a recursive query for
www.example.comto the local resolver (message 1). - The local resolver has nothing cached. It asks a root server (message 2). The root does not know the address. It replies with a referral: the names and addresses of the
comTLD servers (message 3). - The local resolver asks a
comTLD server (message 4). It replies with a referral to the authoritative servers forexample.com(message 5). - The local resolver asks the authoritative server (message 6). That server holds the A record and returns it (message 7).
- The local resolver caches the answer and passes it to the client (message 8).
With an empty cache, the client sees 2 messages (one query, one reply). The whole exchange involves 8 messages, 4 queries and 4 replies.
Fully recursive chain
A second style makes every server recursive. The local resolver sends a recursive query to the root. The root then sends a recursive query to the TLD server, which sends one to the authoritative server. Answers come back along the same path. This puts heavy load on the upper servers, which must hold state for each pending query. For that reason root and TLD servers usually refuse recursive queries from the public. They answer iteratively.
| Property | Recursive query | Iterative query |
|---|---|---|
| Who does the searching | The queried server | The asker |
| Reply when the server lacks the answer | Final answer or error | Referral to another server |
| Typical use | Client to local resolver | Local resolver to root, TLD, authoritative |
| Load on upper servers | High, if they accepted it | Low |
| Who ends up caching | Every server on the chain | The asker |
Caching and TTL
Caching is what keeps DNS fast and keeps the root servers from being overwhelmed. Each resource record carries a TTL (time to live), given in seconds. A resolver may keep the record until the TTL runs out. Then it must discard the record and ask again.
A resolver caches more than final answers.
- Final answers, such as the A record for
www.example.com. - Referrals, such as the NS records for
com. These are cached for a long time. After the first lookup, the root is rarely contacted again, because the resolver already knows thecomservers. - Negative answers. If a name does not exist (NXDOMAIN), the resolver can cache that fact for a period.
Because of this, a second lookup in the same domain is shorter. Suppose the local resolver has already resolved www.example.com. A request for mail.example.com can skip the root and TLD steps. The resolver knows the authoritative server for example.com from its cache, so it sends one query there.
| State of the local resolver's cache | Queries the resolver sends |
|---|---|
| Empty | Root, TLD, authoritative: 3 |
| NS records for `com` cached | TLD is skipped only if the `example.com` NS records are also known, otherwise TLD, authoritative: 2 |
| NS records for `example.com` cached | Authoritative only: 1 |
| Exact record cached and TTL not expired | None: 0 |
Caching also happens on the client side. The operating system and the browser often keep their own small caches. A lookup may therefore never leave the machine.
The cost of caching: stale data
A cached record can be out of date. If an administrator changes an address, resolvers that hold the old record keep serving it until the TTL expires. This is why administrators lower the TTL before a planned change. A short TTL gives fresher data but more queries. A long TTL gives fewer queries but slower propagation of changes.
DNS transport details
DNS normally runs over UDP port 53. A query and its reply each fit in one datagram, so no connection setup is needed. This saves the handshake delay of TCP. If a reply is too large for UDP, the server sets a truncation flag, and the client repeats the query over TCP on port 53. Zone transfers between name servers also use TCP.
Time cost of a lookup
GATE questions often ask for total time. Let the RTT between the client and the local resolver be R0, and let the RTTs from the resolver to the root, TLD, and authoritative servers be R1, R2, and R3.
- Empty cache: DNS time = R0 + R1 + R2 + R3 (the client waits for the whole chain).
- With the final record cached at the resolver: DNS time = R0.
After the address is known, the browser opens a TCP connection. That costs 1 RTT for the handshake. It then sends an HTTP request and waits for the reply, which costs another RTT. If the page references n objects and the connections are non-persistent with no parallelism, each object adds 2 RTTs.
For example, take R0 = 5 ms and R1 = R2 = R3 = 20 ms, with a requested base page that contains 3 small objects. We assume no parallel connections and ignore transmission time.
- DNS time: 5 + 20 + 20 + 20 = 65 ms.
- Base page: 1 RTT for TCP and 1 RTT for the request, 2 × RTT_web.
- Each of 3 objects: 2 × RTT_web.
- Total web time = 8 × RTT_web, added to the 65 ms.
Common errors
- Counting the client in the iteration. In the standard setup the client sends one recursive query. Only the local resolver iterates.
- Forgetting that referrals come back as replies. Each iterative step is a query and a reply. Count both when a question asks for messages.
- Assuming the root is always contacted. With cached NS records, the root and TLD steps are skipped.
- Treating a cached answer as authoritative. It is non-authoritative.
- Mixing up TTL meanings. DNS TTL is a time in seconds. The IP header TTL is a hop count. They are unrelated.
Exercise
A client's local resolver has an empty cache. The client resolves www.shop.example.org with a recursive query. The resolver then queries the root, the org TLD server, and the authoritative server for example.org. (a) How many DNS messages are exchanged in total, counting queries and replies, between all parties? (b) Ten seconds later, the client resolves api.example.org, and all NS records are still within their TTL. How many messages are exchanged now?
Show answer
(a) The client sends 1 query and gets 1 reply, which is 2 messages. The resolver sends 3 iterative queries and gets 3 replies, which is 6 messages. The total is 8. (b) The resolver already knows the authoritative server for example.org from its cache of NS records. It sends 1 query there and gets 1 reply, and the client exchange adds 2 more, so the total is 4. The api.example.org A record itself was not cached, so the authoritative server must still be asked.
Exercise
A client is 10 ms RTT from its local resolver. The resolver is 40 ms RTT from each of the root, TLD, and authoritative servers. The cache is empty. How long does the DNS part of the lookup take if the resolver iterates? How long does it take if the resolver has the final record cached?
Show answer
Empty cache: 10 + 40 + 40 + 40 = 130 ms. Final record cached: 10 ms, the RTT to the local resolver only.
Exercise
Why do root servers usually answer only iterative queries?
Show answer
A recursive query forces the server to keep state for each pending lookup until the answer returns. The root handles a huge number of queries, so this would overload it. With iterative queries the root replies at once with a referral and keeps no state. The local resolver does the remaining work and caches the results.