SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
  • OSI Model
  • IP Address
  • TCP vs UDP
  • HTTP / HTTPS
  • DNS
  • Proxy vs Reverse Proxy
  • Checksums
  • HTTP/1.1 vs HTTP/2 vs HTTP/3
  • Cookies and CORS
  • Subnets, CIDR and NAT
  • Firewalls and VPN
  • Ports and Sockets
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Foundations/Networking

DNS

The phone book of the internet: turns names like google.com into IP addresses.

⏱ 13 min readBeginnerMust KnowInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

DNS (Domain Name System) turns a name people can remember, like google.com, into an IP address computers use, like 142.250.x.x.

  • It is the phone book of the internet.
  • It is spread over many servers in a tree: root, TLD, authoritative.
  • Answers are cached for a time called TTL, so most lookups are very fast.
  • Almost every request on the internet starts with a DNS lookup.

02Concept: What is it?

Computers find each other by IP address. People are bad at remembering numbers like 203.0.113.10, but good at remembering names like shop.com. DNS is the system that does the translation.

The idea
πŸ”€shop.comeasy for people
→↓DNS
πŸ”’203.0.113.10needed by computers

A name has levels, read from right to left

www.shop.com.
.Root
←
comTop-level domain (TLD)
←
shopDomain (second level)
←
wwwSubdomain
πŸ“Not one server
There is no single DNS server. Thousands of servers share the work. No one server knows everything. Each knows only its own part, and who to ask next.

03Real-Life Example

🍡Finding a friend’s house
You want to visit Ravi but do not know his address. You ask the local post office. If the clerk does not know, they ask the country office (β€œWho handles the city Pune?”), then the city office (β€œWho handles Kothrud?”), then the area office that finally knows Ravi’s exact street. The clerk writes it on a sticky note, so the next time someone asks, the answer is instant.
Finding Ravi
DNS
You
Browser
Local post office clerk
DNS resolver
Country office
Root server
City office
TLD server (.com, .in)
Area office that knows the street
Authoritative server
Sticky note
Cache (with a TTL)
Ravi’s street address
IP address

04Why Do We Need It?

🧠Names are easy
Nobody wants to type 142.250.80.46.
πŸ”„IPs can change
The owner can move the site to a new server. The name stays the same.
βš–οΈMany servers, one name
One name can point to many servers. This is the base of DNS load balancing.
🌍Nearest server
DNS can give users in different countries different IPs, so each gets a close server.
πŸ“§More than websites
DNS also tells where to send email and proves domain ownership.
πŸ“ˆScales hugely
A tree of servers plus caching handles billions of lookups a day.
Without DNS, you would need to remember an IP address for every website you use.

05Where Do We Use It?

🌐Every website visit
The first step before connecting.
πŸ“§Email delivery
MX records say which server takes mail for a domain.
πŸ”ŒAPIs and microservices
Services find each other by name (service discovery).
πŸš€Deployments
Switch traffic to a new server by changing a record.
🌍CDNs
Names point to the nearest edge server.
πŸ›‘οΈSecurity checks
TXT records prove you own a domain (for email or certificates).

06Types / Parts / How It Works

The servers involved

Who is who in DNS
πŸ’»Stub resolverin your device (browser + OS)
→↓
🧭Recursive resolverfinds the answer for you (ISP, 8.8.8.8, 1.1.1.1)
→↓
Name servers
🌐Rootknows the TLD servers
🏒TLDknows who owns each domain
πŸ“’Authoritativehas the real records

Record types

TypeWhat it holdsExample
AA name to an IPv4 addressshop.com β†’ 203.0.113.10
AAAAA name to an IPv6 addressshop.com β†’ 2001:db8::10
CNAMEA name that is an alias of another namewww.shop.com β†’ shop.com
MXWhich servers receive emailshop.com β†’ mail.shop.com (priority 10)
NSWhich name servers are in chargeshop.com β†’ ns1.dnshost.com
TXTFree text, used for proofs and email rulesβ€œv=spf1 include:mailer.com ~all”
PTRAn IP back to a name (reverse lookup)203.0.113.10 β†’ shop.com
SOAAdmin details of the zoneSerial number, refresh times

