DNS
How human-readable names like example.com are turned into IP addresses, and how that can steer traffic.
What is it?
Computers connect to IP addresses, but people remember names. The Domain Name System (DNS) is the internet's distributed phone book: it turns a name like api.example.com into an address like 203.0.113.7.
No single server knows every name. DNS is a hierarchy: root servers know who runs .com, the .com servers know who runs example.com, and example.com's authoritative name servers hold the actual records. Answers are cached at every step for a time set by the record's TTL (time to live), so most lookups never leave your machine or your ISP's resolver.
Because DNS decides which IP a client connects to, it's also a simple traffic-steering tool: return different IPs to different users to spread load or send them to the nearest region.
Explain like I'm 10
Asking for directions in a new city: you ask a local guide (the resolver). If they don't know, they ask the tourist office (root), which points them to the district office (.com), which points them to the building manager (authoritative server), who gives the exact address. The guide remembers it for a while so the next person gets an instant answer.
Examples
Looking up records with dig
dig +short example.com A # IPv4 address(es)
dig +short example.com AAAA # IPv6 address(es)
dig +short www.example.com CNAME
dig +short example.com MX # mail servers, with priority
dig +short example.com TXT # SPF, domain verification, etc.
# Follow the full chain from the root servers down:
dig +trace example.comA zone file for a small service
; name TTL type value
example.com. 300 A 203.0.113.10
example.com. 300 A 203.0.113.11
example.com. 300 AAAA 2001:db8::10
www 3600 CNAME example.com.
api 60 CNAME my-lb-123.elb.amazonaws.com.
example.com. 3600 MX 10 mail1.example.com.
example.com. 3600 MX 20 mail2.example.com.
example.com. 3600 TXT "v=spf1 include:_spf.google.com ~all"Two A records give simple round-robin across two servers. The api name has a short 60-second TTL so it can be repointed quickly; stable names like www use an hour.
How it works
The resolution chain for api.example.com, on a cold cache:
1. The app asks the OS stub resolver, which checks its own cache and /etc/hosts. 2. On a miss it asks a recursive resolver (your ISP's, or a public one like 1.1.1.1 or 8.8.8.8), usually over UDP port 53. 3. The recursive resolver asks a root server (13 named root server identities, a–m, each served by many anycast instances worldwide). The root replies with the name servers for .com. 4. It asks a .com TLD server, which replies with example.com's name servers (NS records). 5. It asks the authoritative server, which returns the actual record. 6. The resolver caches every answer for its TTL and returns the IP.
Common record types - A: name → IPv4 address. AAAA: name → IPv6 address. - CNAME: name → another name (an alias). A CNAME can't coexist with other records at the same name, so it can't sit at the zone apex (example.com itself); providers offer ALIAS/ANAME or "CNAME flattening" for that. - MX: mail servers for the domain, with a priority. - TXT: free text, used for SPF, DKIM, DMARC and domain-ownership verification. - Also: NS (delegation), SOA (zone metadata), PTR (reverse lookup), SRV (host and port of a service), CAA (which CAs may issue certificates).
TTL is how many seconds a resolver may cache an answer. Short TTLs (30–60 s) let you fail over quickly but cause more queries; long TTLs (hours) are cheap and fast but changes propagate slowly. Some resolvers and clients (notably some JVM configurations) ignore or extend TTLs.
DNS-based load balancing: return several A records (round-robin), or use GeoDNS / latency-based routing where the authoritative server picks the answer based on the resolver's location (or the client subnet via EDNS Client Subnet) and health checks. It's coarse: clients cache answers, so traffic shifts only as TTLs expire.
App ──▶ Stub resolver (OS cache)
│ miss
▼
Recursive resolver ──1──▶ Root server "ask .com servers"
(ISP / 1.1.1.1) ──2──▶ .com TLD server "ask ns1.example.com"
│ ──3──▶ Authoritative "api = 203.0.113.7, TTL 60"
│ caches answer for TTL
▼
App connects to 203.0.113.7Why does it exist?
A single central list of every hostname could never handle the internet's query volume or the rate of changes. DNS splits ownership by delegation, so each organisation runs its own zone, and caches aggressively so the root and TLD servers see only a tiny share of lookups. It also decouples names from addresses: you can move a service to new machines without changing what users type.
When to use it
Every public service needs DNS. In design work, reach for it to point a domain at a load balancer or CDN, to fail over between regions, to route users to their nearest region with GeoDNS, and to do internal service discovery in simple setups (e.g. Kubernetes cluster DNS).
When not to use it
Don't rely on DNS alone for fast failover or fine-grained load balancing: cached answers mean some clients keep hitting a dead IP for minutes. Put a load balancer behind a stable DNS name for per-request balancing and health checks, and use anycast or a global load balancer when you need sub-minute failover.
Common mistakes
Lowering a TTL at the moment of a migration. Resolvers still hold the old answer for the old TTL, so lower it at least one old-TTL period before the change.
Putting a CNAME at the zone apex, which the DNS spec doesn't allow alongside the required SOA and NS records.
Treating multiple A records as real load balancing. Clients pick and cache unevenly, and there are no health checks unless the DNS provider adds them.
Forgetting that DNS lookups add latency on a cold cache (often tens of milliseconds, sometimes more), and not reusing resolved connections.
Ignoring negative caching: a lookup for a record that doesn't exist yet (NXDOMAIN) is also cached, based on the SOA, so creating it a moment later may not be visible right away.
Practice exercises
- Easy:
Use
dig +traceon a domain you use daily and identify the root, TLD and authoritative servers in the output. - Medium:
You're moving api.example.com from one load balancer to another next Tuesday. The record's TTL is currently 86400. Write a step-by-step plan, with timings, that avoids users hitting the old one for a day.
- Hard:
Design DNS for a service running in us-east, eu-west and ap-south so users reach the nearest healthy region. Explain what happens to a European user when eu-west fails, and how long it takes.
Interview questions
What does DNS do?
It translates human-readable domain names into records such as IP addresses, through a distributed, hierarchical, heavily cached database.
What's the difference between an A record and a CNAME?
An A record maps a name directly to an IPv4 address. A CNAME maps a name to another name, which is then resolved; it's an alias and can't coexist with other records at the same name.
Describe the DNS resolution chain for a name not in any cache.
Stub resolver → recursive resolver → root server (returns .com NS) → TLD server (returns the domain's NS) → authoritative server (returns the record). The recursive resolver caches each step for its TTL.
What is TTL, and what's the trade-off in choosing it?
The number of seconds a resolver may cache an answer. Short TTLs allow fast changes and failover but increase query load and lookup latency; long TTLs are efficient but make changes slow to propagate.
What are MX and TXT records used for?
MX lists the mail servers for a domain with priorities (lower is tried first). TXT holds arbitrary text, commonly SPF, DKIM and DMARC email policies and domain-ownership verification tokens.
Why does DNS mostly use UDP, and when does it use TCP?
Most queries and answers are small and fit in one datagram, so UDP avoids a handshake. TCP is used when a response is too large (truncated flag set), for zone transfers, and by DNS-over-TLS.
How does GeoDNS route users to the nearest region?
The authoritative server looks at the source of the query (the resolver's IP, or the client subnet if EDNS Client Subnet is sent), maps it to a location, and returns the IP of the closest healthy region.
Why is DNS failover slower than load-balancer failover?
Clients, resolvers and sometimes application runtimes cache the old answer until its TTL expires, and some ignore TTLs. A load balancer health-checks backends and stops sending traffic within seconds, per request.
How would you migrate a domain to new infrastructure with no downtime?
Lower the TTL well ahead (at least one old-TTL period), run old and new side by side, switch the record, watch traffic drain from the old setup, keep the old one alive until the max TTL has passed, then raise the TTL again.
What's the difference between GeoDNS and anycast for global routing?
GeoDNS returns different IPs per location, so routing happens at lookup time and is subject to caching. Anycast announces the same IP from many locations via BGP, so the network routes each packet to the nearest site and failover happens at routing speed without DNS changes.