Names and addresses: DNS
After this: Say what DNS turns a name into, and count the lookups when its answer is remembered for a fixed time.
On this page · 6 sections · 3 checks
When you call a friend, you tap their name in your phone's contacts. The phone network does not know names, though. Your phone looks up the number saved under the name and dials the number. Opening a website works the same way, and this lesson is about step 1 of the last lesson: finding the address.
A name and a number
People remember names such as shop.example.com. The network moves messages between machines by number. Every machine that can be reached on the internet has an , a number such as 203.0.113.10. To send a request, the browser needs the server's IP address, not its name.
The directory that turns names into addresses is called , the Domain Name System. The browser asks: "what is the address of shop.example.com?" DNS answers: "203.0.113.10". Only then can the browser open a connection.
Who answers the question
The browser does not search the whole directory itself. It asks one server near you, called a , often run by your internet provider. If the resolver does not know the answer, it asks the other parts of DNS in turn and passes the final answer back. In this tutorial the DNS card stands for that whole search, and one lookup costs 20 ms.
Remembering the answer
Asking DNS before every page would waste 20 ms each time. So the browser remembers the answer. But not forever: every answer comes with a , short for time to live, which says how many seconds the answer may be used.
Work through three visits, with a TTL of 300 seconds and visits at 0 s, 120 s and 400 s:
- At 0 s the browser knows nothing, so it asks DNS. The answer is good until 0 + 300 = 300 s.
- At 120 s the answer is still good, because 120 is less than 300. No lookup: the browser goes straight to the server.
- At 400 s the answer has expired, because 400 is past 300. The browser asks DNS again, and the new answer is good until 700 s.
Two lookups for three visits. The rule: a visit asks DNS when the browser has no answer yet, or when the time now has reached the time of the last lookup plus the TTL.
Watch the three visits
Press Run it and watch, at each visit, whether the message goes to DNS first or straight to the server. This picture leaves out the database, so its totals are smaller than in lesson 1; here only the lookups matter.
- 3 visitsRun it and step through. The browser opens the site at 0 s, 120 s and 400 s, and keeps the address it gets for 300 s. At each visit, check whether the request goes to DNS first or straight to the server.
Press Run it, then step through and watch the requests move.
The counter "DNS lookups" ends at 2: one at 0 s and one at 400 s. Visits 2 and 3 also skip opening a connection, because the picture keeps it open between visits. Lesson 4 looks at that saving.
Why not remember forever?
A site can move to a new machine with a new address. A browser that never asked again would keep sending requests to the old address, and the site would seem broken. The TTL is a trade-off chosen by the site's owner. A short TTL notices a move sooner; a long TTL saves more lookups.
Check yourself
The browser asks DNS about shop.example.com. What does DNS send back?
The browser keeps the address DNS gives it for 60 seconds. You open the site at 0 s, 50 s, 70 s and 200 s. How many times does the browser ask DNS?
Remembering the address saves a lookup. Why does the browser not keep it forever?
- Say what DNS does: it turns a name such as shop.example.com into an IP address, the number that finds one machine.
- Say that the answer is remembered for a fixed time, its TTL, so a repeat visit needs no lookup.
- Count the lookups for a list of visit times: a new lookup is needed once the remembered answer has expired.
- Say why the answer is not kept forever: the site may move to a new address.