DNS: The Phonebook of the Internet
DNS: The Phonebook of the Internet
Every computer on the internet has a unique IP address (like 192.0.2.1 or 2001:db8::1). However, humans are much better at remembering names like google.com or wikipedia.org. The Domain Name System (DNS) is the distributed hierarchical database that translates these human-readable domain names into machine-readable IP addresses.
In the context of system design, DNS is often the first point of entry for a user's request and can be used for load balancing and high availability.
How DNS Works: A Hierarchical Search
DNS isn't a single server; it's a global hierarchy of servers. When you type a URL into your browser, a process called DNS Resolution occurs:
- DNS Recursor (Recursive Resolver): Usually provided by your ISP or a public provider like Google (8.8.8.8). It acts as the librarian, searching for the address on your behalf.
- Root Nameservers: The first stop for the recursor. It doesn't know the IP, but it knows where to find the TLD Nameservers.
- TLD (Top-Level Domain) Nameservers: These servers handle specific suffixes like
.com,.org, or.net. They point the recursor to the Authoritative Nameservers. - Authoritative Nameservers: These are the final authority for a specific domain. They hold the actual resource records (the IP address).
Key DNS Record Types
- A Record: Maps a domain name to an IPv4 address.
- AAAA Record: Maps a domain name to an IPv6 address.
- CNAME (Canonical Name): Maps one domain to another (an alias). Useful for pointing
www.example.comtoexample.com. - MX Record: Points to the mail servers for the domain.
- TXT Record: Holds arbitrary text, often used for security verification (like SPF or DKIM).
- NS Record: Specifies the authoritative nameservers for the domain.
DNS for Load Balancing and Latency
DNS can be more than just a translator; it can be a traffic manager:
- Round Robin DNS: An authoritative nameserver returns a list of multiple IP addresses for a single domain. Browsers typically pick one at random, distributing load across multiple servers.
- GeoDNS: The nameserver detects the user's location based on their IP and returns the IP of the server cluster closest to them.
- Anycast DNS: Multiple servers across the globe share the same IP address. Routers automatically send the request to the nearest server (based on BGP routing).
Interrogating DNS with 'dig'
- 1Step 1
Use the
digcommand to find the IPv4 address of a domain.1dig google.com A +shortThis returns just the IP address(es) associated with the domain.
- 2Step 2
To see every step of the hierarchy from the root down to the authoritative server, use the
+traceflag.1dig google.com +trace - 3Step 3
You can bypass your default resolver and ask a specific nameserver directly (e.g., Google's 8.8.8.8).
1dig @8.8.8.8 google.com - 4Step 4
Look at the number in the second column of a full
digoutput. That is the TTL in seconds. It tells you how long resolvers are allowed to cache this record before asking again.
TTL: The Balancing Act
Time To Live (TTL) determines how long a DNS record is cached.
- High TTL (e.g., 24 hours): Reduces load on your nameservers and speeds up resolution for users (as it's cached locally), but makes it hard to change IPs quickly if a server fails.
- Low TTL (e.g., 60 seconds): Allows for rapid failover and dynamic traffic management, but increases latency (more frequent lookups) and load on your infrastructure.
Common Mistakes
- DNS Propagations Myth: People often say "DNS takes 24 hours to propagate." In reality, it's just waiting for the old TTL to expire in various caches around the world.
- Neglecting TTL for Migrations: If you're moving to a new server, you should lower your TTL before the move to ensure users switch to the new IP quickly.
- Using CNAME at the Apex: You generally cannot have a CNAME record for your root domain (e.g.,
example.com). You must use an A/AAAA record or "Alias" records provided by specific DNS hosts.
Recap
- DNS translates domain names to IP addresses through a hierarchical search.
- Recursive resolvers do the work; Authoritative servers have the final answer.
- Record types like A, CNAME, and MX define different types of mapping.
- TTL is the key variable for balancing speed vs. agility.
Knowledge Check
Which DNS server is the final source of truth for a specific domain's records?