Sub topic 1
Why we need DNS
Prerequisites
This chapter assumes you know what IPv4 and IPv6 addresses are, how a client connects to a server on a port, and how to run commands in a terminal. It also uses Python 3 for one short example.
The problem DNS solves
Computers reach each other by IP address, such as 192.0.2.10 or 2001:db8::10. People remember names such as example.com. Something has to translate between the two, and that translation is name resolution.
Names also solve a second problem. A service's address can change when you move hosts, change providers, or add servers. If every user had the address hardcoded, each change would break them. A name gives clients a stable label, and you update the mapping behind it.
The hosts file: the first answer
Early networks used a single file, HOSTS.TXT, that listed every name and address. One administrator maintained it, and every site downloaded a fresh copy. This approach failed as the network grew:
- The file got larger and had to be copied to every machine.
- Updates reached machines slowly, so copies were often out of date.
- One group controlled all names, which created a bottleneck.
- Two sites could not easily choose the same name without a central check.
DNS, the Domain Name System, was designed in the 1980s to replace this. Your machine still has a small hosts file (/etc/hosts on Linux and macOS), but now it only holds local overrides.
What DNS is
DNS is a distributed, hierarchical database. Each property addresses one weakness of the central file:
| Property | Meaning | Problem it solves |
|---|---|---|
| Distributed | Many servers each hold part of the data | No single machine holds everything or fails for everyone |
| Hierarchical | Names form a tree, and each branch is managed by its owner | Administration is delegated, so there is no central bottleneck |
| Cached | Answers are stored temporarily and reused | Most lookups never need to travel far |
DNS stores more than addresses. It also holds mail routing, text data, and other record types, which later chapters cover.
Seeing it from an application
Applications rarely talk to DNS directly. They ask the operating system's resolver. This example requests the addresses for a name:
import socket
results = socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM)
for family, socktype, proto, canonname, sockaddr in results:
print(family.name, sockaddr[0])getaddrinfo returns one entry per address. When both IPv4 and IPv6 are available, you get AF_INET and AF_INET6 results. The program never needs to know which servers answered.
The big picture
The sections that follow open the DNS box: the name tree, the servers that hold each part, and the resolvers that walk the tree for you.
Common misunderstandings
- DNS does not carry your web traffic. It only provides the address.
- A name does not map to exactly one address. One name can return several, and different users may get different answers.
- A name resolving does not mean the server is reachable. Resolution and connectivity are separate steps.
Exercise
A company moves its website to a new server with a new IP address. Why does using a name, rather than the old address, make this change easier, and why was a single shared HOSTS.TXT file a poor way to distribute such changes?
Show answer
With a name, only the name-to-address mapping changes. Clients keep using the same name and receive the new address on their next lookup. With HOSTS.TXT, every site had to download an updated copy of one central file, so copies were stale for a long time, the file kept growing, and one administrator had to approve every change.