DNS Explained: How Domain Names Get Turned Into IP Addresses
Every single time you open a browser and visit a website, something remarkable happens in the background — something so fast and so reliable that most developers take it completely for granted. Your computer doesn't actually know where 'youtube.com' lives. It only speaks in numbers. DNS is the invisible system that bridges the human-readable world of domain names and the machine-readable world of IP addresses, and it handles over a trillion lookups every single day across the planet.
Before DNS existed — and yes, there was a time before it — every computer on the internet had to maintain a single text file called HOSTS.TXT that listed every known hostname and its IP address. A central team at Stanford Research Institute would update it, and sysadmins everywhere had to manually download the latest copy. When the internet had a few hundred machines, this was just about manageable. When it grew to thousands? The system collapsed under its own weight. DNS was invented in 1983 by Paul Mockapetris to solve exactly this scalability crisis — and the solution he came up with is still running the internet today.
By the end of this article you'll be able to explain exactly what happens between typing a URL and a webpage loading, describe the hierarchy of DNS servers and the role each one plays, understand what DNS records like A, CNAME, and MX actually do, and confidently answer DNS questions in a technical interview. No prior networking knowledge needed — we're building this from the ground up.
IP Addresses: The Numbers the Internet Actually Uses
Before DNS makes any sense, you need to understand what it's translating TO. Every device connected to the internet — your laptop, your phone, a web server in a data centre in Virginia — has a unique numerical address called an IP address. Think of it like a home address, except for computers.
An IPv4 address looks like this: 142.250.80.46. Four numbers, each between 0 and 255, separated by dots. That specific address is one of Google's servers. Your computer could reach Google by typing that number directly into your browser — try it. It works. But nobody wants to memorise 142.250.80.46 instead of 'google.com'.
IPv6 addresses look even worse: 2607:f8b0:4004:c09::6a. Humans are terrible at remembering these. Computers are great at them. DNS exists purely to bridge that gap — to let humans use words while computers use numbers. Every domain name on the internet is ultimately just a friendly alias for one or more IP addresses.
# This command performs a DNS lookup from your terminal. # It asks: "What IP address does 'google.com' map to?" # nslookup is available on Windows, macOS, and Linux. nslookup google.com # You can also use 'dig' on macOS/Linux for more detail: dig google.com +short # Let's also look up a smaller site to see a simpler single-IP result: nslookup github.com
Address: 192.168.1.1#53
Non-authoritative answer:
Name: google.com
Addresses: 142.250.80.46
142.250.80.110
# dig output (cleaner):
142.250.80.46
# github.com lookup:
Name: github.com
Address: 140.82.114.4
The DNS Hierarchy: Four Servers, One Answer
DNS isn't a single giant database. If it were, it would be the most catastrophic single point of failure in human history. Instead, DNS is a beautifully distributed hierarchy of servers spread across the entire planet. When your computer needs to resolve a domain name, it talks to up to four different types of server in sequence until it gets an answer.
Think of it like asking for directions in an unfamiliar city. You first ask your hotel concierge (your local DNS resolver). They might know the answer from memory. If not, they call the city's central tourist office (the Root Nameserver). The tourist office doesn't know the exact restaurant, but they say 'for Italian restaurants, call the Italian Quarter office' (the TLD Nameserver). The Italian Quarter office then gives you the exact address (the Authoritative Nameserver). Four stops, one answer.
Here are the four players:
- RECURSIVE RESOLVER — Your ISP or a public DNS service (like 8.8.8.8 — Google's DNS). This is the server your device asks first. It does all the legwork of talking to the other servers on your behalf.
- ROOT NAMESERVER — There are 13 logical root nameservers (labelled A through M) run by organisations like NASA and ICANN. They don't know IP addresses — they just know which TLD nameserver to contact for '.com', '.org', '.uk', etc.
- TLD NAMESERVER — TLD stands for Top-Level Domain. The .com TLD nameserver knows which Authoritative Nameserver is responsible for every .com domain ever registered.
- AUTHORITATIVE NAMESERVER — This is the final authority. It's managed by whoever owns the domain (or their hosting provider). It holds the actual DNS records and gives the definitive answer.
# The 'dig +trace' command shows you the FULL DNS resolution journey. # Watch it walk through all four server types step by step. # Run this in your terminal (macOS or Linux). On Windows, use WSL. dig thecodforge.io +trace # WHAT YOU'LL SEE: # Step 1 — Your resolver contacts a ROOT nameserver (e.g., a.root-servers.net) # Step 2 — Root server says: "for .io domains, ask this TLD server" # Step 3 — TLD server says: "for thecodeforge.io, ask THIS authoritative server" # Step 4 — Authoritative server gives the actual IP address # You can also see exactly WHICH root servers exist: dig . NS +short
;; QUESTION SECTION:
;thecodeforge.io. IN A
. 518400 IN NS a.root-servers.net. ; Root server responds
. 518400 IN NS b.root-servers.net.
io. 172800 IN NS a0.nic.io. ; .io TLD server responds
io. 172800 IN NS b0.nic.io.
thecodeforge.io. 3600 IN NS ns1.hostingprovider.com. ; Authoritative server
thecodeforge.io. 300 IN A 104.21.45.67 ; FINAL ANSWER — the IP address
DNS Records Demystified: A, CNAME, MX, TXT and More
DNS isn't just about mapping domain names to IP addresses. It's a full-blown database of records that controls how your domain behaves across the internet. Each record type answers a different question about your domain.
Think of DNS records like different departments in a company. The A Record department handles 'where is the website?'. The MX Record department handles 'where should emails go?'. The CNAME department handles 'this name is just an alias for another name'. They all live in the same building (your Authoritative Nameserver) but do completely different jobs.
Here are the records every developer needs to know:
— A RECORD: Maps a domain name directly to an IPv4 address. This is the most fundamental record. 'thecodeforge.io → 104.21.45.67'.
— AAAA RECORD: Same as A, but for IPv6 addresses. The four A's stand for 'quad-A'.
— CNAME RECORD: 'Canonical Name' — an alias. 'www.thecodeforge.io → thecodeforge.io'. Points one name to another name, not directly to an IP.
— MX RECORD: 'Mail Exchange' — tells the internet which server handles email for your domain. Without this, nobody can email you at your domain.
— TXT RECORD: Plain text attached to a domain. Used for email verification (SPF, DKIM), proving domain ownership to Google/GitHub, and security configs.
— NS RECORD: 'Nameserver' — declares which servers are authoritative for this domain. When you buy a domain and point it to Cloudflare, you're updating NS records.
— TTL (Time To Live): Not a record type, but every record has one. It's a number in seconds telling DNS servers how long to cache this record before checking again. TTL of 3600 means 'cache this for 1 hour'.
# Let's query specific DNS record types for real domains. # The syntax is: dig <domain> <RECORD_TYPE> # 1. A Record — get the IPv4 address for github.com dig github.com A +short # Output: 140.82.114.4 # 2. MX Record — find where GitHub's email servers are dig github.com MX +short # Output: 1 aspmx.l.google.com. (GitHub uses Google Workspace for email) # 3. CNAME Record — www is often a CNAME alias dig www.github.com CNAME +short # Output: github.com. (www.github.com is just an alias for github.com) # 4. TXT Records — see domain verification and security records dig github.com TXT +short # Output includes SPF record: "v=spf1 ip4:192.30.252.0/22 include:_netblocks.google.com ~all" # 5. NS Records — which nameservers are authoritative for github.com? dig github.com NS +short # Output: # ns-1283.awsdns-32.org. <-- GitHub uses Amazon Route 53 # ns-1707.awsdns-21.co.uk. # ns-421.awsdns-52.com. # ns-520.awsdns-01.net. # 6. Check TTL — how long is this record cached? (look at the number after IN) dig github.com A # In the ANSWER SECTION you'll see something like: # github.com. 60 IN A 140.82.114.4 # ^^--- TTL of 60 seconds — GitHub refreshes DNS frequently
140.82.114.4
# MX Record:
1 aspmx.l.google.com.
# CNAME:
github.com.
# TXT (excerpt):
"v=spf1 ip4:192.30.252.0/22 include:_netblocks.google.com ~all"
# NS Records:
ns-1283.awsdns-32.org.
ns-1707.awsdns-21.co.uk.
ns-421.awsdns-52.com.
ns-520.awsdns-01.net.
# Full A record with TTL:
;; ANSWER SECTION:
github.com. 60 IN A 140.82.114.4
DNS Caching, TTL and Why Your Changes Seem to Take Forever
You've just launched your new website. You updated the DNS records. You refresh the browser. Still showing the old site. You check the DNS settings — everything looks right. What's happening? The answer is caching, and understanding it will save you hours of frustration.
Every DNS resolver along the lookup chain caches the answer it receives for exactly as long as the TTL (Time To Live) says. If your A record has a TTL of 86400 (24 hours), every resolver that looked up your domain in the past 24 hours won't check again until that timer runs out. Even if you change the IP address at the Authoritative Nameserver, millions of resolvers worldwide are still serving the old answer from their cache.
This is called 'DNS propagation' — and it's not actually propagation in the way people imagine (records pushing outward). It's really just the world's cached copies expiring and fetching fresh answers at different times. Different users around the world will see the new records at different times depending on when their local resolver's cache expires.
The professional move: before making a big DNS change (like moving a site to a new host), lower your TTL to 300 seconds (5 minutes) a day or two in advance. Once traffic is migrated successfully, raise it back to 3600 or 86400 for better performance.
# Python's built-in 'socket' library can perform basic DNS lookups. # For richer DNS querying, we'll also use 'dnspython' — install it with: # pip install dnspython import socket import dns.resolver # from the dnspython library # ───────────────────────────────────────────── # METHOD 1: Basic lookup using Python's socket module # This asks your OS's configured DNS resolver for an IP address # ───────────────────────────────────────────── domain_name = "github.com" # getaddrinfo returns a list of (family, type, proto, canonname, sockaddr) tuples results = socket.getaddrinfo(domain_name, 80) # port 80 = HTTP print(f"IP addresses for {domain_name}:") for result in results: ip_address = result[4][0] # sockaddr is a tuple; index 0 is the IP print(f" → {ip_address}") print() # ───────────────────────────────────────────── # METHOD 2: Querying specific record types with dnspython # This gives us full control — like running 'dig' from Python # ───────────────────────────────────────────── # Query the A record (IPv4 address) print(f"A Records for {domain_name}:") a_records = dns.resolver.resolve(domain_name, 'A') for record in a_records: print(f" IP Address : {record.address}") print(f" TTL : {a_records.rrset.ttl} seconds") print() # Query the MX record (mail servers) print(f"MX Records for {domain_name}:") mx_records = dns.resolver.resolve(domain_name, 'MX') for record in mx_records: # MX records have a 'preference' (priority) — lower number = higher priority print(f" Mail Server : {record.exchange} (Priority: {record.preference})") print() # Query TXT records (often used for domain verification) print(f"TXT Records for {domain_name}:") txt_records = dns.resolver.resolve(domain_name, 'TXT') for record in txt_records: # Decode bytes to string for readable output decoded_text = b"".join(record.strings).decode('utf-8') print(f" TXT : {decoded_text[:80]}...") # Truncate long records for display
→ 140.82.114.4
A Records for github.com:
IP Address : 140.82.114.4
TTL : 60 seconds
MX Records for github.com:
Mail Server : aspmx.l.google.com. (Priority: 1)
Mail Server : alt1.aspmx.l.google.com. (Priority: 5)
Mail Server : alt2.aspmx.l.google.com. (Priority: 5)
TXT Records for github.com:
TXT : v=spf1 ip4:192.30.252.0/22 include:_netblocks.google.com ~all...
| DNS Record Type | What It Maps | Real-World Use Case | Can Be at Root Domain? |
|---|---|---|---|
| A Record | Domain → IPv4 address | Point thecodeforge.io to your web server's IP | Yes |
| AAAA Record | Domain → IPv6 address | Same as A record but for IPv6-enabled servers | Yes |
| CNAME Record | Domain → Another domain name | www.thecodeforge.io → thecodeforge.io | No — subdomains only |
| MX Record | Domain → Mail server hostname | Route emails sent to @thecodeforge.io to Google Workspace | Yes |
| TXT Record | Domain → Arbitrary text string | SPF email security, proving domain ownership to Google/GitHub | Yes |
| NS Record | Domain → Authoritative nameserver | Tell the internet which DNS servers manage your domain | Yes |
| TTL (on any record) | Cache lifetime in seconds | 300s before migrations, 86400s for stable production records | N/A |
🎯 Key Takeaways
- DNS is a distributed, hierarchical database — not a single server. Four server types collaborate on every lookup: Recursive Resolver → Root Nameserver → TLD Nameserver → Authoritative Nameserver.
- TTL controls caching. A low TTL (300s) means faster propagation of changes but more DNS queries. A high TTL (86400s) means faster performance for users but slow DNS change rollouts. Tune it intentionally, especially before migrations.
- CNAME records create aliases between domain names, but they can only be used on subdomains — never at the root/apex domain (e.g., you can CNAME 'www.site.com' but not 'site.com' itself).
- Your computer has its own DNS cache, your router has one, your ISP's resolver has one, and every server along the chain has one. When debugging DNS issues, always test with a specific public resolver (dig @8.8.8.8) to bypass all local caches and get the true current answer.
⚠ Common Mistakes to Avoid
- ✕Mistake 1: Changing DNS records and expecting instant results — Symptom: You update your A record but the old site still loads for hours. Fix: This is TTL caching at work. Plan ahead — reduce TTL to 300 seconds at least 48 hours before a migration. After the migration, verify with 'dig @8.8.8.8 yourdomain.com A' to bypass your local cache and query Google's resolver directly.
- ✕Mistake 2: Creating a CNAME at the root/apex domain — Symptom: Your DNS provider throws an error, or email stops working entirely. The DNS spec forbids CNAME records at the zone apex (e.g., thecodeforge.io) because a CNAME must be the only record for that name, but the root domain always needs NS and SOA records too. Fix: Use an ALIAS or ANAME record if your DNS provider supports it (Cloudflare, Route 53), or simply use an A record at the root domain and a CNAME only for 'www'.
- ✕Mistake 3: Forgetting that DNS is public and fully inspectable — Symptom: Developers accidentally expose internal infrastructure details (server IPs, internal subdomain names like 'staging.company.com' or 'vpn.company.com') in DNS records that anyone on the internet can query. Fix: Audit your DNS records regularly. Never put sensitive hostnames in public DNS. Use split-horizon DNS (different DNS records for internal vs. external networks) if you need internal names to resolve only within your organisation.
Interview Questions on This Topic
- QWalk me through exactly what happens — at the DNS level — from the moment I type 'google.com' in my browser and hit Enter to when the page starts loading.
- QWhat is the difference between an A record and a CNAME record, and when would you choose one over the other? What's the limitation of CNAME that makes it sometimes impossible to use?
- QWhat is DNS TTL and why does it matter in a production deployment? If you needed to migrate a high-traffic site to a new server with zero downtime, how would you use TTL strategically?
Frequently Asked Questions
What is DNS in simple terms?
DNS (Domain Name System) is the internet's contact book. It translates human-readable domain names like 'google.com' into numerical IP addresses like '142.250.80.46' that computers use to find each other. Without DNS, you'd have to memorise a unique number for every website you want to visit.
How long does DNS propagation take?
DNS propagation isn't a single event — it's the process of the world's cached DNS records expiring and being refreshed. It can take anywhere from a few minutes (if the previous TTL was low) to 48 hours (if the previous TTL was set to 86400 seconds). You can speed this up significantly by lowering your TTL to 300 seconds a day or two before making DNS changes.
What is the difference between a DNS resolver and an authoritative nameserver?
A recursive resolver (like Google's 8.8.8.8 or your ISP's DNS server) is the middleman — it receives your query and hunts down the answer by talking to other servers on your behalf. An authoritative nameserver is the final authority — it's the server that actually holds the DNS records for a specific domain and gives the definitive, canonical answer. When you buy a domain and set up DNS records in Cloudflare or Route 53, those are your authoritative nameservers.
Written and reviewed by senior developers with real-world experience across enterprise, startup and open-source projects. Every article on TheCodeForge is written to be clear, accurate and genuinely useful — not just SEO filler.