20 min read

How the internet works

This page reached you as pulses on a wire. Below, we rebuild the internet from that wire up, one problem at a time. Every figure is a short animation. Press Next to step through it.

The first characters of this page's HTML, as bits. High is 1, low is 0.

Layer 1 · Physical

Bits on a wire

Start with two computers and a copper wire between them. The sender can do one thing: raise or lower the voltage on the wire. The receiver can do one thing: measure it.

That is enough to send anything. Both sides agree on two rules. High voltage means 1 and low voltage means 0. And the sender changes the level only on the beat of a clock, so the receiver knows when to look.

  1. Computers A and B, joined by one wire. Nothing is being sent yet.
  2. A switches the wire between high and low voltage. The trace shows the level over time.
  3. On every clock tick, B reads the wire. High is 1, low is 0. Eight ticks give eight bits.
  4. Eight bits make a byte. 01001000 is the number 72, and the ASCII table says 72 is the letter H.
  5. Eight more ticks: 01101001 is 105, the letter i. B now has the message “Hi”.
  6. Fiber sends flashes of light. Wi-Fi sends radio waves. The idea is the same: both sides agree on what each signal means.

Everything else on this page is a way of organizing these bits. A gigabit link carries a billion of them every second. Real links use cleverer signals that pack several bits into each tick, but the idea is the same.

Layer 2 · Link

Frames: who is this for?

The wire now carries a stream of bits. Three problems remain:

  • Where does one message end and the next begin?
  • If several computers share the network, who is the message for?
  • If electrical noise flips a bit, how would anyone know?

The fix is to send bits in packages called frames. Ethernet and Wi-Fi both do this, with slightly different layouts. Here is Ethernet's: a start pattern, two addresses, a type, the data, and a checksum.

  1. A raw stream of bits. Where does the message start, and who is it for?
  2. Wrap the data in a frame. Every frame has the same parts in the same order. Short data is padded out to 46 bytes.
  3. The preamble, 10101010…, says “a frame starts here” and lets B sync its clock to A's.
  4. Two MAC addresses: who the frame is for, and who sent it. Every network card has its own 48-bit MAC address. The type says what kind of data is inside.
  5. The checksum. A runs a formula, CRC-32, over the addresses, type and data, and puts the result at the end.
  6. Noise flips one bit on the way, and “Hi” arrives as “Ii”. B runs the same formula, gets a different number, and throws the frame away.

Ethernet doesn't resend damaged frames. It drops them and moves on. Remember that: something higher up will have to notice the gap.

To connect several computers, plug them into a switch. That makes a local network, like the one in your home or office. The switch has one job: deliver each frame to the right computer.

  1. Four computers plugged into a switch. The switch keeps a table of which computer is on which port. It starts empty.
  2. A sends a frame to B. The switch doesn't know where B is yet, so it sends a copy out of all its other ports.
  3. C and D see the frame isn't addressed to them and ignore it. The switch notes that A is on port 1.
  4. B replies. The switch already knows A is on port 1, so only A gets it. It notes that B is on port 2.
  5. From now on, frames between A and B go straight to the right port. Nobody else sees them.

Wi-Fi sends its own kind of frame over radio, with your access point in the role of the switch. One difference: radio reaches every device in range, so anyone nearby can pick up every frame.

Layer 3 · Network

IP: an address for the whole planet

A switch finds computers by flooding frames and watching the replies. That works in a building. It can't work for the planet: no switch could track billions of devices, and flooding would drown everyone.

The deeper problem is that a MAC address says who a device is, not where it is. The internet needs addresses that work like postal addresses, where each part narrows down the location.

That's an IP address. aknight.dev's server is at 104.21.71.112: four numbers from 0 to 255, 32 bits in all. Addresses are handed out in blocks. Cloudflare holds every address that starts with the same first 13 bits as 104.16.0.0. That block is written 104.16.0.0/13, and it covers 524,288 addresses.

Data travels in packets of up to about 1,500 bytes: a short IP header with the source and destination addresses, then the data. Routers pass packets along. Each router has a table that maps blocks of addresses to a next hop. It picks the most specific block that matches.

  1. Your laptop wants to send a packet to aknight.dev's server at 104.21.71.112. Routers run by different companies stand in between.
  2. Your laptop checks its own table first. 104.21.71.112 isn't on the home network, so the packet goes to the home router.
  3. The home router knows one way out: everything goes to your ISP. It also lowers the hop counter, TTL, from 64 to 63.
  4. The ISP's router finds the most specific match. 104.16.0.0/13 is Cloudflare, reached through Transit A.
  5. Transit A passes it to Cloudflare. Each router picks only the next hop. None of them knows the whole route.
  6. Cloudflare's router hands the packet to a server. Five hops, a few milliseconds.
  7. Now send six packets. Each is routed on its own. One takes another path, and one reaches an overloaded router and is dropped. They arrive as 1, 2, 4, 3, 6.

