Before Anything Can Be Sent, Something Has to Find Out Where to Send It, and Usually That Costs Nothing
A name lookup is a chain of caches that almost always hits. The interesting case is the one that misses, which can cost a quarter of the load and leaves no trace in the page.
The chain of caches
Nothing can be sent anywhere until the name has become a number. That
translation is the first thing that happens after you press enter, and it is
the only step that can be entirely free.
It is free because the answer is usually already somewhere. A lookup does not
go out to the network and ask; it works down a chain of places that might
already know, and stops at the first one that does.
| typical cost, millisecon | share of lookups answere | no packet leaves the mac | answered without a full | |
|---|---|---|---|---|
| the browser own cache | 0 | 60 | 1 | 1 |
| the operating system cac | 0 | 15 | 1 | 1 |
| a caching daemon on the | 1 | 18 | 0 | 1 |
| the resolver your networ | 25 | 5 | 0 | 0 |
| a full walk of the hiera | 180 | 2 | 0 | 0 |
| a lookup that times out | 5000 | 0 | 0 | 0 |
The shape of that table is the point. A step whose average cost is a few
milliseconds and whose worst case is five seconds is not well described by its
average, and tuning it means attacking the tail rather than the mean.
What happens on a miss
When nothing in the chain knows, the resolver has to find out, and the way it
finds out is a walk down a hierarchy.
Two things about this walk are worth keeping. The first is that the referrals
are cached too, which is why the second name you look up in a domain is much
faster than the first even though it was never looked up before. The second is
that each step has a timeout, and when a server does not answer, the resolver
waits before trying another. That wait is what produces the five-second row in
the table.
| step | lookup | where it was answered | cost in milliseconds | running total | what happened |
|---|---|---|---|---|---|
| 1 | the site itself | full walk | 180 | 180 | Nothing cached anywhere. Three exchanges plus the resolver own overhead. The page has not yet been asked for. |
| 2 | the image host | resolver cache | 22 | 202 | A different domain, but the resolver had it from another user a minute ago. This is why a busy shared resolver is faster than a quiet private one. |
| 3 | a second name in the site domain | partial walk | 60 | 262 | The referrals from the first lookup were cached, so only the last exchange was needed. Two thirds of the cost removed by work already done. |
| 4 | the analytics host | full walk, one server slow | 1100 | 1362 | One of the servers did not answer and the resolver waited before trying another. A second and a tenth, spent before a single byte of that script was requested. |
What a miss costs
Because almost every lookup hits, the useful quantity is the expected cost
rather than either extreme.
- the average cost of a lookup across many page loads
- the fraction of lookups answered from a cache, typically above nine in ten
- the cost when nothing is cached, from a hundred milliseconds to several seconds
This is also the argument for reducing the number of distinct names a page
uses. Every additional host is another chance to pay the miss cost, and the
costs do not overlap neatly, because the browser discovers most of those names
only after it has parsed the first response.
Lifetimes and staleness
Every answer arrives with a number attached saying how long it may be kept.
That number is set by whoever runs the domain, and choosing it is a real
decision with two sides.
The asymmetry is worth stating plainly, because the intuition runs the other
way. People reach for long lifetimes to save lookups. But the saving flattens
out quickly: the second visit to a site usually happens within minutes, so a
five-minute lifetime captures most of the benefit a one-day lifetime would. The
cost of the long lifetime, by contrast, does not flatten out at all. It is the
full time it takes to move traffic away from a machine that has failed, and
during an incident that is the number everybody is waiting on.
One more consequence belongs here. Caches along the chain keep the answer for
the lifetime they were given, and nothing can tell them to forget it. There is
no way to reach into the resolvers of the world and invalidate a record. So the
lifetime is not a suggestion about freshness, it is a commitment made in
advance about how slowly you will be able to react, and the time to set it is
before the incident rather than during one.
With the address in hand, the next step is opening a conversation with the
machine it names, which is the subject of the next lesson and costs rather more
round trips than most people expect.
Recap
- A lookup walks a chain of caches, and the overwhelming majority of lookups are answered by one of them without any packet leaving the machine.
- A full miss costs several sequential round trips to different servers, which is why a cold lookup can be a fifth of a page load while a warm one is free.
- Every answer carries a lifetime, and that number decides both how often you pay the full cost and how long a change takes to reach everybody.
This is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents