DNS
Understanding Domain Name System and how it resolves domain names
Domain Name System (DNS)
DNS is the phonebook of the internet. It translates human-readable domain names (like www.example.com) into IP addresses that computers use to identify each other.
How DNS Works
When you type a URL in your browser:
- Browser Cache: First checks if the domain was recently resolved
- OS Cache: Checks the operating system's DNS cache
- Resolver: Contacts your ISP's DNS resolver
- Root Server: If not cached, queries the root DNS server
- TLD Server: Directs to Top-Level Domain server (.com, .org, etc.)
- Authoritative Server: Returns the final IP address
DNS Record Types
| Type | Purpose | Example |
|---|---|---|
| A | Maps domain to IPv4 | example.com → 93.184.216.34 |
| AAAA | Maps domain to IPv6 | example.com → 2606:2800:220:1:... |
| CNAME | Alias to another domain | www.example.com → example.com |
| MX | Mail server | example.com → mail.example.com |
| NS | Name server | example.com → ns1.example.com |
| TXT | Text records | SPF, DKIM verification |
DNS Caching
DNS responses are cached at multiple levels to improve performance:
Code
interface DNSCache {
ttl: number; // Time-to-live in seconds
record: DNSRecord;
timestamp: Date;
}
// Typical TTL values
const TTL_VALUES = {
short: 300, // 5 minutes - frequently changing
medium: 3600, // 1 hour - standard
long: 86400, // 24 hours - stable records
};DNS Load Balancing
DNS can distribute traffic across multiple servers:
Round Robin
Returns different IP addresses in rotation:
Query 1: example.com → 192.168.1.1
Query 2: example.com → 192.168.1.2
Query 3: example.com → 192.168.1.3
Geolocation-based
Returns IP based on user's location for lower latency.
Weighted
Assigns weights to different servers based on capacity.
DNS Security
Common Attacks
- DNS Spoofing: Returning fake IP addresses
- DNS Amplification: DDoS using DNS responses
- Cache Poisoning: Injecting malicious records
Protection Mechanisms
- DNSSEC: Cryptographic signatures for DNS records
- DNS over HTTPS (DoH): Encrypts DNS queries
- DNS over TLS (DoT): TLS encryption for DNS
Best Practices
- Use multiple DNS providers for redundancy
- Set appropriate TTLs based on change frequency
- Implement DNSSEC for security
- Monitor DNS performance and availability
- Use anycast DNS for global distribution