Two kinds of query

Recursive
device β†’ resolver
  • β€œGet me the final answer.”
  • The resolver does all the hard work
  • What your browser asks
Iterative
resolver β†’ name servers
  • β€œTell me who to ask next.”
  • Each server gives a pointer, not the full answer
  • How the resolver walks the tree
πŸ“Where is the cache?
There are caches in the browser, the operating system, and the resolver. A record is kept until its TTL (time to live, in seconds) runs out.
VisualizerFollow a DNS lookupPick what is already cached, then press Next to walk the lookup.
What is already cached?
πŸ’»Browserown cache
🧭ResolverISP or 8.8.8.8
🌐Root serverβ€œ.”
🏒TLD server.com
πŸ“’Authoritativeshop.com’s DNS
shop.com ? β†’ not in cache
Step 1 of 7
Browser checks its own cache
You typed shop.com. The browser (and the operating system) look in their cache first. Nothing found.
Servers asked
4
after the browser
Speed
slowest
VisualizerTTL: why DNS changes take timeChange the TTL and drag the clock. See who still reaches the old server.
TTL set by the site owner
πŸ•‘Minute 0you look up shop.com β†’ 203.0.113.10
🚚Minute 2owner moves site to 198.51.100.7
πŸ‘€Minute 3cache still valid
You connect to
203.0.113.10
OLD server βœ—
Lookups / hour
12
per resolver
The owner moved the site, but your cached answer is still valid for 2 more minute(s). You still go to the old server. This delay is what people call β€œDNS propagation”. A short TTL changes faster but makes more lookups. A long TTL is cheaper but slower to change.

07How to Use It / Steps / Implementation

The lookup, in order

  1. 1
    Browser cache
    Already know the IP? Use it.
  2. 2
    Operating system cache and hosts file
    Checked next.
  3. 3
    Recursive resolver
    Asked next. If it has the answer cached, it replies at once.
  4. 4
    Root server
    Replies with the .com TLD servers.
  5. 5
    TLD server
    Replies with the domain’s authoritative servers.
  6. 6
    Authoritative server
    Replies with the real record and its TTL.
  7. 7
    Cache and connect
    Everyone on the path caches the answer, and the browser connects.

Setting up DNS for your own site

  1. Buy a domain from a registrar. They set the NS records at the TLD.
  2. Pick a DNS host (the registrar, or a cloud DNS service). It runs your authoritative servers.
  3. Add records: an A record for the root, a CNAME for www, MX for email.
  4. Choose a TTL. Use a short TTL (like 60s) before a planned move, and a longer one (like 1 hour) normally.
  5. Test with a lookup tool.
Example zone records
shop.com.      300  IN  A      203.0.113.10
shop.com.      300  IN  AAAA   2001:db8::10
www.shop.com.  300  IN  CNAME  shop.com.
shop.com.     3600  IN  MX     10 mail.shop.com.
shop.com.     3600  IN  TXT    "v=spf1 include:mailer.com ~all"
Try it yourself (terminal)
dig shop.com            # ask for the A record
dig +trace shop.com     # walk root β†’ TLD β†’ authoritative
nslookup shop.com       # works on Windows too

08Key Terms to Know

TermMeaning in easy words
Domain nameThe human-friendly name, like shop.com
IP addressThe number that computers use to find each other
ResolverThe server that looks up answers for you (ISP or public, like 8.8.8.8)
Root serverTop of the tree. Knows the TLD servers
TLDTop-level domain: .com, .org, .in, .uk
Authoritative serverThe server that holds the real records for a domain
ZoneThe set of records for a domain
RecordOne entry in DNS, such as A, MX or TXT
TTLHow many seconds an answer may be cached
RegistrarThe company you buy a domain from
PropagationThe time old cached answers take to expire after a change
DNSSECSignatures that prove a DNS answer was not faked
Port 53The port DNS uses, mostly over UDP (and TCP for large answers)

09Pros, Cons and Trade-offs

βœ… Pros
  • Easy names instead of numbers
  • Spread over many servers, so it scales
  • Caching makes it fast and cheap
  • Can steer users to the nearest server
  • Can move a site without changing its name
