Chapter 7: DNS, CDN & Load Balancing
Metadata Card
| Item | Content |
|---|---|
| Difficulty | (Medium) |
| Prerequisites | Chapter 1 (Layering Model); Chapter 4 (HTTP) |
| Keywords | DNS, A/AAAA/CNAME records, recursive/iterative queries, Anycast, CDN, edge nodes, L4/L7 load balancing, consistent hashing, Round Robin |
| Python | 3.8+ |
| Code | ~100 lines |
Your Progress
"You discover an invisible navigation system on the post road — when you say 'google.com', DNS (Domain Name Resolution Array) translates it into post road coordinates. CDN (Content Distribution Post Road) copies spell scrolls to the beacon tower closest to you. Load balancing distributes mana traffic across multiple backend beacon towers."
DNS — World's Largest Distributed K/V Store
┌───────────────────────┐
│ Root DNS Servers │ (13 logical groups, Anycast)
│ (.) │
└────────┬──────────────┘
│
┌────────┴──────────────┐
│ TLD DNS Servers │ .com / .org / .cn / .io ...
│ (TLD) │
└────────┬──────────────┘
│
┌────────┴──────────────┐
│ Authoritative DNS │ Authoritative records for example.com
│ (Authoritative) │
└───────────────────────┘Recursive vs Iterative:
- Recursive: Client says "look up www.example.com and tell me the answer" — does nothing, waits for result
- Iterative: Recursive resolver asks each server level — multiple round trips
DNS Record Types & TTL
| Type | Full Name | Purpose | Example |
|---|---|---|---|
| A | Address | Domain → IPv4 | example.com → 93.184.216.34 |
| AAAA | IPv6 Address | Domain → IPv6 | example.com → 2606:2800:220:1:248:1893:25c8:1946 |
| CNAME | Canonical Name | Domain → another domain (alias) | www.example.com → example.com |
| MX | Mail Exchange | Domain → mail server | example.com → mail.example.com (priority 10) |
| NS | Name Server | Domain → authoritative DNS | example.com → ns1.example.com |
| TXT | Text | Arbitrary text storage | SPF, domain verification |
| SOA | Start of Authority | Zone authority info | — |
CDN — Putting the World at Your Doorstep
Architecture: Edge nodes (POP) around the world cache content. Users fetch from the nearest edge node.
Three elements:
- Edge/POP Node: Cache servers deployed globally
- Origin Pull: If edge doesn't have the resource (cache miss), it pulls from origin
- Anycast Routing: Multiple edge nodes share one IP; user traffic automatically routes to "nearest" node
Load Balancing — Taming Traffic
L4 vs L7:
| Feature | L4 (Transport) | L7 (Application) |
|---|---|---|
| Working Level | Based on IP + Port | Based on HTTP content (URL, Header, Cookie) |
| Performance | Very high (pure kernel forwarding) | Lower (needs to parse application protocol) |
| Flexibility | Low — only IP/PORT based | High — URL path, user identity, content type |
| Examples | LVS, IPVS | Nginx, HAProxy, Envoy, Traefik |
Common Algorithms: Round Robin, Weighted RR, Least Connections, IP Hash, Consistent Hashing.
Consistent Hash
This pattern maps servers onto a virtual ring (0 ~ 2^32-1). When adding/removing a server, only neighboring nodes are affected — only K/N keys need migration.
Traveler's Notes
DNS + CDN + Load Balancing form the entry skeleton of the modern internet. When you visit any large website, all three are working within the first second. Remember: DNS isn't just a phonebook — it's a distributed, fault-tolerant, hierarchically cached key-value store that handles billions of queries per second.