Where the time goes
After this: Add up a first visit (280 ms) and a repeat visit (100 ms) from round numbers, and say why the round trips cost the most.
On this page · 6 sections · 3 checks
In lesson 1 the first visit took 280 ms, and most of it was spent waiting for messages to travel. Where exactly did those 280 ms go? And if you could make one part faster, which part should it be?
A table of rough times
The time from asking for something to getting the answer is called . A message to another machine and its answer back is a . Here are the latencies this learning path uses, in milliseconds (ms), from fastest to slowest:
| What | Latency |
|---|---|
| Read a value from the computer's memory | 0.0001 ms |
| Read from an SSD, a fast disk | 0.1 ms |
| A round trip between two machines in one data centre (a building full of servers) | 0.5 ms |
| Ask a cache, a fast store of kept answers (tutorial 3) | 1 ms |
| A database query | 10 ms |
| A round trip from London to Virginia, across the ocean | 80 ms |
These are teaching numbers: rounded on purpose, from rough rules of thumb used in the industry. Real values can be several times larger or smaller, depending on the machines and the network. What matters is the size of the gaps. In the time of one round trip across the ocean, a computer could read its memory 800,000 times.
The page in this tutorial makes two small database queries, at 8 ms each instead of 10.
The floor set by light
Why is a round trip across the ocean so slow? Distance. London to Virginia is about 5,900 km. Light in a glass fibre cable covers about 200,000 km per second.
- One way: 5,900 ÷ 200,000 = 0.0295 seconds, which is 29.5 ms.
- There and back: 2 × 29.5 = 59 ms.
No glass fibre cable can beat 59 ms, because light in glass cannot go faster than that. Even light through empty space, at about 300,000 km per second, would need about 39 ms there and back. Real cables do not run in a straight line, and messages pass through equipment on the way, so this learning path uses 80 ms.
Adding up a first visit
Opening a secure connection takes two round trips: one to open the connection, and one to agree on encryption with . Then the request and its response take a third. Here is the whole first visit:
| Step | Round trips | Time |
|---|---|---|
| Ask DNS for the address | 20 ms | |
| Open the connection | 1 | 80 ms |
| Agree on encryption (TLS) | 1 | 80 ms |
| Send the request, get the response | 1 | 80 ms |
| The server asks the database twice | 16 ms | |
| The server builds the page | 4 ms | |
| Total | 3 | 280 ms |
A repeat visit
Now open the page again 30 seconds later. The browser still remembers the address, so there is no lookup. The connection is still open, so there is no need to open it or agree on encryption again. What is left is one round trip, 80 ms, plus the server's 16 + 4 = 20 ms: 100 ms. The two visits together take 380 ms.
Press Run it and step through both visits. Compare the steps of visit 2 with visit 1, and read each visit's total in the counters under the picture.
- 2 visitsRun it and step through both visits. Compare the steps of visit 1 with the steps of visit 2, and read each visit's total in the counters under the picture.
Press Run it, then step through and watch the requests move.
What to speed up
Of the 280 ms, the three round trips take 240 ms. The server's part, the queries and the work together, takes 20 ms. Making the server's code twice as fast would save 2 ms. Removing one round trip would save 80 ms.
That is why so much of web design is about avoiding trips: remembering answers, as the browser does with DNS, keeping connections open, and, later in this learning path, keeping copies of data close to the user.
Check yourself
The same page, but the server is close by: one round trip takes 20 ms instead of 80 ms. Everything else is the same: DNS 20 ms, three round trips, two queries of 8 ms, 4 ms of work. How long does the first visit take?
Back to the 280 ms first visit. You may make one change. Which one saves the most time?
Someone promises a round trip from London to Virginia in 10 ms, with better cables. Why can that not work?
- Read the table of rough times, from a memory read (0.0001 ms) to a round trip across the ocean (80 ms).
- Say why a London to Virginia round trip cannot be faster than about 59 ms: light in fibre needs that long for the distance.
- Add up a first visit, 280 ms, and a repeat visit, 100 ms, and say which steps the repeat visit skips.
- Say that cutting round trips saves the most time, because each one costs more than all the server's work.