🎓 A1Learner
🔍
0 of 47 lessons completed

How DNS Resolves Names

DNS is the system that turns a site name into the address of the machine that holds it. People remember names more easily than long address numbers.

Looking up a name in the record book

The record book pairs each name with an address. A lookup prints the address stored for the name.

book = {"photos.example": "203.0.113.10", "shop.example": "203.0.113.20"}

for name in ["photos.example", "shop.example"]:
    print(name, "->", book[name])

The cache answers first

The resolver checks its cache before the record book. Each lookup prints the step that answered, and a missing name prints a clear not-found line.

cache = {"photos.example": "203.0.113.10"}
book = {"photos.example": "203.0.113.10", "shop.example": "203.0.113.20"}

def lookup(name):
    if name in cache:
        return "cache answered: " + cache[name]
    if name in book:
        return "record book answered: " + book[name]
    return name + " was not found"

for name in ["photos.example", "shop.example", "games.example"]:
    print(name, "->", lookup(name))

Example

cache = {}
book = {"photos.example": "203.0.113.10", "shop.example": "203.0.113.20"}

def lookup(name):
    if name in cache:
        print(name, "-> cache answered:", cache[name])
        return
    if name in book:
        cache[name] = book[name]
        print(name, "-> record book answered:", book[name])
        return
    print(name, "was not found")

lookup("photos.example")
lookup("photos.example")
lookup("shop.example")
lookup("games.example")

Key points

  • DNS: the system that turns a site name into a machine address.
  • Name: the label a person asks with, such as photos.example.
  • Address: the number that identifies the machine holding the site.
  • Resolver: the part of DNS that does the looking up.
  • Resolver cache: a small store where the resolver keeps recent answers.
  • Record book: the stored list that pairs each name with its address.

In this example, the first lookup for one name reaches the record book and saves the answer in the cache. The second lookup for that name stops at the cache, which shows why the cache is checked first.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A resolver needs the address for a name. Where does it check first?