A backbone router's table lists about a million blocks. That sounds like a lot, but it's tiny next to the billions of devices those blocks contain. Routers build these tables by telling each other which blocks they can reach, using a protocol called BGP.

IP makes no promises. Overloaded routers drop packets. Packets can take different routes and arrive out of order. IP doesn't notice and doesn't fix it. That's TCP's job, two sections from now.

Frames carry packets, one hop at a time

A packet doesn't replace the frame from the last section. It rides inside one. But a frame only crosses one link, between two devices that can reach each other directly. So at every router, the packet comes out of its old frame and goes into a new one for the next link.

  1. The packet starts inside a frame. The frame is addressed only to the next device: your home router.
  2. The frame crosses one link, the Wi-Fi, to the home router.
  3. The router throws the frame away and reads the IP header inside. Destination 104.21.71.112: next hop, the ISP. TTL drops to 63.
  4. It puts the packet in a new frame, addressed to the ISP's router, and sends it over the fiber.
  5. The ISP's router does the same: old frame off, new frame on, next link.
  6. Each frame lasted one hop. The packet made the whole trip, and its destination never changed.

How does your laptop learn the home router's MAC address? It asks the whole local network, “Who has 192.168.1.1?”, and the router answers. That exchange is called ARP.

Your laptop's address, 192.168.1.23, is private: millions of homes use the same one, so it means nothing on the open internet. Your home router swaps in its own public address on the way out and swaps yours back in on replies. This is called NAT. It lets a whole household share one public address, which is how IPv4's 4.3 billion addresses stretch to cover far more devices. The newer IPv6 uses 128-bit addresses, enough for every device to have its own.

Layer 4 · Transport

Ports: which program?

IP gets a packet to the right computer. But your laptop runs a browser, a music app and a chat app, all at once. Which one is the packet for?

The answer is a port, a number from 0 to 65,535 that tells the computer which program gets the packet. Servers listen on well-known ports: 53 for DNS, 80 for HTTP, 443 for HTTPS. Your side picks a random high port, like 52814, for each conversation.

  1. Your laptop runs several programs that use the network. Packets for all of them arrive at the same IP address.
  2. Three packets arrive. The IP address got them to this laptop. Which program gets each one?
  3. Each packet also carries a port number. The operating system gives each packet to the program using that port.
  4. Outgoing packets carry ports too. The browser sends to port 443 on aknight.dev's server. Port 443 means HTTPS.

Two addresses and two ports identify one conversation: 192.168.1.23:52814 talking to 104.21.71.112:443. That's how both ends keep thousands of conversations apart.

UDP: ports and nothing else

The simplest way to use ports is UDP. It adds an 8-byte header to the packet: source port, destination port, length and a checksum. No setup and no guarantees. Send and hope.

That suits short questions that are cheap to ask again if the answer gets lost, like the DNS lookups coming up later. It also suits live calls and games, where a late packet is useless anyway. A web page needs more: every byte, in order.

Layer 4 · Transport

TCP: nothing lost, nothing out of order

IP loses some packets and delivers others out of order. TCP turns those unreliable packets into a reliable stream: bytes go in one end and come out the other complete and in order, or the connection reports an error.

The trick is to number every byte. The receiver says which bytes it has, and the sender resends whatever went missing. First, both sides agree where the numbering starts.

  1. Your browser wants to talk to aknight.dev's server on port 443. Before any data moves, TCP sets up a connection.
  2. SYN: “Let's talk. My starting number is 1000.” The browser's first byte will be number 1001.
  3. SYN-ACK: “Got it. Next I expect your byte 1001. My starting number is 5000.”
  4. ACK: “Got it. Next I expect your byte 5001.” The connection is open. Setting it up took one round trip.
  5. Now data flows. The browser sends 400 bytes, numbered 1001 to 1400. The server replies ACK 1401: everything up to byte 1400 arrived.

Real starting numbers are random, like 2,766,301,940. That makes it hard for an attacker to slip fake packets into someone else's connection.

When a packet goes missing

The server now sends the page in segments of 1,000 bytes. Your laptop keeps a buffer, where early arrivals wait until the gap before them is filled.

  1. The server sends 5,000 bytes of the page in five segments. Your laptop passes bytes to the browser only in order.
  2. Segment 1 arrives. Bytes 5001–6000 go straight to the browser, and your laptop replies ACK 6001: “send byte 6001 next.”
  3. Segment 2 is lost. Segment 3 arrives but has to wait in the buffer, because bytes 6001–7000 are missing. Your laptop repeats ACK 6001.
  4. Segments 4 and 5 wait too, and each makes your laptop repeat ACK 6001. Three repeats tell the server exactly where the gap starts.
  5. The server resends segment 2. The gap is filled, so bytes 6001–10000 go to the browser at once. Your laptop replies ACK 10001.

