ContentsThe library

From an Address to a Page

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.

FIG 1The chain, with typical hit rate and cost
typical cost, milliseconshare of lookups answereno packet leaves the macanswered without a full
the browser own cache06011
the operating system cac01511
a caching daemon on the 11801
the resolver your networ25500
a full walk of the hiera180200
a lookup that times out 5000000
Three quarters of lookups never leave the machine at all, and over nine in ten are answered without a full walk. The two marked rows are the cases people actually feel: a full walk at a fifth of a second, and a timeout, which at five seconds is longer than the rest of the page load put together and is the real reason a site occasionally appears to be down when it is not.

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.

FIG 2A full lookup, with nothing cached
Three sequential round trips to three different servers, each potentially on another continent. The important word is sequential: this cost cannot be reduced by doing things in parallel, because the answer to each step is the address to use for the next. It can only be reduced by already knowing.

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.

FIG 3The same page, four lookups, in order
steplookupwhere it was answeredcost in millisecondsrunning totalwhat happened
1the site itselffull walk180180Nothing cached anywhere. Three exchanges plus the resolver own overhead. The page has not yet been asked for.
2the image hostresolver cache22202A 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.
3a second name in the site domainpartial walk60262The referrals from the first lookup were cached, so only the last exchange was needed. Two thirds of the cost removed by work already done.
4the analytics hostfull walk, one server slow11001362One 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.
4 steps
Four lookups, one page, and the total is over a second with the slowest single lookup accounting for most of it. The last row is the ordinary case of a third-party host being the slow part of your page, and it happens before any request is sent, so no amount of optimising your own server touches it.

What a miss costs

Because almost every lookup hits, the useful quantity is the expected cost

rather than either extreme.

FIG 4The expected cost of a lookup
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
With a hit rate of 0.93 and a miss costing 200 milliseconds, the average lookup costs 14 milliseconds, which is nothing. The same arithmetic with a miss costing 5 seconds because a server is unreachable gives 350 milliseconds, which is a great deal. The hit rate is not what varies between a good day and a bad one; the miss cost is.

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.

FIG 5Choosing a lifetime
chosen for the ability to fail overchosen to minimise lookups200004000060000800006060: traffic can be moved in a minute300300: the common default36003600: an hour of staleness8640086400: a day, and almost no lookupsrecord lifetime, seconds
The two spans are two different goals and they pull in opposite directions. Measured hit rates improve only slightly above a few minutes, because most repeat lookups happen soon after the first, so the long end buys very little traffic reduction at the cost of a great deal of flexibility. That asymmetry is the argument for the short end.

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

NextOpening a Conversation →

The rest of this course

  1. 01Before Anything Can Be Sent, Something Has to Find Out Where to Send It, and Usually That Costs Nothingyou are here
  2. 02Four Round Trips Before the First Useful Byte, and Every One of Them Is the Speed of Light Doing Its Jobopening only
  3. 03The Network Will Lose Some of Your Packets and Tell Nobody, So Everything Above It Is Built on Noticingopening only
  4. 04Your Connection Is a Hundred Megabits and the First Thing It Sends Is Fourteen Kilobytes, on Purposeopening only
  5. 05After Four Round Trips of Machinery, What Finally Goes Down the Wire Is a Few Lines of Text You Could Have Typedopening only
  6. 06The Fastest Request Is the One That Is Never Sent, and Whether It Is Sent Was Decided by a Line of Text Last Weekopening only
  7. 07You Think You Are Talking to a Server, and You Are Talking to Four Machines That Have Agreed Not to Mention Itopening only
  8. 08The Page Took Two and a Half Seconds, and Four Hundred Milliseconds of That Was Your Codeopening only

Read alongside