Part 7 of 8 · 2 min read
Counting round trips
A worked example that adds up the waiting, and what each design choice removes.
Counting round trips
Think of a phone call to a faraway office where each exchange takes a long pause. Fewer exchanges mean a faster call.
A worked example. All numbers are made up to show the idea. A user in Mumbai opens a page from a server in Virginia. The round trip is 200 milliseconds. The DNS answer is not saved anywhere, and the resolver answers in about one 50 ms exchange. TLS needs one extra round trip here.
| Step | Round trips | Time |
|---|---|---|
| DNS lookup | about 0.25 | 50 ms |
| TCP handshake | 1 | 200 ms |
| TLS handshake | 1 | 200 ms |
| HTTP request and first byte of reply | 1 | 200 ms |
| Total before the page starts to arrive | 650 ms |
The user waits about two thirds of a second before the first byte, and the page is not yet drawn.
Now compare with a server in Mumbai, round trip 20 ms:
| Step | Time |
|---|---|
| DNS (saved at the resolver) | 5 ms |
| TCP handshake | 20 ms |
| TLS handshake | 20 ms |
| HTTP request and first byte | 20 ms |
| Total | 65 ms |
The same steps take ten times less, only because the server is ten times closer.
What removes waiting:
- Servers near users. A smaller round trip shrinks every line. Content delivery networks (module 4) and several data centres (Level 0, module 7) do this.
- Reusing connections. A second request on an open, secure connection skips the DNS, TCP and TLS rows. Only the request line remains: 200 ms or 20 ms.
- Saved DNS answers. A good TTL means most users never wait for the lookup.
- Fewer, bigger requests. One request for ten items costs one round trip. Ten requests in a row cost ten.
- HTTP 2 or 3. They send many requests together.
Read it as
Before looking at code speed, look at the number of round trips. A page that makes 20 requests in a row on a 200 ms link waits 4 seconds, whatever the code does.
Check yourself:
- Which row of the first table shrinks the most if a connection is reused?
- How long would 20 requests in a row take at a round trip of 100 ms?
Words you will meet
| Word | Plain meaning |
|---|---|
| Round trip (RTT) | Time for a message to go and the reply to return |
| Protocol | Agreed rules for how machines talk |
| DNS | The internet's phone book for names and addresses |
| TTL | How long a saved answer may be used |
| TCP | A reliable, ordered stream of bytes |
| Handshake | The opening exchange of a connection |
| Congestion window | The amount of unconfirmed data in the air |
| UDP | A lean, unreliable protocol of single packets |
| TLS | Encryption, authentication and integrity for a connection |
| Certificate | A signed document linking a public key to an owner |
| HTTP, HTTPS | The web's request-and-response protocol, and its secure form |
| Status code | A number that tells the result of a request |