AI Hints
Home›System Design Tutorial›How the Network Works
System Design · Lesson 13 of 120

DNS Deep Dive

The internet's phone book.

⏱ 2 min read▶ Video at 13:58🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Computers find each other by number, called an IP address. People prefer names, like shoply dot com. DNS, the Domain Name System, turns names into numbers, billions of times a day.

DNS Deep Dive
From the video: DNS Deep Dive
  • Computers use numbers
  • People use names
  • DNS translates

Resolving a name

When you type shoply dot com, your computer first checks its own cache. If the answer isn't there, it asks a resolver, usually run by your internet provider. The resolver asks a root server, which points to the dot com servers. Those point to Shoply's own name server, which finally gives the IP address.

  1. Local cache
  2. Resolver
  3. Root server
  4. .com servers
  5. Shoply's server
Resolving a name
From the video: Resolving a name

Asking DNS from Python

Python can ask DNS too. Get address info returns the addresses for a name. Here we look up localhost, which every computer knows, so this works anywhere. Run it. Localhost maps to one two seven dot zero dot zero dot one, the address that always means this machine.

lookup.pypython
import socket

name = "localhost"
infos = socket.getaddrinfo(name, 80, socket.AF_INET, socket.SOCK_STREAM)
for family, kind, proto, canon, (ip, port) in infos:
    print(f"{name} -> {ip}")
Output
localhost -> 127.0.0.1

DNS in system design

DNS matters in design, for three reasons. Answers are cached for a time called the TTL, so changes take a while to spread. DNS can return different addresses to different users, sending them to the nearest data centre. And if DNS is down, nobody can find you, so it must be very reliable.

keyvalue
TTLhow long answers are cached
Geo DNSnearest data centre
Round-robinseveral addresses
Criticalif it fails, nobody finds you

Summary

DNS turns names into IP addresses, through a chain of servers, with caching at every step. Next, the language browsers and servers speak. HTTP.

Key takeaways
  • Names to numbers
  • Cached with a TTL
  • Can route to the nearest site
Next up: HTTP and HTTPS — how browsers talk