DNS (Domain Name System)
What is DNS?
DNS is the internet's phone book - it translates human-readable domain names (like google.com) into IP addresses (like 142.250.80.46) that computers use to communicate. It's a hierarchical, distributed database system that forms a critical part of internet infrastructure.
DNS Hierarchy
DNS Structure
Root (.)
/ | \
.com .org .net (TLDs)
| | |
google wikipedia example (SLDs)
| | |
www en mail (Subdomains)
DNS Components
-
Root Servers
- 13 root server clusters (A-M)
- Knows all TLD nameservers
- Distributed globally with anycast
-
TLD (Top-Level Domain) Servers
- Generic: .com, .org, .net, .edu
- Country-code: .uk, .jp, .de
- New gTLDs: .app, .dev, .blog
-
Authoritative Nameservers
- Contains actual DNS records
- Final source of truth for a domain
- Usually 2-4 servers for redundancy
-
Recursive Resolvers
- ISP or public DNS (8.8.8.8, 1.1.1.1)
- Performs full DNS lookup
- Caches results
DNS Resolution Process
Full Resolution Flow
User enters "www.example.com" in browser
↓
1. Check Browser Cache
↓ (miss)
2. Check OS Cache
↓ (miss)
3. Check Router Cache
↓ (miss)
4. Query ISP Recursive Resolver
↓
5. Query Root Server → Returns .com TLD servers
↓
6. Query .com TLD → Returns example.com nameservers
↓
7. Query example.com nameserver → Returns IP address
↓
8. Return IP to browser
↓
9. Browser connects to IP address
Recursive vs Iterative Queries
Recursive Query
Client → Recursive Resolver: "What's the IP of example.com?"
Recursive Resolver does all the work
Recursive Resolver → Client: "It's 93.184.216.34"
Iterative Query
Resolver → Root: "Where's .com?"
Root → Resolver: "Ask these TLD servers"
Resolver → TLD: "Where's example.com?"
TLD → Resolver: "Ask these nameservers"
Resolver → Nameserver: "What's the IP?"
Nameserver → Resolver: "93.184.216.34"
DNS Record Types
Common Record Types
-
A (Address) Record
example.com. IN A 93.184.216.34
Maps domain to IPv4 address
-
AAAA Record
example.com. IN AAAA 2606:2800:220:1:248:1893:25c8:1946
Maps domain to IPv6 address
-
CNAME (Canonical Name)
www.example.com. IN CNAME example.com.
blog.example.com. IN CNAME example.wordpress.com.Alias for another domain
-
MX (Mail Exchange)
example.com. IN MX 10 mail1.example.com.
example.com. IN MX 20 mail2.example.com.Specifies mail servers with priority
-
TXT (Text)
example.com. IN TXT "v=spf1 include:_spf.google.com ~all"
Arbitrary text data (SPF, DKIM, domain verification)
-
NS (Nameserver)
example.com. IN NS ns1.example.com.
example.com. IN NS ns2.example.com.Specifies authoritative nameservers
-
PTR (Pointer)
34.216.184.93.in-addr.arpa. IN PTR example.com.
Reverse DNS lookup (IP to domain)
-
SOA (Start of Authority)
example.com. IN SOA ns1.example.com. admin.example.com. (
2024010101 ; Serial
7200 ; Refresh
3600 ; Retry
1209600 ; Expire
3600 ; Minimum TTL
) -
SRV (Service)
_sip._tcp.example.com. IN SRV 10 60 5060 sipserver.example.com.
Service discovery (port, priority, weight)
-
CAA (Certificate Authority Authorization)
example.com. IN CAA 0 issue "letsencrypt.org"
Specifies allowed certificate authorities
DNS Caching
Cache Locations
-
Browser Cache
- Chrome: chrome://net-internals/#dns
- Very short TTL (60 seconds typical)
-
Operating System Cache
- Windows:
ipconfig /displaydns
- Linux: nscd or systemd-resolved
- macOS:
dscacheutil -cachedump
- Windows:
-
Router/Gateway Cache
- Home routers cache DNS
- Corporate firewalls
-
ISP Recursive Resolver
- Largest cache
- Serves many users
TTL (Time To Live)
example.com. 300 IN A 93.184.216.34
↑
TTL in seconds (5 minutes)
Common TTL Values:
- 300 (5 min): Frequently changing content
- 3600 (1 hour): Normal websites
- 86400 (1 day): Stable infrastructure
- 604800 (1 week): Rarely changing records
DNS Security
DNSSEC (DNS Security Extensions)
Provides authentication and integrity but not confidentiality
How it works:
- Zone signing with private key
- Public key in DNSKEY record
- Chain of trust from root
- Signature validation
example.com. IN RRSIG A 8 2 86400 (
20240131000000 20240101000000 12345 example.com.
[signature data]
)
DNS Privacy
-
DNS over HTTPS (DoH)
https://cloudflare-dns.com/dns-query
https://dns.google/dns-query- Encrypted DNS queries over HTTPS
- Port 443
- Harder to block/filter
-
DNS over TLS (DoT)
tls://1.1.1.1
tls://8.8.8.8- Encrypted DNS over TLS
- Port 853
- Easier to identify and block
-
DNSCrypt
- Encrypted DNS protocol
- Authentication of resolver
- Prevention of tampering
DNS Attacks
-
DNS Spoofing/Cache Poisoning
- Attacker provides false DNS responses
- Poisoned cache spreads to users
- Mitigation: DNSSEC, 0x20 bit encoding
-
DNS Amplification DDoS
- Small query generates large response
- Spoofed source IP
- Mitigation: Rate limiting, response rate limiting
-
DNS Tunneling
- Encode data in DNS queries/responses
- Bypass firewalls
- Detection: Analyze query patterns
-
Domain Hijacking
- Unauthorized domain transfer
- Registrar account compromise
- Mitigation: Registry lock, 2FA
-
DNS Rebinding
- Bypass same-origin policy
- Attack local network
- Mitigation: DNS rebinding protection
DNS Load Balancing
Round-Robin DNS
example.com. IN A 192.0.2.1
example.com. IN A 192.0.2.2
example.com. IN A 192.0.2.3
- Simple load distribution
- No health checking
- Client-side caching issues
GeoDNS/GeoIP
US Users → example.com → 192.0.2.1 (US Server)
EU Users → example.com → 192.0.2.2 (EU Server)
Asia Users → example.com → 192.0.2.3 (Asia Server)
Weighted DNS
example.com. IN A 192.0.2.1 ; weight=3
example.com. IN A 192.0.2.2 ; weight=1
Traffic distribution: 75% to first server, 25% to second
DNS Providers and Services
Public DNS Resolvers
-
Cloudflare (1.1.1.1)
- Fastest performance
- Privacy-focused
- DNSSEC validation
-
Google (8.8.8.8)
- Reliable and fast
- Global anycast network
- Extensive caching
-
Quad9 (9.9.9.9)
- Security-focused
- Blocks malicious domains
- No logging policy
-
OpenDNS (208.67.222.222)
- Content filtering options
- Phishing protection
- Enterprise features
Managed DNS Providers
-
Route53 (AWS)
- Health checking
- Traffic policies
- AWS integration
-
Cloudflare DNS
- Fast propagation
- DDoS protection
- Analytics
-
Google Cloud DNS
- Anycast nameservers
- DNSSEC support
- SLA guarantee
DNS Configuration
Zone File Example
$ORIGIN example.com.
$TTL 3600
@ IN SOA ns1.example.com. admin.example.com. (
2024010101 ; Serial
3600 ; Refresh
1800 ; Retry
604800 ; Expire
86400 ) ; Minimum TTL
@ IN NS ns1.example.com.
@ IN NS ns2.example.com.
@ IN A 93.184.216.34
@ IN MX 10 mail.example.com.
www IN CNAME example.com.
mail IN A 93.184.216.35
ftp IN A 93.184.216.36
DNS Propagation
- Not instant - can take 24-48 hours
- Depends on TTL values
- Old cached records expire gradually
- Use DNS propagation checkers
Performance Optimization
Best Practices
-
Minimize DNS Lookups
- Reduce unique hostnames
- Use domain sharding carefully
-
Optimize TTL Values
- Balance between caching and flexibility
- Lower TTL before changes
-
Use DNS Prefetching
<link rel="dns-prefetch" href="//api.example.com">
-
Implement DNS Failover
- Multiple NS records
- Health checking
- Automatic failover
-
Consider Anycast DNS
- Same IP multiple locations
- Automatic routing to nearest
- Built-in redundancy
Troubleshooting DNS
Common Tools
-
nslookup
nslookup example.com
nslookup example.com 8.8.8.8 -
dig
dig example.com
dig @8.8.8.8 example.com
dig +trace example.com
dig -x 93.184.216.34 # Reverse lookup -
host
host example.com
host -t MX example.com -
systemd-resolve
systemd-resolve --status
systemd-resolve example.com
Common Issues
-
NXDOMAIN
- Domain doesn't exist
- Check spelling, propagation
-
SERVFAIL
- DNS server failure
- DNSSEC validation failure
-
Slow Resolution
- High latency to DNS server
- Too many DNS lookups
- No caching
-
Split-Horizon DNS
- Different responses internal/external
- VPN considerations
Modern DNS Features
DNS Service Discovery
_service._proto.domain.com
_http._tcp.example.com SRV 10 1 80 web.example.com
Multicast DNS (mDNS)
- Local network discovery
- .local domain
- Zero configuration
DNS-SD (DNS Service Discovery)
- Automatic service discovery
- Works with mDNS
- Used by Bonjour, Avahi
Interview Questions
-
Q: What happens when DNS resolution fails? A: Browser shows error, fallback to hosts file, try alternate DNS servers, or connection timeout.
-
Q: How do you reduce DNS lookup time? A: DNS caching, reduce unique domains, DNS prefetching, use faster DNS servers, implement anycast.
-
Q: What's the difference between A and CNAME records? A: A record points to IP address directly; CNAME is an alias to another domain name. CNAME can't be at root domain.
-
Q: How does DNS caching affect deployment? A: Old IPs may be cached; use low TTL before changes, wait for propagation, implement gradual rollout.
-
Q: How do you implement DNS failover? A: Multiple A records, health checking, dynamic DNS updates, or use managed DNS with failover features.