๐ DNS Lookup & Resolution โ
BeginnerBeginner0โ1 yr experience Foundational concepts explained from first principles. No prior system-design experience required.Prerequisites: Comfortable reading code. Familiarity with at least one programming language.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.
