How the Web Works by kirupa (https://www.kirupa.com/me/index.htm) | filed under Build Essentials (https://www.kirupa.com/build/index.htm) TL;DR: When we open a web page, our browser looks up the site's numeric address, opens a secure connection to it, asks for the page, and then asks again for every image, script, and font the page mentions. The lookup is called DNS, the asking is HTTP, and the nearby copies that make it fast live on a CDN. - DNS turns www.kirupa.com into numbers like 172.66.40.239. A resolver asks a root server, then a .com server, then the site's own nameserver, and remembers the answer for as long as it's told to (300 seconds here). - Every HTTP response starts with a status code. 200 means here it is, 301 means it moved, 404 means nothing is at that path, and a 5xx code means the server itself failed. - One page is a lot of requests. At the time of writing, a fresh load of one kirupa.com article made 121 of them, to 16 different host names. - A website doesn't live in one place. There's the origin server, copies on CDN edge servers, and a copy in our browser's cache, which is why a fix that's deployed can still look old. At the time of writing, when I open one of the articles on this site in a fresh copy of Chrome, the page is ready in under a second. In that blink, my browser looks up where the site lives, sets up a private connection, asks for the page, and then makes another 120 requests for the images, scripts, and fonts the page mentions. Most days, none of this matters. Then a page loads slowly, shows a cryptic error, or still shows yesterday's version after our AI agent swears it shipped the fix. In this article, we'll follow that one page load stop by stop, so we know exactly where to look when something goes wrong. Onwards! Taking Apart the Address Our trip starts with an address, and we are going to use a real one, the page for my older article about pizza delivery and fast websites: [Image: The address https://www.kirupa.com/apps/cdn.htm split into three parts. https is the scheme: how we'll talk. www.kirupa.com is the host: which computer. /apps/cdn.htm is the path: which page.] (https://www.kirupa.com/build/images/how_the_web_works_url_parts.png) The https at the front means our conversation will be encrypted, which matters a couple of stops from now. (Links that leave off the first two parts are a story for my relative and absolute URLs (https://www.kirupa.com/html5/relative_urls_and_absolute_urls.htm) article.) There is a catch, though. Computers don't find each other by names. They use numbers, a lot like phone numbers, so our browser has to turn www.kirupa.com into a number before it can ask for anything. That lookup is the first of five stops our trip will make: [Image: Five stops in a row, joined by arrows: 1, look up the number. 2, connect securely. 3, ask for the page. 4, fetch the extras. 5, draw the page.] (https://www.kirupa.com/build/images/how_the_web_works_trip_overview.png) We'll visit each stop in order, using the real answers my browser got. How Does a Browser Find the Server? Our browser doesn't go looking for the number on its own. It asks a helper called a resolver, which usually belongs to our internet provider: [Image: My browser asks a resolver: Where is www.kirupa.com? The resolver hasn't asked anyone yet. On the right sit the three servers it can ask, all grayed out: the root server (a.root-servers.net), a .com server (a.gtld-servers.net), and kirupa.com's nameserver (hugh.ns.cloudflare.com).] (https://www.kirupa.com/build/images/how_the_web_works_dns_ask.png) Our resolver doesn't know the answer yet either, so it starts at the very top and asks one of the root servers: [Image: The same scene. The resolver asks the root server, a.root-servers.net, which is now highlighted. The root server replies: Ask the .com servers.] (https://www.kirupa.com/build/images/how_the_web_works_dns_root.png) Does the root server know our number? Nope. The root servers only know who is in charge of each ending, like .com or .org. There are 13 named root servers, and according to IANA (https://www.iana.org/domains/root/servers), hundreds of machines answer for those 13 names. Next, our resolver will ask one of the .com servers: [Image: The same scene. The resolver asks a .com server, a.gtld-servers.net, which is now highlighted. It replies: Ask hugh.ns.cloudflare.com.] (https://www.kirupa.com/build/images/how_the_web_works_dns_com.png) The .com server doesn't know our number either. What it does know is that kirupa.com's nameservers are run by Cloudflare. Our resolver asks one of those nameservers next: [Image: The same scene. The resolver asks kirupa.com's nameserver, hugh.ns.cloudflare.com, which replies: 172.66.40.239 and 172.66.43.17, good for 300 seconds. A blue arrow carries the answer back to my browser, which now knows 172.66.40.239.] (https://www.kirupa.com/build/images/how_the_web_works_dns_answer.png) (https://www.kirupa.com/build/images/how_the_web_works_dns_answer.png) We finally have our numbers! If one of them doesn't answer, our browser will try the other one. The 300 seconds is how long our resolver and our browser are allowed to remember this answer, which is why most lookups take a few milliseconds instead of four separate trips. This phone book for the internet is called the Domain Name System, or DNS, and Cloudflare's What is DNS? (https://www.cloudflare.com/learning/dns/what-is-dns/) page goes deeper. We can query it ourselves from a terminal: $ dig +short www.kirupa.com 172.66.40.239 172.66.43.17 That 300-second timer is called the TTL (time to live). It's why a site that moves to a new server can keep sending some visitors to the old number for a while. The Trip Itself With a number in hand, our browser can finally send something. What is it sending things across, though? In 2006, US Senator Ted Stevens gave the most famous answer, in a speech about internet regulation (https://en.wikipedia.org/wiki/Series_of_tubes): "It's not a big truck. It's a series of tubes." The picture this put in a lot of heads looked a bit like this: [Image: A gag. My laptop and a server are joined by a loopy pneumatic tube with an envelope shooting through it. Below the tube, a delivery truck is crossed out with a big red X, and a handwritten note points at it: Not a big truck!] (https://www.kirupa.com/build/images/how_the_web_works_tubes.png) As it turns out, he wasn't all that wrong. Our request really does travel through cables, some of them on the ocean floor. What the tubes miss is that our request gets chopped into small pieces called packets, and each packet carries the address it's going to and the address it came from, like a letter in an envelope: [Image: My laptop, at 203.0.113.24, on the left. Cloudflare's server in Seattle, at 172.66.40.239, on the right. Between them is a web of roads joining five post offices. Three numbered envelopes, the pieces of our request, travel on different roads. Below, one envelope up close: To 172.66.40.239, From 203.0.113.24, piece 1 of 3. A handwritten note points at a post office: Each hop gets closer!] (https://www.kirupa.com/build/images/how_the_web_works_packets.png) (https://www.kirupa.com/build/images/how_the_web_works_packets.png) Each post office here is a router. A router doesn't know the whole route. It passes our envelope to a neighbor that's a little closer, and the computer at the far end puts the pieces back in order. Before the real letters go out, our browser and the server agree to talk, and because our address started with https, they also agree on a way to scramble everything: [Image: My browser on the left and Cloudflare's server in Seattle on the right, with three messages between them. 1, my browser: Hello! Let's talk securely. 2, the server: Here's my certificate. The certificate says kirupa.com, *.kirupa.com, issued by Google Trust Services, valid until Jan 16, 2027. 3, my browser checks it and replies: Looks good. Encrypted from here on, shown with a padlock.] (https://www.kirupa.com/build/images/how_the_web_works_secure_connection.png) The certificate is how our browser knows it's really talking to kirupa.com. Someone our browser already trusts vouched for it (Google Trust Services, in this case), and it has an expiration date. This encrypted setup is called TLS, and it's where the padlock in our address bar comes from. Asking for the Page Now our browser will ask for the page, and the request is surprisingly short. Here is the one I sent with curl, a command-line tool that makes browser-style requests, along with the most useful lines of our reply: GET /apps/cdn.htm HTTP/1.1 Host: www.kirupa.com User-Agent: curl/8.14.1 Accept: */* Accept-Encoding: deflate, gzip HTTP/1.1 200 OK Content-Type: text/html Content-Length: 29307 Content-Encoding: gzip Server: cloudflare Cache-Control: public, max-age=0, must-revalidate Age: 686 cf-cache-status: HIT CF-RAY: a635e09f78e72672-SEA Our first line asks to GET the page at /apps/cdn.htm. The Host line names the site, because one server often answers for many sites at the same number. The reply always comes back in three parts: [Image: The response for our page drawn as a sheet with three bands. The top band, the status line, says HTTP/1.1 200 OK: how it went. The middle band, the headers, says Content-Type: text/html, Content-Encoding: gzip, Cache-Control: public, max-age=0, must-revalidate, and cf-cache-status: HIT: notes about the reply. The bottom band, the body, starts with the page's HTML and is 29,307 bytes zipped, 100,502 unzipped: the page itself.] (https://www.kirupa.com/build/images/how_the_web_works_response_parts.png) (https://www.kirupa.com/build/images/how_the_web_works_response_parts.png) We'll lean on the status code and the headers a lot, because they tell us how things went and how long anyone may keep a copy of the page. This ask-and-answer format is the Hypertext Transfer Protocol, or HTTP, and HTTPS is the same conversation inside our encrypted connection. MDN's overview of HTTP (https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Overview) is a friendly guide to the official rules in RFC 9110 (https://www.rfc-editor.org/rfc/rfc9110), and these same requests are what APIs and webhooks (https://www.kirupa.com/build/apis_and_webhooks.htm) are built on. These six status codes are the ones we'll run into the most: [Image: Six cards. 200 OK: here it is. 301 Moved Permanently: it lives over there now. 304 Not Modified: your copy is still good. 404 Not Found: nothing at this path. 500 Internal Server Error: the server broke. 503 Service Unavailable: too busy, or down for now. The 2xx card is green, the 3xx cards are blue, the 4xx card is yellow, and the 5xx cards are red.] (https://www.kirupa.com/build/images/how_the_web_works_status_codes.png) A 2xx code means it worked, and a 3xx code sends us elsewhere, so asking for http://www.kirupa.com/apps/cdn.htm gets a 301 to the https version. A 4xx code means our request was the problem, and a 5xx means the server was. MDN's full list (https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status) even includes 418 I'm a teapot (https://www.rfc-editor.org/rfc/rfc2324), from an April Fools' RFC in 1998. One Page, Many Requests Our 29,307 bytes of HTML are just the beginning. As our browser reads it, it finds the stylesheets, scripts, images, and fonts our page needs, and each one is its own request. This is what one load of our page added up to: [Image: A bar chart of the 121 requests one load of the page made at the time of writing: images 57, scripts 29, fonts 13, stylesheets 12, other 7, and pages 3 (the article plus two embeds). Below, one bar splits the 121 requests by where they went: 57 to www.kirupa.com and 64 to 15 other host names.] (https://www.kirupa.com/build/images/how_the_web_works_many_requests.png) That's 121 requests for one article! Only 57 of them went to www.kirupa.com. The other 64 went to 15 other host names, mostly for a few embedded GIFs and a YouTube video. So, when an agent adds just one more embed or tracking script, it's never just one request. As our files arrive, our browser turns them into something we can see in four stages: [Image: Four stages in a row. Parse: the HTML becomes a tree of elements (html, head, body, h1, p, img). Style: the same tree, with CSS rules matched to each element. Layout: boxes with sizes and positions. Paint: the finished page in color.] (https://www.kirupa.com/build/images/how_the_web_works_bytes_to_pixels.png) MDN's How browsers work (https://developer.mozilla.org/en-US/docs/Web/Performance/Guides/How_browsers_work) goes deeper into each of these stages. For our page, Chrome said everything had finished loading at 815 milliseconds. Where Does a Website Actually Live? I have been a little sneaky up to now. Our browser never talked to kirupa.com's own server! Not even once. The CF-RAY header in our reply ends in a code (https://developers.cloudflare.com/fundamentals/reference/http-headers/#cf-ray) for the data center that answered, and ours ends in SEA, for a Cloudflare server in Seattle. Here is what is going on: [Image: kirupa.com's own server, the origin, sits in the middle. Around it are six smaller servers in Seattle, London, Frankfurt, Tokyo, Sydney, and São Paulo, each next to a visitor, and each joined to the origin by a thin dashed line. My laptop sits next to the Seattle server, and an arrow shows my request going there instead of to the origin.] (https://www.kirupa.com/build/images/how_the_web_works_edges_map.png) Our page starts out on kirupa.com's own server, which is called the origin, while Cloudflare runs servers all over the world that sit close to visitors and keep copies of what they've fetched. When a nearby server has a copy, it answers right away, and when it doesn't, it fetches the file from our origin and keeps a copy for next time. A network like this is called a content delivery network, or CDN, and my What is a CDN? (https://www.kirupa.com/apps/cdn.htm) article explains it with pizza delivery. Its nearby servers are called edge servers, and a header called cf-cache-status tells us whether an edge had a copy. I asked for one of our page's images twice in a row: [Image: Two requests for the same image, where_you_live_200.png. The first, a MISS: my request goes to the Seattle edge, which has to fetch the image from the origin; first byte in 125 ms, done in 187 ms. The second, a HIT: the Seattle edge answers by itself and the origin isn't needed; first byte in 39 ms, done in 67 ms.] (https://www.kirupa.com/build/images/how_the_web_works_miss_hit.png) The first request was a MISS, so the edge fetched the image from our origin and the first byte took 125 milliseconds, while the second was a HIT and took just 39. This brings us to the misconception I want to squash. A website doesn't live in one place, and our page lives in at least three: [Image: The same page in three places, joined by arrows. kirupa.com's own server: the original, updated when we deploy. Cloudflare's edge in Seattle: a copy nearby, Age: 686 seconds. Chrome on my laptop: a copy on my device, where 56 of 57 requests came from cache on my second visit. A handwritten note points at the laptop's copy: Which one am I seeing?] (https://www.kirupa.com/build/images/how_the_web_works_three_copies.png) Each copy has its own clock. The HTML at the Seattle edge was 686 seconds old when I got it (that's the Age header). When I loaded the page a second time, Chrome answered 56 of the 57 requests to www.kirupa.com from its own cache. Wren, the web developer at Crumb & Co., ran into exactly this, because our bakery's site sits behind a CDN too. Rosa asked for our holiday pie pre-order page to say that pickup is November 24 or 25, and Wren's coding agent made the change, deployed it, and reported success. Rosa's phone still showed the old text. Nothing was broken, because the agent had updated the origin and Rosa was looking at an older copy. An agent's "deployed" only tells us about the origin. To see what a customer sees, we read three headers on their response: - cf-cache-status says whether the edge had a copy (HIT) or had to fetch one (MISS). - Age says how many seconds old that copy is. - Cache-Control (https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control) says how long anyone is allowed to keep it. Then we wait for that copy to expire or clear it out, which CDNs call purging. Every cache comes with this tradeoff. The longer copies live, the faster our pages are, and the longer an old price sticks around. When It Breaks Every stop on our trip can fail, and each failure looks different. Here are our five stops again, next to what we'll see when each one goes wrong: [Image: The five stops from earlier, each next to what we see when it fails. 1, look up the number: DNS_PROBE_FINISHED_NXDOMAIN, because the name doesn't exist or its DNS records are missing. 2, connect securely: ERR_CONNECTION_TIMED_OUT or NET::ERR_CERT_DATE_INVALID, because the server is unreachable or its certificate expired. 3, ask for the page: 404 Not Found, 500, or 503, because of a wrong path or a failed server, and a slow page means a long wait for the first byte. 4, fetch the extras: the page loads unstyled or with broken images because an extra request failed. 5, draw the page: a blank page or buttons that do nothing, because of a JavaScript error.] (https://www.kirupa.com/build/images/how_the_web_works_failure_map.png) (https://www.kirupa.com/build/images/how_the_web_works_failure_map.png) Before we touch any code, we want to know which stop failed. A 404 means the trip worked and the path is wrong, while a certificate warning means we never got to ask at all. Naming the stop helps an AI agent too, since "the stylesheet at /css/main.css returns a 404" tells it exactly where to look. Try It This Week Chrome's developer tools (DevTools) show every stop we just walked through. Here is what the Timing tab showed for our page itself: [Image: The Timing breakdown for the page's own request, drawn like Chrome DevTools: DNS Lookup 49.8 ms, Initial connection 32.8 ms, SSL 32.5 ms (inside the connection), Request sent 0.1 ms, Waiting for server response 37.4 ms, and Content Download 13.3 ms, for 134.3 ms from start to the page's last byte. A handwritten note points at the DNS bar: Biggest slice!] (https://www.kirupa.com/build/images/how_the_web_works_devtools_timing.png) The DNS lookup was the biggest slice at 49.8 milliseconds, because my browser had never looked up www.kirupa.com before. Now let's trace a page load on your own product: 1. Open your product's home page, open DevTools (Cmd+Option+I on a Mac, Ctrl+Shift+I on Windows), and click the Network tab. 2. Check Disable cache, reload, and note the request count at the bottom. 3. Click the first row, which is the page itself, and find its status code, Cache-Control, and any CDN header like cf-cache-status or Age. 4. In its Timing tab, write down how long each stop took. 5. Uncheck Disable cache, reload, and count the rows marked disk cache or memory cache in the Size column. 6. Repeat this on your slowest page and write one sentence naming its slowest stop, which is the best bug report you can hand an engineer or an agent. Frequently Asked Questions What Happens When You Type a URL and Press Enter? The browser looks up the site’s numeric address using DNS, opens a connection, and sets up encryption if the address starts with https. It then sends an HTTP request for the page and gets back a status code, headers, and the HTML. Finally, it requests every stylesheet, script, image, and font that HTML mentions, and draws the page. What Is DNS and How Does It Work? DNS is the system that turns names like www.kirupa.com into the numeric addresses computers use. A resolver asks a root server which servers handle .com, asks one of those which nameservers the domain uses, and then asks those nameservers for the address. The answer is cached for a set time called the TTL, which is why most lookups are fast. What Is the Difference Between HTTP and HTTPS? HTTP is the request-and-response conversation a browser has with a server. HTTPS is the same conversation inside an encrypted connection (TLS), so nobody along the way can read or change it. The server proves who it is with a certificate, and that’s where the browser’s padlock comes from. Why Does My Website Still Show the Old Version After I Deploy? A deploy updates the origin server, but copies of the old page can still live in CDN edge caches and in visitors’ browser caches. The response headers tell you which copy you got: look at cf-cache-status or X-Cache, Age, and Cache-Control. Wait for those copies to expire, or purge the CDN cache and reload without the browser cache. Conclusion Phew! We just followed one click from an address all the way to pixels. Our resolver walked from a root server to a .com server to kirupa.com's nameserver to get a number. Our browser opened an encrypted connection, sent an HTTP request, and went back for 120 more files, and copies answered almost all of the requests that went to www.kirupa.com. One honest limitation is that this was one page load, from one desk, on one afternoon, so your numbers will be different. The habit still works anywhere, though. When something looks wrong, we figure out which stop failed and which copy we're looking at before we change any code. And as for Senator Stevens...the internet really is a series of tubes. The tubes are just full of tiny envelopes, and most of them never have to go all the way back to the origin. Before you go, here are a few things you can do: 1. Test your knowledge by taking the quiz below. 2. Read the next article in the series, Data Modeling and Databases (https://www.kirupa.com/build/data_modeling.htm), or check out some related content: - What is a CDN? (https://www.kirupa.com/apps/cdn.htm). The pizza delivery explanation of edge servers and caching. - Relative URLs and Absolute URLs (https://www.kirupa.com/html5/relative_urls_and_absolute_urls.htm). When a link should spell out the whole address, and when it shouldn’t. - The Anatomy of a Chat Request (https://www.kirupa.com/ai_fundamentals/anatomy_of_a_chat_request.htm). The same request-and-response idea, aimed at an AI model. 3. Stuck on something, or curious about a detail we skipped? Ask on the forums (https://forum.kirupa.com/), and we’ll help you out. [Mastery Quiz] Test your knowledge! Take the How the Web Works Quiz: https://www.kirupa.com/quizzes/how_the_web_works.htm Practice with 6 multiple-choice questions and an explanation after every answer. [/Mastery Quiz]