Sub topic 1
Why we need DNS
Prerequisites
This chapter assumes you already know the following:
- What IPv4 and IPv6 addresses are, and that a host can have both.
- The client-server model, and that a service listens on a port (for example, 443 for HTTPS).
- How to run commands in a terminal and read their output.
You do not need to know anything about DNS yet. This chapter explains the problem DNS solves. Later chapters cover how it works.
The problem: networks route numbers, people use names
Every packet on the internet is delivered using an IP address. A client that wants to reach a web server must know an address such as 192.0.2.10 or 2001:db8::10. People do not remember addresses well, and they do not want to. They remember names like example.com.
Software needs something that converts a name into an address before it can open a connection. That conversion is called name resolution. DNS, the Domain Name System, is the system that performs it for the internet.
A name does more than save memory. Consider what an address binds you to:
- An address identifies a network location, not a service. If a company moves its website to a new hosting provider, the address changes. The service is the same.
- Addresses come in two families. A site may be reachable over IPv4, IPv6, or both. A name lets one identifier cover both.
- One service may run on many machines. Large sites answer from dozens of addresses in different regions. A single hard-coded address cannot express that.
A name is a stable identifier. The address behind it is allowed to change. DNS is the layer of indirection that makes this possible.
The first solution: a single hosts file
Early networks had only a few hundred hosts. The mapping from names to addresses was kept in one file, usually called HOSTS.TXT. One central site maintained it, and every machine downloaded a fresh copy on a schedule.
You can still see the descendant of that file on your own machine. On Linux and macOS it is /etc/hosts. On Windows it is C:\Windows\System32\drivers\etc\hosts.
127.0.0.1 localhost
::1 localhost
192.0.2.25 mail.example.comEach line maps an address to one or more names. A lookup is a simple scan of the file.
This design stopped working as networks grew, for several reasons:
- Distribution cost. Every host needed a full copy. As the number of hosts grew, so did the file and the number of machines fetching it.
- Update delay. A change made at the central site was invisible until each host fetched the next copy. In the meantime, some machines used the old address.
- Name collisions. With a flat list, two organizations could not both pick the name
gatewaywithout a central authority to arbitrate every choice. - Single point of control and failure. One site had to accept every change and serve every download.
These are scaling problems, and they are the requirements DNS was designed around.
What a replacement has to do
From the failures above, we can list what a name system for the whole internet must provide.
| Requirement | Why the hosts file fails | How DNS responds |
|---|---|---|
| Unique names without one global editor | One site edits everything | Names are arranged in a hierarchy, and each part is managed by its owner |
| Scales to billions of names | Every host holds every entry | The data is split across many servers, and each server holds only part of it |
| Changes take effect quickly | Copies are refreshed on a schedule | Records carry a time-to-live that controls how long others may reuse them |
| Survives failures | One central source | Each part of the data is served by several servers |
| Fast for the common case | Every lookup needs a local copy | Answers are cached close to the client |
DNS is therefore a distributed, hierarchical database. Distributed means no single machine holds all the data. Hierarchical means names are organized as a tree, so that responsibility for each branch can be handed to a different party. The next chapters take each of these ideas in turn.
What DNS stores
DNS is often described as a phone book for the internet. That description is useful but incomplete. DNS maps names to addresses, and it also maps names to other kinds of data. Some examples:
- The IPv4 address of a host (an
Arecord). - The IPv6 address of a host (an
AAAArecord). - Where to deliver email for a domain (an
MXrecord). - Another name that this name is an alias for (a
CNAMErecord). - Free-form text, used for things like domain ownership checks (a
TXTrecord).
The name to address mapping is the most common use, so this tutorial follows it first. You will meet each record type in detail in a later chapter.
Seeing name resolution in code
Applications rarely talk to DNS directly. They call a function provided by the operating system, which does the work. In Python, socket.getaddrinfo is a thin wrapper over the C library function of the same 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])Each tuple in results describes one way to connect. family tells you whether the address is IPv4 (AF_INET) or IPv6 (AF_INET6). sockaddr[0] is the address itself. A typical run prints one line per address, for example an AF_INET6 line followed by an AF_INET line. The exact addresses and their order depend on your network and on the site.
Two details are worth noticing:
- The function returns a list, not a single address. A name can map to several addresses, and a client is expected to try them.
- The caller asks for a name and a port together. The resolver result is then ready to hand to
connect.
Because getaddrinfo goes through the operating system, it uses the same sources your other programs use. That includes the hosts file, which is checked before any network query on most systems. A later chapter covers this order.
The journey of a lookup, in outline
A full explanation comes later, but a first picture helps when reading the next chapters. When you type example.com into a browser, roughly the following happens.
The key point is that the application never needs to know where the data lives. It asks one nearby helper, and that helper does the searching. The later chapters replace the single "DNS servers" box with the real structure: root servers, top-level domain servers, and the authoritative servers of the domain.
Why this design matters in practice
Understanding the reason for DNS makes several everyday behaviors easier to predict.
- Outages look like network failures. If name resolution fails, nothing else works, even though the network is fine. Browsers report that a site "can't be found" while a ping to its raw IP address still succeeds. This is the quickest test for a DNS problem: connect by address, and compare.
- Changes are not instant everywhere. Because answers are cached to keep lookups fast, a change reaches different users at different times. That is a consequence of the design, not a fault.
- A name is not a single machine. One name may return several addresses, and different users may receive different ones.
- DNS is a dependency of nearly everything. Web, email, software updates and APIs all begin with a lookup. This makes DNS a common target for attack and a common point of failure. Later chapters on security and encrypted DNS build on this.
Common misconceptions
- "DNS is one big server." No single server holds the whole database. The data is spread across many servers run by many organizations.
- "DNS only returns IP addresses." It returns many record types, as shown above.
- "The hosts file is obsolete." It is still consulted, and it is useful for local overrides and testing. It simply cannot serve as the internet-wide system.
- "A name always means one address." It can map to several, and the set can differ by client and by time.
Checking your understanding
Exercise
A colleague says, "We could skip DNS by distributing a hosts file to every client machine." Give two reasons this does not scale to the internet.
Show answer
Any two of the following. Every machine would need a full copy of a very large file, which is costly to store and distribute. Updates would reach machines only when they next fetch the file, so some would use stale addresses. A flat list needs one party to approve every name to prevent collisions. A single central source is also a single point of failure.
Exercise
A website loads when you type its IP address in the browser, but fails with "server not found" when you type its name. Which layer is most likely at fault, and why?
Show answer
Name resolution (DNS). The network path and the web server work, because the connection by address succeeds. Only the step that converts the name to an address fails.
Exercise
Why does socket.getaddrinfo return a list of tuples instead of one address?
Show answer
A name can map to several addresses, for example one IPv4 and one IPv6 address, or several servers for the same service. The function returns all of them so the client can try each one until a connection succeeds.