Notes › Computer Networks
DNS — The Internet's Phone Book
Computers connect using IP addresses (76.76.21.21), people remember names (codifylern.vercel.app). The Domain Name System (DNS) translates names into addresses. It is a huge, distributed, hierarchical database.
- Runs mostly over UDP port 53 (fast, small queries); uses TCP 53 for large responses and zone transfers.
- Application-layer protocol.
The hierarchy
. (root) ← 13 root server names, hundreds of machines (anycast)
┌─────────┼──────────┐
.com .in .app ← TLD (top-level domain) servers
│ │ │
google.com co.in vercel.app ← authoritative servers for each domain
│
codifylern.vercel.app ← the record you want
A name is read right to left: root → TLD → domain → subdomain.
Resolving www.example.com step by step
- Browser cache → OS cache (
hostsfile too) → router. - Ask the recursive resolver (your ISP, or 8.8.8.8 / 1.1.1.1). If it has the answer cached, done.
- Resolver asks a root server → "I don't know, ask the
.comTLD servers at …". - Resolver asks the .com TLD server → "ask example.com's nameservers at …".
- Resolver asks the authoritative nameserver for example.com → "
www.example.com= 93.184.215.14". - Resolver caches it (for the TTL) and returns it to your computer.
Recursive vs iterative queries
| Recursive | Iterative |
|---|---|
| "Give me the final answer." The server does all the work. | "Give me the best answer you have" — usually a referral to another server. |
| Your computer → resolver | Resolver → root / TLD / authoritative |
Record types
| Record | Maps | Example |
|---|---|---|
| A | name → IPv4 | codify.dev A 76.76.21.21 |
| AAAA | name → IPv6 | codify.dev AAAA 2606:4700::6810:85e5 |
| CNAME | name → another name (alias) | www CNAME cname.vercel-dns.com |
| MX | mail server for the domain (with priority) | codify.dev MX 10 mail.provider.com |
| TXT | free text — domain verification, SPF, DKIM, DMARC | "google-site-verification=…" |
| NS | which nameservers are authoritative | ns1.provider.com |
| SOA | zone info (primary NS, serial, timers) | — |
| PTR | IP → name (reverse DNS) | used by mail servers |
| SRV | service location (host + port) | VoIP, games |
| CAA | which CAs may issue certificates | 0 issue "letsencrypt.org" |
Rule: a CNAME can't sit at the root of a domain (example.com) next to other records — providers offer ALIAS/ANAME/"CNAME flattening" for that.
TTL and caching
Every record has a TTL (time to live) in seconds. Resolvers and browsers cache answers until it expires.
- Long TTL (86400 = 1 day) → fewer lookups, but changes take long to spread ("DNS propagation").
- Lower the TTL (e.g. 300) a day before moving a site to a new server.
Setting up a custom domain (practical)
- Buy the domain from a registrar.
- Point an A record for the apex (
codify.dev) to the host's IP, and a CNAME forwwwto the host's name (e.g. Vercel/Netlify tell you the exact values). - For email: add MX records plus SPF/DKIM/DMARC TXT records so mail isn't marked as spam.
- Verify with:
nslookup codify.dev,dig codify.dev A +short,dig codify.dev MX.
DNS and load distribution
- Several A records for one name → clients spread across servers (DNS round robin).
- GeoDNS returns the nearest data centre's IP.
- Anycast: the same IP announced from many places; routing picks the closest (root servers, 1.1.1.1, CDNs).
Security
| Threat | What happens | Defence |
|---|---|---|
| DNS spoofing / cache poisoning | fake answers stored in a resolver, users sent to a bad IP | DNSSEC (signed records), random source ports/IDs |
| Eavesdropping | ISP sees every name you look up | DoH (DNS over HTTPS), DoT (DNS over TLS) |
| DDoS on DNS providers | sites become unreachable | anycast, multiple providers |
| Domain hijacking | attacker changes your NS records | registrar lock, 2FA |
Interview one-liners
- DNS converts domain names into IP addresses; it uses UDP port 53.
- Order: browser cache → OS → resolver → root → TLD → authoritative.
- Client→resolver is recursive; resolver→other servers is iterative.
- A = IPv4, AAAA = IPv6, CNAME = alias, MX = mail.
- TTL controls how long answers are cached.
- Related: What Happens When You Type a URL, HTTP and HTTPS — How the Web Talks.