⚠️ Cons
  • Cache delay: changes are not instant
  • If DNS is down, sites seem down
  • Plain DNS can be faked or spied on
  • Extra lookup time on first visit
  • Few big providers are a risk if they fail
ChoiceWe gainWe lose
Short TTLFast changes and failoverMore lookups, more load, slightly slower
Long TTLFewer lookups, fasterSlow to change or recover
Public resolver (8.8.8.8, 1.1.1.1)Often fast and reliableA third party sees your lookups
DNSSECProof the answer is realMore setup and risk of mistakes
DNS over HTTPS (DoH)Lookups are private from the local networkHarder for networks to filter

10Common Problems and Mistakes

Common mistakes

  1. Long TTL before a migration. Users keep going to the old server for hours. Lower the TTL days before the move.
  2. Putting a CNAME on the root domain. Standard DNS does not allow it. Many providers offer an ALIAS or ANAME record for this.
  3. Forgetting IPv6. Without an AAAA record, IPv6 users may be slower or cut off.
  4. Only one DNS provider. If it fails, your whole site disappears. Big sites use more than one.
  5. Letting a domain expire. Someone else can buy it. Turn on auto-renew.
  6. Dangling records. A CNAME that points to a deleted service can be taken over by an attacker.

Known problems

☠️Cache poisoning
A fake answer gets into a resolver’s cache. DNSSEC and random query IDs help.
🌊DDoS on DNS
In 2016 an attack on the DNS provider Dyn made many big sites hard to reach.
πŸ•΅οΈSpying
Plain DNS is readable by the network. DoH and DoT encrypt it.
🧱Blocked or hijacked DNS
Some networks return fake answers. Use a trusted resolver.

11Real-World Examples

πŸ”ŽPublic resolvers
Google 8.8.8.8, Cloudflare 1.1.1.1 and Quad9 9.9.9.9 are used by millions of people.
🌍CDNs
A CDN name returns an IP close to the user. Two people in different countries can get different answers.
☁️Cloud DNS
AWS Route 53 and similar services host records and can do health checks and failover.
🧩Service discovery
Inside clusters, services find each other by names like payments.internal.
πŸ“§Email
MX, SPF and DKIM records decide where mail goes and if it is trusted.
πŸ”Certificates
A TXT record can prove you own a domain before you get a certificate.
DNS steering users to the nearest server
πŸ§‘User in India
πŸ§‘User in Europe
→↓shop.com?
πŸ“’Smart DNS
→↓different IPs
πŸ–₯️Mumbai serverIP A
πŸ–₯️Frankfurt serverIP B
India path Europe path
The same name can return different IPs for different users. More in the DNS Load Balancing topic.
  1. 1
    Day -3: lower TTL to 60 seconds
    So caches expire quickly.
  2. 2
    Day 0: switch the A record to the new IP
    New lookups go to the new server within a minute.
  3. 3
    Day +1: raise TTL again
    Back to fewer lookups.

12Related Topics to Read Next

IP Address→

DNS gives you the IP address that you then connect to.

TCP vs UDP→

DNS mostly uses UDP on port 53.

HTTP / HTTPS→

The browser does a DNS lookup before every new HTTP request.

DNS Load Balancing→

Share traffic by giving out different IPs.

Anycast Routing→

How the root and public resolvers have many servers on one IP.

Single Point of Failure (SPOF)β†’

One DNS provider can be a SPOF.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”DNS: name β†’ IP address. The phone book of the internet.
  • βœ”Path: browser β†’ OS β†’ resolver β†’ root β†’ TLD β†’ authoritative.
  • βœ”Records: A (IPv4), AAAA (IPv6), CNAME (alias), MX (mail), NS (name servers), TXT (text).
  • βœ”TTL: cache time. Lower it before a move.
  • βœ”Port: 53, mostly UDP.
  • βœ”Risks: cache poisoning, DDoS, spying. Use DNSSEC and DoH where it helps.
  • βœ”Uses: websites, email, service discovery, CDNs, load balancing.
  • βœ”One line: β€œDNS is a cached, worldwide lookup that turns names into addresses.”
← Previous
HTTP / HTTPS
Next β†’
Proxy vs Reverse Proxy