Chapter 48 of 48

What Happens When You Type a URL in the Browser

"What happens when you type a URL like https://www.example.com into the browser and press Enter?" is probably the most famous computer networks interview question. Interviewers love it because there is no single right length for the answer. You can answer in one minute or in one hour, and the way you answer shows how well you understand how the pieces fit together.

This chapter is a summary of the whole tutorial. Almost every topic you have studied appears somewhere in this journey: URLs, DNS, IP addressing, subnetting, ARP, Ethernet, NAT, routing, TCP, congestion control, TLS, HTTP, CDNs and more.

In one line: the browser finds the server's IP address (DNS), opens a connection to it (TCP + TLS, or QUIC), sends an HTTP request, receives the response, and turns the HTML into a page on your screen, fetching more files as needed.

Let's walk through it step by step.


The Big Picture

You type https://www.example.com and press Enter
        |
 1. Browser parses the URL (and checks HSTS)
        |
 2. Browser checks its caches (HTTP cache, service worker)
        |
 3. DNS lookup: www.example.com -> IP address
        |
 4. Packets leave your machine: subnet check, ARP, Wi-Fi/Ethernet, NAT, routing
        |
 5. TCP three-way handshake with the server (port 443)
        |
 6. TLS handshake: certificate check + session keys
        |
 7. Browser sends the HTTP request (GET /)
        |
 8. Server side: CDN / load balancer -> web server -> app -> database
        |
 9. HTTP response comes back (status, headers, HTML)
        |
10. Browser renders: DOM, CSSOM, JavaScript, layout, paint
        |
11. More requests for CSS, JS, images (same connection); connection later closed

Step 1: The Browser Parses What You Typed

