You type https://shop.example.com/products/42 and press Enter. Within a few hundred milliseconds your browser has found a server on the other side of the planet, opened an encrypted connection, asked for a page, and painted it on screen. Tracing that journey step by step touches almost every layer of system design: caching, DNS, TCP, TLS, HTTP, load balancing, application servers, databases and rendering.
Think of it like
Visiting a shop in an unfamiliar city
First you check whether you already know the address (cache). If not, you look it up in a directory (DNS). You travel there and knock to make sure someone answers (TCP handshake), show ID and agree on a private code with the clerk (TLS), then place your order (HTTP request). The clerk fetches it from the stockroom (app server and database) and hands it over (response), and you assemble it at home (rendering). On your next visit you remember the address and the clerk already knows you, so it is much faster (caching and keep-alive).
The client-server model
The web is built on a simple contract: a client (browser, mobile app, or another server) sends a request, and a server sends back a response. The client always initiates. The server listens on a well-known port, handles many clients at once, and keeps no memory of previous requests unless it chooses to (HTTP itself is stateless; cookies and sessions add state on top).
Client
- Initiates every exchange
- Resolves names, opens connections, sends requests
- Caches responses to avoid repeat work
- Renders the result for a human (or parses it, for an API client)
Server
- Listens on a port (443 for HTTPS, 80 for HTTP)
- Authenticates and authorizes the caller
- Runs business logic, reads and writes data
- Returns a status code, headers and a body
Anatomy of a URL
https://shop.example.com:443/products/42?color=red&size=m#reviews
└─┬─┘ └──────┬───────┘ └┬┘└────┬─────┘└───────┬───────┘└──┬───┘
scheme host port path query fragment| Part | Example | What it does |
|---|---|---|
| Scheme | https | Protocol to use. https means HTTP over TLS, default port 443. |
| Host | shop.example.com | Resolved to an IP address via DNS. shop is a subdomain, example the registered domain, com the top-level domain. |
| Port | :443 | Which process on the server to talk to. Omitted when it is the scheme's default. |
| Path | /products/42 | Identifies the resource on that server. |
| Query | ?color=red&size=m | Key-value parameters for filtering, searching or pagination. |
| Fragment | #reviews | A position within the page. Never sent to the server; handled entirely by the browser. |
The journey of a request
Here is the whole trip on one diagram. The first flow is a cold first visit, where every step is paid in full. The second is a repeat visit, where caches and an already-open connection skip most of the work. Step through the simulation above for a timeline view of the same phases.
Step by step
1. Check the caches
The fastest network request is the one you never make. Before touching the network, the browser consults a chain of caches, each closer to the user than the last.
- HTTP cache (memory and disk): fresh responses are reused without asking the server. Controlled by
Cache-Control,ETagandLast-Modified(see HTTP, HTTPS & TLS). - Service worker: a script that can intercept requests and answer from its own cache, enabling offline apps.
- DNS caches: the browser, the operating system and the recursive resolver each cache answers for the record's TTL.
2. DNS resolution
Computers route by IP address, not by name. DNS is the internet's distributed phone book: a hierarchy of root, top-level domain (TLD) and authoritative servers, fronted by recursive resolvers that do the legwork and cache aggressively. A cold lookup can cost tens to hundreds of milliseconds; a cached one costs nothing. Domain Name System (DNS) covers record types, TTLs and GeoDNS in depth.
3. TCP connection
HTTP/1.1 and HTTP/2 run on TCP, which provides a reliable, ordered byte stream over an unreliable network. Before any data flows, client and server perform a three-way handshake (SYN, SYN-ACK, ACK) to agree on starting sequence numbers. That costs one full round trip. The details, plus flow and congestion control, are in Networking Essentials.
4. TLS handshake
For HTTPS, the client and server then run a TLS handshake. The server proves its identity with a certificate signed by a trusted certificate authority, and both sides derive shared session keys using an ephemeral Diffie-Hellman exchange. TLS 1.3 needs one round trip (TLS 1.2 needed two), and resumed sessions can even send data in the first flight (0-RTT). See HTTP, HTTPS & TLS.
5. HTTP request and response
Now the browser sends the actual request: a method, a path, headers and an optional body. The server replies with a status code, headers and a body. Over HTTP/2 and HTTP/3 these are encoded as binary frames, but the meaning is identical to the readable HTTP/1.1 text below.
GET /products/42?color=red HTTP/1.1
Host: shop.example.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5)
Accept: text/html
Accept-Encoding: gzip, br
Cookie: session=8f3a91c2
Connection: keep-alive
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Encoding: br
Content-Length: 18342
Cache-Control: private, max-age=0, must-revalidate
ETag: "p42-v7"
<!doctype html><html><head><link rel="stylesheet" href="/app.css">...6. The server side
- 1
Edge and CDN
Static assets (and sometimes whole cached pages) are served from a CDN point of presence near the user, so the request may never reach your data center. See Content Delivery Networks.
- 2
Load balancer
The public entry point. It terminates TLS, runs health checks and spreads requests across many app servers. See Load Balancing.
- 3
Gateway or reverse proxy
Often combined with the load balancer: authentication, rate limiting, routing
/api/*and/static/*to different backends. See Proxies & API Gateways. - 4
Application server
Parses the request, identifies the user from the session cookie or token, runs business logic and assembles the response. It should be stateless so any instance can serve any request.
- 5
Cache and database
Hot data comes from an in-memory cache; everything else from the database, the durable source of truth. See Caching and SQL vs NoSQL Databases.
- 6
Response
The server sets a status code, headers (content type, caching rules, cookies), compresses the body with gzip or Brotli, and sends it back along the same path.
7. Rendering: the critical rendering path
The browser does not wait for the whole page before starting. It streams the HTML through a pipeline, and anything that blocks the pipeline delays the first paint. That chain of work is the critical rendering path.
- 1
Parse HTML into the DOM
The parser builds the Document Object Model incrementally. A preload scanner looks ahead for CSS, JS and images and starts fetching them early.
- 2
Parse CSS into the CSSOM
CSS is render-blocking: the browser will not paint until it has the stylesheets, otherwise content would flash unstyled.
- 3
Run JavaScript
A classic
<script>blocks parsing until it downloads and runs, because it might modify the DOM.defer,asyncandtype="module"scripts do not block. - 4
Build the render tree
Combine DOM and CSSOM, keeping only visible nodes (
display: noneelements are dropped). - 5
Layout
Compute the exact size and position of every box for the current viewport (also called reflow).
- 6
Paint and composite
Fill in pixels for text, colors and images on layers, then the GPU composites the layers onto the screen.
8. Keep-alive and connection reuse
A page typically needs dozens of resources. Paying for DNS, TCP and TLS on every one would be ruinous, so connections are persistent by default in HTTP/1.1 (Connection: keep-alive). HTTP/1.1 browsers open up to about 6 parallel connections per origin; HTTP/2 and HTTP/3 multiplex every request over a single connection. Idle connections are closed after a timeout, and TLS session resumption makes reconnecting cheaper.
Where the time goes
| Phase | Round trips | At 50 ms RTT | How to avoid paying it |
|---|---|---|---|
| DNS lookup | 0 (cached) to several | 0 to 100+ ms | DNS caching, sensible TTLs, dns-prefetch |
| TCP handshake | 1 | 50 ms | Keep-alive, connection pooling |
| TLS 1.3 handshake | 1 (TLS 1.2: 2) | 50 ms | Session resumption, 0-RTT, HTTP/3 |
| Request to first byte | 1 + server time | 50 ms + server | CDN, caching, faster backend |
| Subresources (CSS, JS, images) | 1 or more | 50+ ms | HTTP/2 multiplexing, preload, CDN, browser cache |
Cold HTTPS request
~4 RTT
DNS + TCP + TLS 1.3 + HTTP, plus server time
Same, over HTTP/3
~3 RTT
QUIC merges transport and TLS handshakes
Warm connection
1 RTT
Keep-alive: just the request itself
Fresh browser cache
0 RTT
No network at all
Measure it yourself
curl -s -o /dev/null https://example.com/ -w '
dns lookup: %{time_namelookup}s
tcp connected: %{time_connect}s
tls done: %{time_appconnect}s
first byte: %{time_starttransfer}s
total: %{time_total}s
'Implementation
Do DNS, TCP, TLS and HTTP as separate explicit steps so each cost is visible.
import socket
import ssl
import time
def ms(start: float, end: float) -> str:
return f"{(end - start) * 1000:7.1f} ms"
def timed_get(host: str, path: str = "/") -> None:
t0 = time.perf_counter()
# 1. DNS: resolve the host name to an IP address
family, kind, proto, _, addr = socket.getaddrinfo(host, 443, type=socket.SOCK_STREAM)[0]
t_dns = time.perf_counter()
# 2. TCP: three-way handshake (one round trip)
sock = socket.socket(family, kind, proto)
sock.connect(addr)
t_tcp = time.perf_counter()
# 3. TLS: handshake + certificate validation against the system CA store
ctx = ssl.create_default_context()
tls = ctx.wrap_socket(sock, server_hostname=host)
t_tls = time.perf_counter()
version = tls.version()
# 4. HTTP: send the request and wait for the first byte
request = (
f"GET {path} HTTP/1.1\r\n"
f"Host: {host}\r\n"
"Accept: text/html\r\n"
"Connection: close\r\n"
"\r\n"
)
tls.sendall(request.encode())
body = tls.recv(65536)
t_ttfb = time.perf_counter()
while chunk := tls.recv(65536):
body += chunk
tls.close()
t_done = time.perf_counter()
print("status ", body.split(b"\r\n", 1)[0].decode())
print("dns ", ms(t0, t_dns))
print("tcp ", ms(t_dns, t_tcp))
print("tls ", ms(t_tcp, t_tls), version)
print("ttfb ", ms(t_tls, t_ttfb))
print("download ", ms(t_ttfb, t_done), len(body), "bytes")
timed_get("example.com")Key takeaways
- The client always initiates; HTTP is stateless request/response, with cookies and sessions layered on top.
- A cold HTTPS request pays for DNS, a TCP handshake, a TLS handshake and the request itself: about 4 round trips before the first byte.
- Caches at every layer (browser, OS, resolver, CDN, server-side cache) exist to skip some or all of those steps.
- On the server side: load balancer, stateless app servers, then a cache in front of the database.
- Rendering is a pipeline: CSS blocks paint and classic scripts block parsing, so defer what is not critical.
- Keep-alive and HTTP/2 multiplexing amortize handshake costs across many requests.
Common pitfalls
- Thinking the URL fragment (
#section) reaches the server. It never does. - Ignoring round-trip time: adding bandwidth does little for page load, while removing handshakes and moving content closer helps a lot.
- Putting large synchronous
<script>tags in<head>, which blocks parsing and delays first paint. - Forgetting that DNS answers are cached for their TTL, so a DNS change does not take effect everywhere immediately.
Interview tips
- Answer the "type a URL" question breadth-first, then offer to go deep on any one layer.
- Quantify: name the round trips (TCP 1, TLS 1.3 1, HTTP 1) and how caching, keep-alive and CDNs remove them.
- Mention where TLS is terminated (usually the load balancer) and why app servers are stateless.
Real-world applications
- Debugging slow page loads with browser dev tools' network waterfall.
- Choosing CDN, caching and connection settings for a web application.
- Explaining infrastructure choices (TLS termination, load balancers) in design reviews.
Test yourself
Which part of https://a.com/p?q=1#top is never sent to the server?
Related topics
Finished this lesson? Track it to see your progress on the roadmap.