A classic question
You type example.com into your browser and press Enter. A moment later a page appears. What happened in between?
It is one of the most common interview questions in software, because a complete answer touches almost everything: names, connections, encryption, servers and rendering. This page follows one request through six stops, from the key press to the first byte of the reply.
You type example.com and press Enter. Follow the messages, and count the round trips.
Stop 1: DNS, turning a name into a number
Machines on the internet find each other by IP address, a number such as 203.0.113.10. Names like example.com exist for people. So the first job is a lookup: what is the address for this name?
The system that answers is DNS, the Domain Name System. It works like a chain of directories.
Check close to home first
The browser, then your operating system, then your router each keep a short-term memory of recent answers. Usually one of them already knows.
Otherwise ask a resolver
Typically run by your internet provider or a public DNS service.
The resolver asks its way down
A root server says who handles
.com. That server says who handlesexample.com. That one gives the address.The answer is remembered for a while
Each answer comes with a lifetime, so the next lookup is instant.
Stop 2: TCP, opening a connection
The internet delivers small packets and promises very little: packets can be lost, duplicated or arrive out of order. TCP builds something reliable on top: a connection where bytes arrive complete and in order.
Setting one up takes a three-message exchange, the three-way handshake:
- SYN. "I would like to talk."
- SYN-ACK. "Agreed. Can you hear me?"
- ACK. "Yes."
Now each side knows the other is there and listening.
Stop 3: TLS, making it private
The address starts with https, so before anything personal is sent, the two sides set up encryption. This is TLS.
The server proves its identity with a certificate, and both sides agree on a secret key that nobody listening can work out. From here on, everything is scrambled. The full story is in HTTPS and the TLS Handshake.
Stop 4: HTTP, the actual request
Only now does the browser say what it wants:
GET / HTTP/1.1
Host: example.com
Accept: text/htmlGET means "give me". / is the home page. The headers add details: which site, which formats are acceptable, any cookies.
Stop 5: the server does its work
The request rarely goes straight to one program. It may pass a load balancer, which picks one of many servers. That server runs application code, which may check a cache or query a database, and assembles a response.
Stop 6: the first byte comes back
HTTP/1.1 200 OK
Content-Type: text/html
<!doctype html>...A status line (200 OK means success), headers, then the page itself. The time from pressing Enter to this moment is called time to first byte.
Where the time goes
Count the arrows in the animation before the GET is sent.
Example
Three round trips before the request
One round trip for DNS. One for the TCP handshake. One for TLS.
Three full trips there and back, before the server knows which page you want. If you are far from the server and each round trip takes a tenth of a second, a third of a second is gone on greetings alone.
That is why so much engineering goes into avoiding these steps:
- DNS answers are cached, so repeat lookups cost nothing.
- Connections are kept open and reused for the next request, skipping stops 2 and 3.
- Content delivery networks put servers close to users, making every round trip shorter.
- Newer protocols fold the connection and encryption handshakes into fewer trips.
And then the browser gets to work
The first byte is not the end. The HTML arrives, and it refers to more things: stylesheets, scripts, images, fonts. The browser requests each of those, mostly over the connection it already has. It builds the page structure, works out the layout, and paints it to the screen. Scripts then run and may fetch still more data.
A modern page is often a hundred requests. The six stops above are what each new connection costs.
Key takeaways
- DNS turns the name into an IP address.
- TCP opens a reliable connection with a three-message handshake.
- TLS proves the server's identity and sets up encryption.
- Only then is the HTTP request sent, handled by the server, and answered.
- Three round trips happen before the request leaves, which is why caching and reusing connections matter so much.