Back to blog

Where do the first 200 ms of a photo page actually go?

Thắng Đoàn
Thắng Đoàn

FotoFlow has 100 users and one server, and a user just said the feed feels slow. You cannot make anything faster until you know where the time goes, so let's follow one page load from Enter to pixels.

First: find the address

Networks route by number, not by name. So before anything else, the browser asks a DNS resolver: where is fotoflow.example? The answer, an IP address, comes back in about 20 ms.

DNS answers are cached with a time-to-live. For the next few minutes, repeat visits skip this step entirely. Free speed, nothing to build.

Second: open the line

The browser now connects to that IP. A TCP handshake agrees that both machines will talk. A TLS handshake on top adds encryption. Every web request is a conversation between machines, and each step costs time. Each handshake needs a round trip, a there-and-back.

Here a round trip is 30 ms, so connecting costs 60 ms before a single byte of the page is requested.

Third: ask, query, answer

Over the open connection the browser sends its request. The server parses it and calls your code. The photos do not live in the web server; they live in a database, so the server sends a query and waits for rows. That is one more round trip plus about 10 ms of real work. The server turns rows into HTML, and downloading the page takes another 20 ms. The browser renders.

The clock says 155 ms since Enter. Then the browser spots the images, stylesheets and scripts in the page, and fires off roughly 40 more requests. Reusing the already-open connection saves every one of them the handshake cost. Modern HTTP/2 pushes them all through that single connection at once.

Where the time actually went

Add it up. DNS, handshakes, request, response, download. Only about 25 of the 155 ms is your server actually working. The rest is the network. That changes what you optimize.

Two quick cases prove it. A new recommendation box ships, and its query sorts the whole photo table on every page view. The database takes 400 ms, and the page jumps past half a second. Same network, same handshakes. The fix is in the query, not the wires. Second case: same page, but opened from Sydney while the server sits in Virginia. Every round trip now costs 200 ms instead of 30. Two handshakes plus the request round trip burn 600 ms before download even starts. A round trip is set by distance and the network, not by how fast your server is.

What this does not solve

Knowing where time goes does not fix the two usual suspects. The slow query needs an index, which is its own post. The Sydney problem needs content closer to users, which is a CDN, also its own post. This post only gives you the map.

When your app felt slow last time, did anyone actually measure, or did everyone just upgrade something?
If a page loads in 155 ms but takes 555 ms for one user, is that a server problem or a geography problem?

Measure first, upgrade second.

Next in the series: Is your server slow, or is it just busy?

Sources

MDN: An overview of HTTP

Cloudflare: What is DNS?

Cloudflare: What is TLS?

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.