🔍 DNS Lookup & Resolution
DNS (Domain Name System) is the "Phonebook of the Internet." It translates human-readable domain names (e.g., google.com) into machine-readable IP addresses (e.g., 142.250.190.46).
1. The DNS Hierarchy
DNS is not a single database; it's a distributed, hierarchical system.
- Root Servers: The starting point of any DNS query. They don't know the IP, but they know where the TLD servers are.
- TLD (Top-Level Domain) Servers: Manage extensions like
.com,.org,.net. They point to the Authoritative Nameservers. - Authoritative Nameservers: The final source of truth. They hold the actual DNS records (A, AAAA, CNAME, etc.) for a specific domain.
2. DNS Resolution Flow
When you type a URL, your computer (the DNS Client) asks a Recursive Resolver (usually provided by your ISP or a provider like Cloudflare/Google) to find the IP.
📊 Step-by-Step Sequence
3. DNS Caching: The Speed Secret
To avoid going through the hierarchy every time, DNS results are cached at multiple levels:
- Browser Cache: The first place checked.
- OS Cache: If not in the browser, the OS checks its own cache.
- Router Cache: Your home router often keeps a small cache.
- ISP/DNS Provider Cache: The Recursive Resolver handles the most traffic and has the largest cache.
📊 Flowchart: Caching Logic
4. 💻 Code Example: Dynamic Resolution
In a real application, you might need to resolve hostnames programmatically. Here is how you can do it in Node.js/Deno using the dns module.
Simplified DNS Client Logic
javascript
const dns = require("node:dns/promises");
/**
* Simulates a high-level DNS resolution process
*/
async function resolveDomain(domain) {
console.log(`🔍 Resolving domain: ${domain}...`);
try {
// Under the hood, this uses the OS resolver
// which in turn queries your recursive resolver (like 8.8.8.8)
const { address, family } = await dns.lookup(domain);
console.log(`✅ Success!`);
console.log(`📡 IP Address: ${address}`);
console.log(`🌐 Version: IPv${family}`);
return address;
} catch (error) {
console.error(`❌ Failed to resolve ${domain}: ${error.code}`);
throw error;
}
}
// Usage
resolveDomain("google.com");📈 Architecture Context
In system design, DNS is often the entry point for all user traffic.
Key Concepts for Interviews
- TTL (Time to Live): How long a DNS record should be cached.
- Anycast: Routing a single IP to multiple physical locations (common for DNS providers).
- DNSSEC: Security extensions to prevent DNS hijacking.
