Introduction to DNS
easy 25 minDNS is the distributed database that turns names such as www.example.com into addresses computers can connect to.
See it run
The parts of DNS, how a name is read, which record to choose, and how to change an address without losing users.
Operations
One lookup
From a typed name to a connection. Each server knows only the next step.
Step 1 of 6
The question
www.example.com?
The browser needs the address of www.example.com and asks its resolver.
Step 1: The question
def resolve(name): if name in cache: return cache[name] tld = ask(ROOT, name) # who runs com? owner = ask(tld, name) # who runs example.com? answer = ask(owner, name) # the record cache[name] = answer, answer.ttl return answer- Questions asked
- 1 to 4
- You learn
- The address
- Answer in a cache
- no walk at all
Say who does the walking: the resolver asks the three servers, not the browser.
In the real world
The same introduction to dns doing real jobs. Pick one and step through it.
Two addresses for one name, and what plain DNS does not notice.
Step 1 of 4
Two answers
Rough sharing
DNS returns both addresses in changing order. Users spread roughly evenly.
Step 1: Two answers
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Putting an IP address in the program
criticalThe address changes one day and every copy of the program breaks.
Wrongconnect("192.0.2.10")Rightconnect("api.example.com")Lowering the TTL at the moment of the change
majorCaches already hold the answer with the old TTL.
Wrongset_ttl(60)set_address(new)Rightset_ttl(60)wait(old_ttl)set_address(new)Resolving once at start-up
majorThe process keeps calling an address that has left, long after the TTL.
WrongADDR = resolve(NAME) # for everRightdef address(): return cache_for_ttl(NAME)Treating round-robin DNS as a load balancer
minorIt spreads lookups and not requests, and it does not see a dead server.
WrongA records: s1, s2, s3RightA record: balancerbalancer checks s1, s2, s3
Interview tips
How to explain it
- A tree of names, each branch run by its owner.
- Root, TLD and authoritative servers point down the tree.
- Answers are records with a time to live.
Memory trick
- You, resolver, root, TLD, authoritative.
- Names are read from right to left.
- CNAME is a nickname.
Reach for it when
- An address may change.
- Users are far apart.
- Customers bring their own domain.
Be careful when
- You need a change in seconds.
- Clients cache for ever.
- Old records point at things you no longer own.