First, the browser decides whether you typed a URL or a search term. "cs learning portal" has spaces and no domain, so it goes to the default search engine. "www.example.com" looks like a domain, so the browser treats it as a URL (and assumes https:// in modern browsers if you did not type a scheme).

Then it splits the URL into parts:

https://www.example.com:443/search?q=tcp&page=2#results
\___/   \_____________/ \_/\_____/ \__________/ \_____/
scheme       host      port  path     query     fragment

Part

Meaning

Scheme

Which protocol to use: https or http

Host

The domain name that DNS will turn into an IP address

Port

Usually omitted: 443 for HTTPS, 80 for HTTP

Path

Which resource on the server

Query

Extra parameters, key=value pairs separated by &

Fragment

A position inside the page. It stays in the browser and is never sent to the server

HSTS check: if the site has previously told the browser "always use HTTPS" (the Strict-Transport-Security header), or is on the browser's built-in HSTS preload list, the browser changes http:// to https:// by itself, before sending anything. This prevents an attacker from intercepting that first plain-HTTP request.


Step 2: Check the Browser's Caches

The fastest network request is the one you never make. Before going to the network, the browser checks:

  • HTTP cache: if the page was stored earlier and is still fresh according to its Cache-Control header, the browser can use it directly. If it is stale, the browser may ask the server "has it changed?" (a conditional request), and get a small 304 Not Modified reply instead of the whole file.

  • Service worker: some sites install a script that can answer requests from its own storage, even when offline.

The main HTML page is often not cached for long, so in most cases the browser continues to the network.


Step 3: DNS Lookup

Computers route packets using IP addresses, not names, so the browser needs the IP address of www.example.com. As you learned in the DNS chapter, it checks several places, from nearest to farthest:

Browser DNS cache
   | miss
Operating system cache + hosts file
   | miss
Recursive resolver (ISP, or public ones like 8.8.8.8 / 1.1.1.1) - checks its cache
   | miss
Root server      -> "ask the .com TLD servers"
TLD server (.com)-> "ask example.com's authoritative servers"
Authoritative    -> "www.example.com is 203.0.113.10" (A record), TTL 300 s

(203.0.113.10 is an address reserved for documentation examples. Real answers differ and can change over time.)

  • The browser usually asks for both an A record (IPv4) and an AAAA record (IPv6), and prefers IPv6 when it works.

  • Large sites often answer with a CNAME pointing to a CDN, so you are sent to a nearby edge server rather than the origin.

  • Every answer has a TTL, so it is cached and the next visit skips most of this.

  • DNS queries normally use UDP port 53, or are encrypted with DNS over HTTPS / DNS over TLS.


Step 4: How Packets Leave Your Computer

Now the browser knows the server's IP address. Before any TCP packet can go out, lower layers must work out where to physically send it.

Is the server on my local network?

Your computer compares the destination IP with its own network using the subnet mask (Subnetting & CIDR chapter). Suppose your laptop is 192.168.1.5/24. The server 203.0.113.10 is not in 192.168.1.0/24, so the packet must go to the default gateway (your home router), for example 192.168.1.1.

ARP for the gateway's MAC address

To build a Wi-Fi or Ethernet frame, the laptop needs a MAC address. It uses ARP to ask "who has 192.168.1.1?" (unless the answer is already in the ARP cache).

Important: the laptop never learns the server's MAC address. The frame is addressed to the router's MAC, while the IP packet inside is addressed to the server's IP. MAC addresses change at every hop; IP addresses stay the same end to end (except where NAT changes them).

Encapsulation

+-----------------+---------------------+------------------+---------------------+
| Ethernet/Wi-Fi  | IP header           | TCP header       | Data (TLS record    |
| dst MAC: router | src 192.168.1.5     | src port 50000   |  carrying HTTP)     |
| src MAC: laptop | dst 203.0.113.10    | dst port 443     |                     |
+-----------------+---------------------+------------------+---------------------+
   Layer 2              Layer 3              Layer 4               Layer 7

NAT at the home router

Your laptop's address 192.168.1.5 is private and cannot be used on the Internet. The home router performs NAT: it replaces the source address and port with its own public IP and a port of its choice, and remembers the mapping so replies can be sent back to your laptop.

Routing across the Internet

The packet now travels through your ISP and possibly several other networks. Each router looks up the destination IP in its routing table (longest prefix match), decrements the TTL, and forwards the packet to the next hop. Between large networks, routes are exchanged using BGP. A typical path has 10 to 20 hops, which you can see with traceroute / tracert.


Step 5: TCP Three-Way Handshake

For HTTPS over TCP, the browser opens a connection to port 443 from a random ephemeral port on your machine (for example 50000):

Browser (SYN-SENT)  ---- SYN ---------->  Server (LISTEN)
                    <--- SYN+ACK -------  (SYN-RECEIVED)
(ESTABLISHED)       ---- ACK ---------->  (ESTABLISHED)

This costs 1 RTT. During the handshake, both sides exchange options such as MSS and window scaling (TCP Header chapter). If nothing listens on port 443, the server answers with RST and you see "connection refused"; if a firewall silently drops the SYN, the browser waits and eventually shows a timeout.


Step 6: TLS Handshake

Because the scheme is https, the browser now runs a TLS handshake (SSL/TLS chapter) on top of the TCP connection:

  1. ClientHello: supported TLS versions and cipher suites, a key share, the SNI (Server Name Indication: "I want www.example.com", so one IP can host many HTTPS sites) and ALPN ("I can speak h2 or http/1.1").

  2. ServerHello + certificate: the server picks the parameters, sends its certificate chain, and proves it owns the private key.

  3. Certificate verification: the browser checks that the certificate is for www.example.com, is not expired, and chains up to a trusted Certificate Authority. If not, you see a big security warning.

  4. Session keys: both sides derive the same symmetric keys. From now on, everything is encrypted.

With TLS 1.3 this costs 1 RTT (TLS 1.2 needed 2).


Step 7: The HTTP Request

Now the browser sends the actual request, encrypted inside TLS. Shown in HTTP/1.1 text form (over HTTP/2 the same fields are sent as compressed binary frames):

GET / HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (...)
Accept: text/html,application/xhtml+xml
Accept-Encoding: gzip, br
Accept-Language: en-IN,en;q=0.9
Cookie: session_id=abc123

The Host header tells the server which website is wanted, because one server can host many domains. Cookies saved for this site are attached automatically.


Step 8: What Happens on the Server Side

A big website is rarely one machine. The request may pass through several layers:

Internet
   |
CDN edge server        <- may answer directly from its cache (static files)
   |
Load balancer          <- picks one healthy server out of many
   |
Web server / reverse proxy (e.g. Nginx)
   |
Application server     <- runs the site's code (Java, Python, Node.js ...)
   |
Database / cache       <- fetches the data the page needs

The application builds the HTML (or the CDN returns a cached copy) and the response travels back along the same chain.

The answer is not always the page itself. The server may send a redirect, for example 301 Moved Permanently with Location: https://www.example.com/ if you typed http://example.com. The browser then starts again from Step 1 with the new URL, which costs extra round trips. This is one reason HSTS and correct links matter.


Step 9: The HTTP Response

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Encoding: br
Cache-Control: max-age=0, must-revalidate
Set-Cookie: theme=dark; Secure; HttpOnly
Alt-Svc: h3=":443"; ma=86400

<!doctype html><html>...</html>
  • The status code says what happened (200 OK, 301, 404, 500, ...).

  • Content-Encoding: br means the body is compressed with Brotli; the browser decompresses it.

  • Cache-Control decides what the browser may cache for next time (Step 2).

  • Alt-Svc tells the browser the site also supports HTTP/3, so the next connection may use QUIC.

While the body downloads, TCP flow control (receive window) and congestion control (slow start, cwnd) decide how fast the data can be sent, and lost segments are retransmitted.


Step 10: The Browser Renders the Page

The bytes are now HTML. The browser's rendering engine turns them into pixels:

HTML bytes --parse--> DOM tree  -------+
                                       +--> Render tree --> Layout --> Paint --> Composite
CSS bytes  --parse--> CSSOM tree ------+     (what to show)  (where)    (pixels)  (layers on screen)
                ^
JavaScript can read and change both the DOM and the CSSOM
  1. Parse HTML into the DOM (Document Object Model), a tree of elements.

  2. While parsing, the browser finds more resources: <link rel="stylesheet">, <script>, <img>. It requests them, reusing the same connection (multiplexed over HTTP/2 or HTTP/3).

  3. Parse CSS into the CSSOM. CSS is render-blocking: the page is not painted until the CSS needed for it is ready.

  4. JavaScript: a normal <script> pauses HTML parsing while it downloads and runs. Scripts marked defer or async avoid this.

  5. Render tree, layout, paint, composite: the browser combines DOM and CSSOM, calculates the size and position of each box, draws them, and puts the layers on the screen.


Step 11: More Requests, and Closing the Connection

A typical page triggers dozens of further requests (CSS, JavaScript, fonts, images, API calls). Many go to the same host and reuse the existing connection; others go to different hosts (CDNs, analytics) and repeat Steps 3 to 6 for those hosts.

The connection is kept open for a while (keep-alive) in case you click another link. Eventually it is closed with the FIN/ACK exchange, and the side that closes first spends some time in TIME-WAIT.


Every Layer in One Table

Step

Protocol / Mechanism

OSI Layer

URL parsing, HTTP request/response, rendering

HTTP, HTML, CSS, JS

Application (7)

Name to IP address

DNS (UDP 53, or DoH/DoT)

Application (7)

Encryption and server authentication

TLS

Between transport and application (often placed at Presentation/Session)

Reliable connection to port 443

TCP (or QUIC over UDP)

Transport (4)

Addressing, NAT, routing hop by hop

IP, NAT, routing protocols, BGP

Network (3)

Finding the gateway's MAC, sending frames

ARP, Ethernet / Wi-Fi

Data Link (2)

Bits on cable, fibre or radio

Copper, fibre, radio signals

Physical (1)


Worked Example: How Long Until the Page Arrives?

Assume a first visit with RTT = 40 ms between you and the server, a DNS lookup of 30 ms (the resolver had it cached, so only one trip to the resolver), server processing time of 25 ms, TLS 1.3, and an HTML page of 60,000 bytes. Ignore transmission time (fast link).

DNS lookup                       30 ms
TCP handshake          1 RTT  =  40 ms
TLS 1.3 handshake      1 RTT  =  40 ms
HTTP request/response  1 RTT  =  40 ms
Server processing                25 ms
--------------------------------------
Time to first byte (TTFB)       175 ms

The first byte is not the whole page. With TCP slow start and an initial congestion window of 10 segments of 1460 bytes each:

Flight 1: 10 x 1460 = 14,600 bytes   (total 14,600)  arrives with the first byte
Flight 2: 20 x 1460 = 29,200 bytes   (total 43,800)  +1 RTT
Flight 3: 40 x 1460 = 58,400 bytes   (total 102,200 >= 60,000, done)  +1 RTT

HTML fully received = 175 + 2 x 40 = 255 ms

With HTTP/3 over QUIC, the TCP and TLS handshakes merge into one round trip: TTFB = 30 + 40 + 40 + 25 = 135 ms. On a repeat visit with a cached DNS answer and QUIC 0-RTT, it could be just 40 + 25 = 65 ms.

This kind of calculation explains why websites use CDNs (smaller RTT), connection reuse, DNS caching, TLS 1.3 / QUIC, and why a redirect (an extra full round of requests) hurts.


The Journey in Python

This program follows the same steps on your own machine, without needing the Internet. It parses a URL, starts a tiny web server on 127.0.0.1, resolves localhost using the operating system's resolver (the same OS step a browser goes through before any DNS server is asked), connects with TCP, sends a raw HTTP request, reads the response, and finally parses the HTML to find the extra files a browser would fetch next. It uses plain HTTP to keep the example small; a real browser would add the TLS handshake after connecting.

import socket
import threading
from html.parser import HTMLParser
from urllib.parse import urlsplit

# ---------- Step 1: parse the URL the user typed ----------
url = urlsplit("https://www.example.com/search?q=tcp&page=2#results")
print("Scheme  :", url.scheme)
print("Host    :", url.hostname)
print("Port    :", url.port or {"http": 80, "https": 443}[url.scheme], "(default)")
print("Path    :", url.path)
print("Query   :", url.query)
print("Fragment:", url.fragment, "(never sent to the server)")
print()

# ---------- A tiny local web server so the demo works offline ----------
PAGE = (b'<html><head><link rel="stylesheet" href="/style.css">'
        b'<script src="/app.js"></script></head>'
        b'<body><h1>Hello</h1><img src="/logo.png"></body></html>')

def serve_once(listener):
    conn, _ = listener.accept()
    with conn:
        request = b""
        while b"\r\n\r\n" not in request:          # read until end of headers
            request += conn.recv(1024)
        conn.sendall(b"HTTP/1.1 200 OK\r\n"
                     b"Content-Type: text/html\r\n"
                     b"Content-Length: " + str(len(PAGE)).encode() + b"\r\n"
                     b"Connection: close\r\n\r\n" + PAGE)

listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.bind(("127.0.0.1", 0))                     # OS picks a free port
listener.listen()
port = listener.getsockname()[1]
threading.Thread(target=serve_once, args=(listener,), daemon=True).start()

# ---------- Step 2: DNS - turn the name into an IP address ----------
info = socket.getaddrinfo("localhost", port, socket.AF_INET, socket.SOCK_STREAM)
ip = info[0][4][0]
print("DNS     : localhost ->", ip)

# ---------- Step 3: TCP three-way handshake (done by the OS in connect) ----------
client = socket.create_connection((ip, port))
print("TCP     : connected")

# ---------- Step 4: send the HTTP request ----------
client.sendall(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")

# ---------- Step 5: read the HTTP response ----------
response = b""
while chunk := client.recv(4096):
    response += chunk
client.close()
head, body = response.split(b"\r\n\r\n", 1)
print("Status  :", head.split(b"\r\n")[0].decode())
print("Body    :", len(body), "bytes of HTML")

# ---------- Step 6: the browser parses HTML and finds more files to fetch ----------
class ResourceFinder(HTMLParser):
    def __init__(self):
        super().__init__()
        self.found = []
    def handle_starttag(self, tag, attrs):
        a = dict(attrs)
        if tag == "link" and a.get("rel") == "stylesheet":
            self.found.append(("CSS", a["href"]))
        elif tag in ("script", "img") and "src" in a:
            self.found.append(("JS" if tag == "script" else "Image", a["src"]))

finder = ResourceFinder()
finder.feed(body.decode())
print("Next    :", ", ".join(f"{kind} {path}" for kind, path in finder.found))

Output:

Scheme  : https
Host    : www.example.com
Port    : 443 (default)
Path    : /search
Query   : q=tcp&page=2
Fragment: results (never sent to the server)

DNS     : localhost -> 127.0.0.1
TCP     : connected
Status  : HTTP/1.1 200 OK
Body    : 146 bytes of HTML
Next    : CSS /style.css, JS /app.js, Image /logo.png

Notice how each line maps to a step: the fragment results is never sent, the OS resolver turned a name into an IP, create_connection() did the TCP three-way handshake for us (Socket Programming chapter), and the HTML parser discovered three more resources, just like a browser does before it sends requests for CSS, JS and images.


What If It Fails? Common Follow-Up Questions

Symptom

Where It Failed

"Server not found" / DNS_PROBE_FINISHED_NXDOMAIN

Step 3: DNS could not resolve the name (typo, domain expired, DNS server problem)

"Connection refused"

Step 5: the host replied with TCP RST because nothing listens on that port

"Connection timed out"

Steps 4 to 5: packets dropped by a firewall, or the server/route is down

"Your connection is not private"

Step 6: certificate expired, wrong domain, or untrusted CA

404 Not Found

Step 8/9: the server worked, but the path does not exist

500 / 502 / 503

Step 8: application error, bad gateway (proxy could not reach the backend), or overloaded service

Page loads but looks broken

Step 10/11: CSS or JavaScript files failed to load

This table is also a practical troubleshooting order, matching the Network Troubleshooting chapter: check DNS, then connectivity, then TLS, then the HTTP status.


Common Mistakes

  • Saying the browser finds the server's MAC address. ARP is used only on the local network, for the default gateway's MAC.

  • Putting the TLS handshake before the TCP handshake. With TCP, the order is DNS → TCP → TLS → HTTP. (Only QUIC combines the transport and TLS handshakes.)

  • Forgetting DNS caching. Most lookups never reach the root servers; they are answered by a cache.

  • Saying the fragment (#...) is sent to the server. It is not; it is used only by the browser.

  • Stopping at "the server sends the HTML". Rendering and the dozens of follow-up requests are a big part of what the user experiences as page load time.

  • Saying DNS always uses TCP or HTTP always uses TCP. DNS usually uses UDP, and HTTP/3 uses QUIC over UDP.


Interview Tips

  • Start with the outline, then go deeper. Say the steps in one breath first (URL → cache → DNS → TCP → TLS → HTTP request → server → response → render), then ask "which part would you like me to go deeper into?"

  • Name the layers as you go. It shows you can connect the OSI/TCP-IP model to real behaviour.

  • Mention caching at each level: browser cache, DNS caches, CDN cache, server-side cache.

  • Mention modern details for extra credit: HSTS, SNI, ALPN, HTTP/2 multiplexing, HTTP/3 over QUIC, CDNs and load balancers.

  • Be ready for "what if" questions: what if DNS fails, what if the certificate is invalid, what if you type an IP address directly (DNS is skipped, but the certificate usually will not match), what if the site redirects.

Conclusion

Typing a URL and pressing Enter starts a chain of steps that uses almost every layer of the network stack.

The browser parses the URL and checks its caches, uses DNS to find the server's IP address, sends packets through ARP, Ethernet/Wi-Fi, NAT and routers, opens a TCP connection and a TLS session (or a single QUIC handshake for HTTP/3), sends an HTTP request, gets a response from servers that may sit behind CDNs and load balancers, and finally renders the page while fetching the remaining resources.

The easiest way to remember the order is:

URL → Cache → DNS → TCP → TLS → HTTP Request → Server → HTTP Response → Render

And remember the key idea behind most performance tricks:

Every round trip costs time, so caching, connection reuse, CDNs, TLS 1.3 and QUIC all work by removing round trips or making them shorter.