Show contents
Chapter 4 of 7 · 20 min
DNS, the Internet's directory
By the end of this chapter, you'll be able to
- Explain the role of DNS and the hierarchy of domain names
- Describe a name resolution, from the root to the authoritative server
- Understand caching and an answer's time to live (TTL)
- Simulate a DNS resolver in Python
A hierarchical directory
A domain name reads from right to left, from the most general to the most precise: in courses.kalanblon.com, there is the top-level domain com, then the domain kalanblon, then the subdomain courses. Nobody holds the complete directory: each level delegates to the next.
- The root servers know the servers of each top-level domain (
.com,.org,.sn,.fr…). - The
.comservers know which servers are responsible forkalanblon.com. - The latter, called authoritative, finally give the address.
The main record types:
| Type | Content |
|---|---|
A | an IPv4 address |
AAAA | an IPv6 address |
CNAME | an alias to another name |
MX | the server that receives the domain's email |
NS | the authoritative servers for a domain |
A resolution, step by step
Your computer does not walk this path itself: it asks a resolver (often your Internet provider's), which goes through the hierarchy for it. Let's simulate this distributed directory with dictionaries. Names and addresses are fictitious (.example, an address range reserved for documentation):
servers = {
"root": {"example": ("NS", "tld-example")},
"tld-example": {"shop.example": ("NS", "ns-shop")},
"ns-shop": {
"shop.example": ("A", "203.0.113.10"),
"www.shop.example": ("CNAME", "shop.example"),
"api.shop.example": ("A", "203.0.113.20"),
},
}
def resolve(name, server="root", trace=None):
trace = [] if trace is None else trace
zone = servers[server]
for suffix in sorted(zone, key=len, reverse=True):
if name == suffix or name.endswith("." + suffix):
type_, value = zone[suffix]
trace.append(f"{server} → {type_} {value}")
if type_ == "NS":
return resolve(name, value, trace)
if type_ == "CNAME" and name == suffix:
return resolve(value, "root", trace)
if name == suffix:
return value, trace
trace.append(f"{server} → unknown name")
return None, trace
address, steps = resolve("www.shop.example")
print(address)
for s in steps:
print(" ", s)
203.0.113.10
root → NS tld-example
tld-example → NS ns-shop
ns-shop → CNAME shop.example
root → NS tld-example
tld-example → NS ns-shop
ns-shop → A 203.0.113.10
- each server only knows its zone; for the rest, it points to the responsible server (
NS); - the longest suffix is tested first: it is the most precise answer;
www.shop.exampleis an alias (CNAME): the resolution starts again with the real name.
The resolver keeps the answer: the next requests will be immediate.
Text description of the animation
- The browser asks the resolver for the address.
- The root does not know the address, but knows who runs .example.
- The .example server points to the server of shop.example.
- The authoritative server answers: it is an alias, then the final address.
- The resolver keeps the answer: the next requests will be immediate.
Caching and time to live
Walking this whole path on every visit would be slow and would overload the servers. Resolvers and computers cache the answers, for a duration set by the domain owner: the TTL (time to live), in seconds.
class Cache:
def __init__(self):
self.entries = {}
def read(self, name, now):
entry = self.entries.get(name)
if entry and now < entry[1]:
return entry[0]
return None
def write(self, name, address, ttl, now):
self.entries[name] = (address, now + ttl)
cache = Cache()
cache.write("shop.example", "203.0.113.10", ttl=300, now=0)
for t in (10, 299, 300, 400):
print(f"t = {t:>3} s: {cache.read('shop.example', t) or 'expired, ask again'}")
t = 10 s: 203.0.113.10
t = 299 s: 203.0.113.10
t = 300 s: expired, ask again
t = 400 s: expired, ask again
For 300 seconds (5 minutes), the answer is served from the cache, with no network request at all. After that, it expires and must be asked for again.
Mini-lab: your own directoryPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: a resolver with a cache
Write a function resolve_with_cache(name, t) that first checks a cache, otherwise queries the directory (a dictionary name → (address, ttl)), then stores the answer in the cache. It returns the address and the source ("cache" or "network"). Simulate requests for api.shop.example (TTL of 60 s) at times 0, 30, 59, 60 and 90.
Hint 1
(address, expiry_time).Hint 2
t < expiry. Otherwise, query the directory and replace it.Show the solution and common mistakes
directory = {"api.shop.example": ("203.0.113.20", 60)}
cache = {}
def resolve_with_cache(name, t):
if name in cache and t < cache[name][1]:
return cache[name][0], "cache"
address, ttl = directory[name]
cache[name] = (address, t + ttl)
return address, "network"
for t in (0, 30, 59, 60, 90):
print(t, resolve_with_cache("api.shop.example", t))
0 ('203.0.113.20', 'network')
30 ('203.0.113.20', 'cache')
59 ('203.0.113.20', 'cache')
60 ('203.0.113.20', 'network')
90 ('203.0.113.20', 'cache')
At 60 s, the entry has just expired: the network is asked again, and the new answer is valid until 120 s. Over thousands of visitors, this cache avoids the vast majority of DNS requests.
Common mistakes:
- Testing
t <= expiry: the answer would still be served at the exact moment it expires, one second too long. - Keeping entries without an expiry time: the cache would never see address changes.
Summary
- 1DNS translates domain names into IP addresses, thanks to a hierarchical and distributed directory.
- 2A name reads from right to left; each level delegates to the next (NS records).
- 3Common records: A (IPv4), AAAA (IPv6), CNAME (alias), MX (email).
- 4Answers are cached for their time to live (TTL).
- 5After an address change, old answers stay cached until they expire: lower the TTL beforehand.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.