Networking
Life of a request
Every step between typing a URL and seeing a page, with a latency budget for each hop, why the second request is faster, and a method for finding where a slow request loses its time.
"What happens when you type a URL into a browser and press Enter?" is a common interview question because there is no bottom to it. A good answer touches every layer. A great answer attaches numbers to each layer and says where the time actually goes.
This page ties the networking pages together. We follow one request from a browser in Mumbai to a server in Virginia and back, through caches, DNS, TCP, TLS, a CDN, a load balancer, an app server, a cache and a database. Then we put numbers on each step.
The full path
Here is the journey for https://shop.example.com/products/42, with nothing cached yet:
- The browser checks its HTTP cache. A fresh copy ends the story at 0 ms of network time. See HTTP.
- DNS. The browser checks its host cache, then the OS resolver, then a recursive resolver, which asks the authoritative servers on a miss. The CDN's DNS returns the IP of a nearby point of presence (PoP). See DNS.
- TCP handshake to that IP: SYN, SYN-ACK, ACK. One round trip. See networking basics.
- TLS 1.3 handshake: one more round trip, and the client checks the certificate chain. See TLS. With HTTP/3, steps 3 and 4 merge into one QUIC round trip.
- The request reaches the CDN edge. A cacheable response is answered there. Otherwise the edge forwards the request to the origin over a connection it already holds open. See CDNs.
- The origin load balancer picks a healthy app server. See load balancing.
- The app server. The kernel accepts the connection and wakes the event loop through
epoll(async I/O). The framework parses the request, runs middleware such as auth and rate limits, and calls the handler (how code runs). - Cache and database. The handler checks Redis, a sub-millisecond round trip inside the data center. On a miss, it queries Postgres. The query planner (query execution) uses a B-tree index (B-tree storage engines), and the pages come from memory, not SSD. That is the memory hierarchy at work.
- The response is serialized (serialization), compressed, encrypted and sent back through the load balancer and CDN. The CDN may cache it on the way.
- The browser renders. It fetches CSS, JavaScript, fonts and images, often over the same HTTP/2 or HTTP/3 connection. Then it builds the DOM, runs JavaScript, lays out the page and paints it.
Concepts in this section
Read them in order. The first one puts numbers on the path, and the rest use those numbers.
| Step | Concept | What it answers |
|---|---|---|
| 1 | Latency budget and round trips | Where do the 890 ms of a cold request go? |
| 2 | Connection reuse | Why is the second request so much faster? |
| 3 | Measuring a request | How do waterfalls, Server-Timing and traces show the time? |
| 4 | Debugging a slow endpoint | Where do you start when p99 jumps? |
Common mistakes
- Optimizing code before counting round trips. For distant users, the network often costs more than the handler.
- Creating a new HTTP client per request. Each one pays handshakes again.
- Skipping the CDN for dynamic pages. A CDN also shortens handshakes and slow start on misses.
- Looking only at the median. A p99-only problem has a different cause than a problem that moves every request.
Key takeaways
- A cold HTTPS request costs DNS, one round trip for TCP, one for TLS and one for the request. At 200 ms round trip, that is nearly a second before your code matters.
- The speed of light sets a floor. Mumbai to Virginia cannot go below about 130 ms of round trip.
- A CDN helps even on misses, by ending handshakes close to the user and reusing warm origin connections.
- Second requests are faster because DNS, TCP, TLS and slow start are already paid for.
Further learning
ByteByteGo's animated walkthrough covers the same path from URL to rendered page in a few minutes.
What happens when... (alex/what-happens-when) collects the detailed version of this question.
High Performance Browser Networking (Ilya Grigorik) explains each layer in depth.
Related
HTTP/1.1, HTTP/2 and HTTP/3
The protocol of the web. Separates HTTP's meaning (methods, status codes, caching) from its wire format, and shows how each version fixes the latency problem of the one before.
Availability, latency and the tail
Nines, percentiles and the math of composing services. Why p99 matters more than the average, how fan-out amplifies the tail, and how to design for both uptime and speed.
Observability: logs and traces
Structured logs, distributed tracing and OpenTelemetry, so you can ask new questions of a running system instead of guessing which dashboard to open.