CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 3 of 8 · 3 min read

Finding the address: DNS

How a name becomes an IP address, why it is cached, and what happens when the cache is wrong.

Think of a phone book. Nobody remembers every phone number, so people look names up. There is no single book for the whole world. Instead, there are layers: a book for each country, then one for each city.

Domain Name System

The Domain Name System, or DNS, is the internet's phone book. It is a worldwide, layered system that turns a hostname such as www.example.com into the IP address of a machine.

Example: DNS answers the question "which number belongs to www.example.com?" with an address such as 93.184.216.34.

When a browser opens www.example.com, it does this:

  1. The browser's own memory. If it looked this name up recently, it uses the saved answer.
  2. The resolver. If not, it asks a DNS resolver, usually run by the internet provider. The resolver also keeps saved answers. If it has one, it replies.
  3. The root server. If not, the resolver asks a root name server, which knows only who is in charge of each ending, such as .com.
  4. The ending's server. The resolver asks the server in charge of .com, which knows who is in charge of example.com.
  5. The authoritative server. The resolver asks that server, which holds the true record and returns the address.

The resolver hands the address to the browser, and every step on the way saves a copy.

browser -> resolver -> root server        ("who handles .com?")
                    -> .com server        ("who handles example.com?")
                    -> example.com server ("what is the address of www?")

Saving copies is essential. Without it, every page visit would repeat up to four lookups, and the name servers of the world would collapse under the load. A name rarely moves to another address, so saved copies are usually right.

Time to live

The time to live, or TTL, of a saved answer is how long it may be used before it must be looked up again. A DNS record carries its own TTL.

Example: a record with a TTL of 300 seconds can be reused for five minutes.

The TTL is a trade-off, and a clear one:

  • A long TTL means fewer lookups and less load on the name servers. But when the site moves to a new address, many users keep going to the old one for a long time. Some clients even ignore the TTL, so a small share can reach the old address days later.
  • A short TTL means changes spread quickly. But there are more lookups, which add a little delay and put more load on the name servers.

DNS is also a single point of failure. If the name server of a site is down and no saved copies exist, nobody can find the site, even though the web servers are perfectly healthy. This has caused large outages. Good designs use several name servers and sensible TTLs.

Read it as

A DNS lookup is a question that is usually answered from a saved copy, and only occasionally by walking the layers. The first visit is the slow one.

Check yourself:

  • Why do browsers and resolvers keep saved answers?
  • A site moves to a new server. Why might some users still reach the old one?