DNS Hierarchy, Recursive/Iterative Queries, and Caching
How domain lookups traverse Stub, Recursive, Root (.), TLD, and Authoritative servers, and how NS referrals, glue records, and TTL caching work.
1. The DNS Namespace Hierarchy
The Domain Name System (DNS) is a distributed, hierarchical database that translates human-readable domain names (such as api.premmallah.com) into IP addresses (such as 104.21.44.18) over UDP/TCP port 53.
Instead of storing every domain on earth in one central server, the DNS namespace is organized as an inverted tree read from right to left, starting at the implicit trailing dot (.) of the root:
| Server Tier | Example in Simulator | Responsibility |
|---|---|---|
| Stub Resolver | CLIENT / OS | Built into the client OS. Checks the local OS/browser cache and forwards unresolved queries to a configured Recursive Resolver. |
| Recursive Resolver | 1.1.1.1 (Cloudflare), 8.8.8.8 (Google), or ISP DNS | Accepts a recursive query from the client, walks the DNS tree by following referrals, caches the results, and returns the final answer. |
Root Name Server (.) | a.root-servers.net (13 logical root clusters globally via Anycast) | Top of the hierarchy. Does not know domain IPs; returns an NS referral (and glue IPs) for the appropriate Top-Level Domain (TLD). |
| TLD Name Server | gtld-servers.net (.com), .edu, .org | Maintains referrals to the Authoritative Name Servers registered for every second-level domain (e.g., premmallah.com) under that TLD. |
| Authoritative Name Server | ns1.cloudflare.com (Zone NS) | The final source of truth for the domain's zone file. Returns the definitive answer (AA = 1 flag) with its configured TTL. |
2. Recursive vs. Iterative Queries
A standard DNS lookup combines both query modes:
Client Stub ──[Recursive Query (RD=1)]──► Recursive Resolver
│
┌────[Iterative Query: Root (.)]─────┼────► Referral to .com TLD
├────[Iterative Query: .com TLD]─────┼────► Referral to Zone NS
└────[Iterative Query: Zone NS]──────┴────► Final A Record (104.21.44.18)
- Recursive Query (Client → Resolver): The caller asks the server to return the complete final answer (or an error), performing all intermediate lookups on the caller's behalf.
- Iterative Query (Resolver → Root / TLD / Authoritative): Each server replies with either the answer from its own zone or a referral (the address of the next lower server in the tree for the caller to query next). Root and TLD servers only answer iterative queries.
3. Essential DNS Resource Records & Glue Records
| Type | Name | What it stores |
|---|---|---|
A | IPv4 Address | Maps a hostname directly to a 32-bit IPv4 address (e.g., 104.21.44.18). |
AAAA | IPv6 Address | Maps a hostname directly to a 128-bit IPv6 address. |
CNAME | Canonical Name | Aliases one domain name to another target domain name (e.g., www.github.com → github.com). The resolver must then resolve the target's A record. |
NS | Name Server | Delegates a domain zone to an authoritative name server hostname. |
MX / TXT | Mail Exchange / Text | Mail servers for the domain / arbitrary verification and security text (SPF, DKIM). |
Why Glue Records are needed: When the .com TLD server refers a resolver to an authoritative server whose hostname is inside the same TLD (or inside the domain itself, such as ns1.example.com for example.com), the TLD must also include the IP address of that name server in the Additional section of its response. That extra A record is called a Glue Record, preventing a circular dependency.
4. Multi-Tier Caching and TTL (Time To Live)
Every DNS record includes a TTL (Time To Live) in seconds. Test all three cache levels in the simulator:
- Click FLUSH ALL CACHES, then click RESOLVE DOMAIN on
api.premmallah.com: performs a full 8-hop walk (96 msat 24 ms/RTT). - Click RESOLVE DOMAIN again: Local Browser/OS Cache Hit (
0 ms, 0 network hops). - Click CLEAR LOCAL CACHE, then click RESOLVE DOMAIN: Recursive Resolver Cache Hit (
24 ms, 2 hops: Client ↔ Resolver). - Click CLEAR LOCAL CACHE, switch the domain dropdown to
www.github.com(also a.comdomain), and click RESOLVE DOMAIN: notice the Root Server is skipped (6 hops,72 ms) because the Resolver cached the.comTLDNSreferral during step 1!
Exercises
1. Flush all caches, resolve ocw.mit.edu (.edu), then click CLEAR LOCAL CACHE and resolve cloudflare.org (.org). Does the second lookup skip the Root server? Why or why not?
Show solution
96 ms). The first lookup cached the .edu TLD referral, whereas cloudflare.org is under .org, so the resolver must query the Root server to learn the .org TLD servers.2. Before migrating a production web server to a new IP address at 2:00 PM tomorrow, why do engineers lower the DNS record's TTL from 86400 (24 hours) down to 60 (1 minute) at least a day in advance?
Show solution
Quiz
When a Recursive Resolver queries a Root Name Server for api.premmallah.com, what does the Root Server return?
Root servers only know the authoritative name servers for each Top-Level Domain (.com, .org, .edu, etc.).
Why do Recursive Resolvers rarely need to query Root Name Servers in steady-state operation?
Caching TLD referrals eliminates the Root hop for almost all subsequent queries sharing that TLD.
Which DNS record type aliases one hostname to another canonical hostname rather than to an IP address?
A CNAME (Canonical Name) points a hostname like www.github.com to another hostname like github.com.
In a standard lookup, what is the difference between the Client → Resolver query and the Resolver → Root/TLD queries?
The stub resolver sends a recursive query (RD=1) to the recursive resolver, which then performs iterative queries down the DNS tree.