Why wait for three? A packet that's merely late can cause one or two repeats, and resending it would waste bandwidth. If the ACKs stop coming at all, a timer on the server runs out and it resends anyway.

TCP also decides how fast to send. It allows only a limited amount of unacknowledged data in flight, called the window. While ACKs keep arriving, it grows the window. When packets go missing, it assumes the network is congested and shrinks it. That's why big downloads start slow and speed up, and how millions of connections share the same links without drowning them.

Encryption · between layers 4 and 7

TLS: the S in HTTPS

TCP gives you a reliable connection to any server, but not a private one. Your packets cross networks run by strangers: the café's Wi-Fi, your ISP, a few backbone companies. Any of them can read every byte. Worse, any of them can change bytes on the way.

  1. Without encryption, your data crosses every one of these networks as readable bytes.
  2. You log in to a site. The packet carries your password as plain text.
  3. The café's Wi-Fi router can read it, and so can other people on the same open Wi-Fi.
  4. The request continues to the server, which replies with your account page.
  5. On the way back, your ISP slips an ad into the page. Your browser has no way to tell the page was changed.

The fix is to encrypt everything: scramble it with a secret key that only you and the server know. That raises a puzzle. How do you agree on a secret key with a server you've never met, when anyone can see everything you send?

Agreeing on a secret in public

The answer is a key exchange. The classic way to picture it uses paint, and one fact: mixing paint is easy, but unmixing it is impossible.

  1. You and the server want a shared secret, but anyone can watch what you send. Start with a color everyone knows: yellow.
  2. Each side picks a secret color and never sends it. You pick red. The server picks blue.
  3. Each side mixes its secret into some yellow. You get orange. The server gets green.
  4. You swap mixtures in public. The watcher sees orange and green go by.
  5. Each side mixes its own secret into the mixture it received. Both end up with the same brown.
  6. The watcher has yellow, orange and green, but can't unmix paint to get red or blue. Mixing what it has gives the wrong color.

TLS does the same thing with numbers. The usual method, X25519, mixes them using math on an elliptic curve. It takes a computer microseconds to do and would take longer than the age of the universe to undo. The shared result is turned into the keys that encrypt the rest of the conversation.

Who's on the other end?

A key exchange gives you a secret shared with whoever answered. That might not be aknight.dev.

  1. Your connection to aknight.dev runs through networks you don't control. Suppose one of them is hostile.
  2. An attacker in the middle could run one key exchange with you and a second with the server, then read and change everything in between.
  3. So the server also sends a certificate: a signed statement that says “this public key belongs to aknight.dev”.
  4. Your browser checks the signatures. WE1 signed aknight.dev's certificate, and GTS Root R4 signed WE1's. That root is built into your browser, so the chain is trusted.
  5. The attacker can make its own certificate for aknight.dev, but no trusted authority will sign it. Your browser stops and warns you.

Copying the real certificate doesn't help either. During the handshake, the server proves it holds the certificate's private key by signing with it, and that key never leaves the server.

Browsers and operating systems ship with about 150 trusted root certificates. The authorities behind them check that whoever asks for a certificate really controls the domain. aknight.dev's certificate lasts 90 days and renews automatically.

The TLS 1.3 handshake

Real TLS fits all of this into one round trip, right after the TCP handshake.

  1. Your laptop and the server, with a TCP connection already open. Watch what a router on the path can see.
  2. ClientHello: “I'd like aknight.dev. Here are the ciphers I support, and my half of the key exchange.”
  3. ServerHello: the server's half. Both sides now compute the same secret key, like the brown paint.
  4. From here on, everything is encrypted. The server sends its certificate, a signature made with its private key, and a Finished message.
  5. Your browser checks the certificate and signature, then sends its own Finished. One round trip, and the channel is secure.
  6. The HTTP request goes out encrypted. The router sees only scrambled bytes.

An observer can still see who is talking to whom: the IP addresses, the ports, how much data flows, and usually the site name, which the browser sends in its first message. Every page and every password stays scrambled.

SSL is the old name. TLS replaced SSL in 1999, and the last version of SSL was formally retired in 2015 after attacks broke it. People still say “SSL certificate”. They mean the certificate TLS uses.

Layer 7 · Application

DNS: from a name to a number

You now have everything you need to talk securely to any computer whose IP address you know. But you typed a name, aknight.dev, and routers only understand numbers.

