How a request travels

  • Lesson 1 of 40
  • 10 min
  • Beginner
  • 10 steps

Follow a page load hop by hop and find out where the time really goes.

DNS lookup
30 ms
Connection
80 ms
Request + server
100 ms
Download
90 ms
Total
300 ms
youDNSresolverDNSWeb serverWeb serverDatabasemiapp.ejemplo→ 203.0.113.7where is miapp.ejemplo?cached · 5 minTCP + TLS handshakecached · 5 minGET /products30 %cached · 5 minGET /products30 %SELECT … FROM productspage ready · 260 mscached · 5 minHTML · 120 KB30 %SELECT … FROM products40 files · 1 connectioncached · 5 minHTTP/2 · many requests at once35 %SELECT … FROM productscached · 5 minGET /products30 %400 ms · scans the whole tableORDER BY score · no indexyou · Buenos AiresVirginiacached · 5 min≈ 150 ms per round tripSELECT … FROM productspage ready · 300 ms30 ms80 + 100 + 90 ms15 msWhere the time wentDNS lookup30 msTCP + TLS handshake80 msrequest + server100 msdownload90 mstotal300 ms150 ms are trips over the network
  • request
  • response
  • handshake

Step 1 of 10

Concept

All you have is a name

You type miapp.ejemplo and press Enter. Your browser knows who you want to talk to, but not where it is.

On the internet machines find each other by IP address, not by name. Before asking for the page, it has to find that out.

Step 2 of 10

Technique

Turning the name into an IP

The browser asks a DNS resolver, which answers with the IP: 203.0.113.7. In the example the lookup takes 30 ms.

The answer carries a TTL: nobody asks again until it expires, so later visits skip this step.

What changed

  • DNS resolver
  • DNS lookup: 30 ms

Step 3 of 10

Technique

Opening a connection

With the IP in hand, the browser opens a TCP connection and, since it is https, agrees on encryption with TLS. Each greeting needs a round trip (RTT).

Here one RTT is 40 ms: TCP plus TLS 1.3 add up to 80 ms and we have not asked for anything yet.

What changed

  • Web server
  • DNS cached
  • Connection: 2 RTT = 80 ms

Step 4 of 10

Technique

Finally, the request

The browser sends GET /products with its headers: cookies, language, accepted formats…

Reaching the server costs half an RTT: 20 ms. The server is relaxed, at 30 % CPU.

What changed

  • GET /products on its way: 20 ms

Step 5 of 10

Technique

The server asks for data

To build the page, the server queries the database. Another trip, this time inside the data center.

The query takes 15 ms and the server's own work 45 ms more: 60 ms in total.

What changed

  • Database
  • Server + query: 60 ms

Step 6 of 10

Technique

The page comes back

The server answers with the HTML (120 KB). It comes back in half an RTT and the rest depends on size: the download adds 50 ms.

We are at 260 ms and the browser can start painting.

What changed

  • Response: HTML · 120 KB
  • Download: 50 ms
  • Total: 260 ms

Step 7 of 10

Solution

The HTML asks for more

Reading the HTML reveals CSS, JavaScript, fonts and images: about 40 files. Each one is another request.

With HTTP/2 they all travel at once over the same connection, with no new greetings: the download only grows to 90 ms.

What changed

  • 40 files over 1 connection
  • Download: 90 ms

Step 8 of 10

Problem

A query without an index

Now the query sorts a large table by score without an index: the database scans all of it and takes 400 ms.

Same code, same network: the page goes from 300 to 685 ms because of a single query.

What changed

  • Query: 15 → 400 ms
  • Total: 300 → 685 ms

Step 9 of 10

Problem

Distance is not negotiable

Now you are in Buenos Aires and the server in Virginia: about 8,300 km. Light in fiber needs at least ~80 ms per round trip, and in practice it is about 150 ms.

With the query fast again, the load grows to 690 ms: every round trip costs almost four times as much.

What changed

  • RTT: 40 → 150 ms
  • Total: 300 → 690 ms

Step 10 of 10

Summary

Where the time goes

DNS, connection, request with server, and download: every phase adds up. Of the 300 ms, 150 are trips over the network.

Levers: cache DNS, reuse connections, serve static files close to the user, indexed queries and fewer files.

You will learn

  • What DNS does and why it is usually fast
  • Why opening a connection costs round trips
  • Which part of the time is your code and which part is the network
  • Why one page is dozens of requests
  • Which levers make everything arrive sooner