System designCore9 min

What happens to one request

Follow a single tap on a product page through every machine it touches and back, and see where the quarter of a second goes, what gets reused next time, and where it can break.

1 · The journey

One tap, several machines, a quarter of a second

You are in London and tap a pair of running shoes in an online shop, https://shop.example/shoes/42. About a quarter of a second later the page starts to appear. In between, your request has crossed an ocean, passed through several machines and come back.

Two words first. The client is the program that asks: your browser or app. The server is the program that answers. Every exchange is a request from a client and a response from a server. Here is everyone this one request meets:

Your phoneDNS resolvername → addressLoad balancerpicks a serverApp serverruns the codeCachememoryDatabaseon a miss1 · where is it?2 · connect, ask3 · look up4 · if not cachedYour phoneDNS resolvername → addressLoad balancerpicks a serverApp serverruns the codeCachememoryDatabaseon a miss1 · where is it?2 · connect, ask3 · look up4 · if not cached

The path of one request. The shop in this example runs in a data center across the Atlantic, about 70 ms away there and back.

We follow it in order: find the server, open a connection, pass through the data center, and come back. Then we look at what the trip cost and where it could have failed.

2 · Step 1

First, find out where the server is

Names like shop.example are for people. Machines on the internet find each other by IP address, a number such as 203.0.113.7. Turning the name into the address is the job of the Domain Name System (DNS), a worldwide directory split across many servers.

  1. Check nearby first.

    Your browser and your operating system each remember recent answers. A hit here costs nothing.

  2. Ask a resolver.

    Otherwise the question goes to a resolver, usually run by your internet provider. It keeps its own memory of answers, shared by everyone who uses it.

  3. Walk the directory.

    If the resolver has nothing, it walks down the name from the right. A root server says who handles the ending, such as .com. Those servers say who handles the site's own name, and that server finally gives the address.

  4. Remember the answer.

    Every answer carries a time to live (TTL), often a few minutes. Until it runs out, the next lookup skips the walk.

So a full walk is several round trips, but it is rare: most lookups are answered from a memory a few milliseconds away.

The TTL is a small trade-off of its own. A long one means faster lookups for everyone. A short one means that when you move the service to a new address, the world finds out sooner.

3 · Step 2

Before a word is said, two handshakes

With the address known, the phone opens a connection. Two separate agreements happen before the request itself can be sent:

  • TCP sets up a reliable connection: bytes arrive complete and in order, and lost ones are sent again. It takes three messages, which costs one round trip.
  • TLS makes the connection private. The two sides agree on encryption keys, and the server shows a certificate proving it really is shop.example. With TLS 1.3, the current version, that is one more round trip; older TLS 1.2 needed two.

Three round trips on a new connection: one for TCP, one for TLS, one for the request. At 70 ms each, 210 ms pass before the first byte of the answer, and most of that is waiting for light to cross the ocean.

Newer HTTP/3 runs over a transport that folds both handshakes into a single round trip, which is one reason it exists. Either way, the lesson is the same: a new connection is expensive, so clients keep it open and reuse it.

4 · Step 3

Inside the data center, then out come dozens more requests

The request reaches the data center. Everything from here on is a short hop, well under a millisecond between machines in the same building:

  • Load balancer. It picks one of several healthy app servers. It often ends the TLS here too: it decrypts the request, so the machines behind it do not each carry that work.
  • App server. It runs the code: read the request, find product 42, build the page.
  • Cache. It asks memory first. Popular products are there, and a hit takes well under a millisecond.
  • Database. Only on a miss. Reading from storage takes a few milliseconds more.

The answer travels back the same way: 200 OK and the page's HTML, the text that describes it. But a page is not one file.

The HTML lists everything else the page needs: styles, scripts, photos. Files from the same server reuse the open connection. Files from another server, such as one that holds the photos, cost a new lookup and new handshakes first.

So one tap is really dozens of requests. That is why the reuse in the next section matters so much, and why pages that pull files from many different servers feel slow to start.

5 · The bill

Most of the time is spent traveling, not working

Add it up for the first tap, with the service 70 ms away and 10 ms of work inside the data center. The DNS figure assumes the resolver already knew the answer.

DNS lookup
20 ms
TCP handshake
70 ms
TLS handshake
70 ms
Request and answer
70 ms
Work in the data center
10 ms

About 240 ms for the first tap, and only 10 ms of it is the service doing anything. The rest is distance, paid once per round trip. These are illustrative numbers for this example, not a measurement.

Now tap a second product a moment later. Three things are still there from the first time:

  • The DNS answer, remembered until its TTL runs out.
  • The open connection. Browsers keep connections open and send the next request down the same one.
  • The TLS session. Even if the connection closed, a client that returns soon can resume the old session and skip part of the handshake.

So the second tap costs one round trip plus the work: about 80 ms instead of 240. The first request is the expensive one. What else adds up, and how to cut it, is the subject of Latency.

6 · When it goes wrong

Every hop is a place to be slow or to fail

The user sees one error. The cause can be at any hop, and the error often tells you which one:

Where

What goes wrong

What the user sees

DNS

The name cannot be looked up, or points to an old address

"This site can't be reached"

The network

Messages are lost or a link is cut

A spinner, then a timeout

TLS

The certificate has expired or does not match the name

A browser security warning

Load balancer

No healthy server is left behind it

503 Service Unavailable

App server

It crashes, or sends back something invalid

500 Internal Server Error, or 502 Bad Gateway

Database

It answers too slowly, and the load balancer stops waiting

504 Gateway Timeout

Two things stand out. First, a single request depends on every hop working at the same moment, which is why Availability & reliability multiplies their chances together. Second, the worst failures are often not a dead machine but a slow one: the phone cannot tell a slow server from a lost message, and waits. That puzzle is where Why distributed systems are hard begins.

Check yourself

5 questions

1. You open a site you visited a minute ago. Which step is most likely skipped?
2. A new HTTPS connection uses TLS 1.3, and the server is 70 ms away there and back. Ignoring DNS and server work, when does the first byte of the answer arrive?
3. The load balancer answers 504 Gateway Timeout. Where is the problem most likely?
4. Why is the second tap in the same shop so much faster than the first?
5. The HTML of a product page lists 40 more files, 12 from the shop and 28 photos from a separate image server. Which files cost the most time to start?

Takeaways

Remember this

  • A request finds the server through DNS, opens a connection with TCP and TLS, then passes through a load balancer, an app server, a cache and maybe a database.
  • A new HTTPS connection costs round trips before any work: one for TCP, one for TLS 1.3, one for the request.
  • Most of the time is distance, not work, so the first request is the expensive one.
  • Clients reuse DNS answers, open connections and TLS sessions, which makes the next request far cheaper, and one page is dozens of requests.
  • Every hop can be slow or fail, and the error the user sees often tells you which hop.