The Domain Name System is the internet's phone book, spread across thousands of servers. No single server knows every name. Each part of a name has its own servers: the root servers know where to find the .dev servers, the .dev servers know where to find aknight.dev's name servers, and those know its address.

  1. You typed aknight.dev, but packets need an IP address. Your laptop asks a resolver, usually run by your ISP or a service like Cloudflare's 1.1.1.1.
  2. Your laptop sends one small UDP packet to the resolver's port 53: “What's the address of aknight.dev?”
  3. The resolver doesn't know yet, so it starts at the top. A root server replies: “Not me, but here are the servers for .dev.”
  4. A .dev server replies: “aknight.dev uses Cloudflare's name servers, buck and pearl. Ask them.”
  5. Cloudflare's name server knows the answer: 104.21.71.112.
  6. The resolver passes the answer back and caches it for 5 minutes. Anyone else who asks in that time gets the answer at once.

A DNS question and its answer usually fit in one packet each, so DNS normally uses UDP. Answers too big for one packet switch to TCP. If no answer comes back, your computer asks again.

Caching is what makes DNS fast. Every answer says how long it may be kept. Most lookups never get past your own computer's cache or the resolver's.

Layer 7 · Application

HTTP: asking for the page

At last the browser can ask for what it wants. HTTP is a simple conversation in plain text. The browser sends a request, and the server sends back a response.

  1. The connection is ready: TCP keeps it reliable and TLS keeps it private. Now the browser asks for the page.
  2. The browser writes a request. The first line says what it wants: GET this path, using HTTP/1.1. Headers follow, one per line, and a blank line ends the request.
  3. The request travels through the encrypted connection to the server.
  4. The server replies with a status line, its own headers, a blank line, and then the page itself. Status 200 means OK.
  5. The page lists other files it needs, like a stylesheet and scripts. The browser requests each one over the same connection.

The status code tells the browser what happened. The common ones:

  • 200 OK. Here it is.
  • 301 Moved. Here's the new address.
  • 304 Not changed since your last visit. Use your saved copy.
  • 404 Not found.
  • 500 The server hit an error.

Your browser most likely used HTTP/2 or HTTP/3 for this page. They carry the same requests and headers in a compact binary form and fetch many files at once over one connection. The conversation underneath is the same.

Every layer at once

All together

Each layer solves one problem and relies on the layer below for everything else. HTTP knows nothing about packets. TCP knows nothing about routes. Ethernet knows nothing about the internet. Here is how they stack up in a single packet.

  1. Here is a request like the one your browser sent for this page: 412 bytes of HTTP text.
  2. TLS encrypts it and adds 22 bytes: a 5-byte header in front, and a type byte and a 16-byte tamper check at the end.
  3. TCP adds a 20-byte header: ports 52814 and 443, plus the sequence and ACK numbers.
  4. IP adds 20 bytes: where the packet is from, where it's going, and the hop counter.
  5. The frame adds a 14-byte header with both MAC addresses in front, and a 4-byte checksum at the end. 492 bytes in all.
  6. Those 492 bytes leave as 3,936 bits, one after another, right behind the preamble.
  7. Each device opens only the layers it needs. Every hop replaces the frame, every router reads IP, and only the server can decrypt TLS to read the HTTP inside.

The whole trip

Now the timeline. Every step that waits for an answer costs a round trip: the time for a signal to reach the server and come back. Light in fiber covers about 200 km per millisecond, so distance sets a hard limit.

  1. You press Enter on aknight.dev. Time runs down the page.
  2. DNS: one round trip to the resolver, if it has the answer cached.
  3. TCP: SYN, then SYN-ACK. One round trip to the server.
  4. TLS: the ClientHello rides along with TCP's last ACK. One round trip later, the connection is encrypted.
  5. HTTP: the request rides along with TLS's Finished. One round trip later, the first bytes of the page arrive.
  6. Four round trips before the first byte. At 25 ms each, that's 100 ms of waiting, no matter how fast your connection is.

That's why big sites run servers in hundreds of cities. A round trip to a server 5,000 km away takes at least 50 ms. To one 50 km away, it takes half a millisecond. It's also why HTTP/3 exists. It replaces TCP with QUIC, which runs over UDP and sets up the connection and the encryption together, in one round trip.

Your visit to this page

Your browser did all of this a moment ago to load this page, and it kept the timings. Cloudflare, which serves this site, answered one small follow-up request and reported what it saw. The page only shows these numbers. It doesn't store or send them anywhere.

What we skipped

Each of these deserves its own explainer.

  • BGP. How networks tell each other which addresses they can reach, and how one bad announcement can knock big services offline.
  • Radio. How Wi-Fi and cell networks turn bits into radio waves and share the air with everyone nearby.
  • IPv6. 128-bit addresses, so every device can have its own.
  • QUIC and HTTP/3. Reliability rebuilt on top of UDP, with encryption built in.
  • CDNs. Copies of a site in hundreds of cities, like the one that served you this page.