Chapters

How a request leaves your laptop

One line of JavaScript calls a server in the cloud. This chapter follows that call from the browser until it leaves your laptop as a signal. Each topic here gets its own deep-dive chapter later.

Addressing and local delivery · chapter 1 of 34 · 10 min read

  1. Browserapp
  2. OS stackkernel
  3. NIChardware
  4. Home routerfirst hop
  5. ISP + internetmany hops
  6. Google networkedge + VPC
  7. VMOS + NIC
  8. Node appyour code

This chapter covers the first three stopslater chapters cover the rest of the trip

The example we follow

You open Kadé's website on your laptop at home. The page calls Kadé's API: a Node.js app running on a virtual machine (VM) called kade-api in Google Cloud (GCP). The call uses HTTPS. The full setup, with every address, is in chapter 0.

your laptop · 192.168.1.23Browserruns your fetch()OS network stackHome router192.168.1.1InternetISP + many routersrequestGCP · asia-southeast1kade-api (VM)34.87.120.15Node applisteningthe response comes back the same way

Wide by design - scroll it sideways.

In code, it starts with one line:

const response = await fetch("https://api.kade.lk/profile");

This asks the browser to get the /profile resource from the server named api.kade.lk, using HTTPS. Before anything can be sent, the browser and the operating system (OS) must know four values. Together they name one connection:

ValueExampleWhere it comes fromWhy it matters
Source IP192.168.1.23Your laptopgiven by the home router when you join the WiFiWhere the reply goesthe server answers to this address
Source port52814The OSa free temporary port, picked per connectionWhich program gets the replyyour laptop may have many open connections
Destination IP34.87.120.15DNS lookupturns api.kade.lk into an IP addressWhich machine to reachrouters forward on this value
Destination port443The URL schemehttps:// means port 443 by defaultWhich program on that machinethe process listening on 443
An IP address finds the machine. A port finds the program on that machine. Green means IP, amber means TCP, in every diagram in these notes.

Finding the server: DNS

Computers route by IP address, not by name. DNS (Domain Name System) is the internet's phone book: it turns api.kade.lk into an IP address. No single server knows every name, so the answer is found by walking a chain of servers, from the most general to the most specific.

Browserchecks its own cache,then the OS cacheRecursive resolveryour ISP, or 8.8.8.8asks others for youRoot serverreply: ask the .lk servers.lk TLD serverreply: ask kade.lk's serversAuthoritative serverreply: 34.87.120.1512345

1: the browser asks the resolver "where is api.kade.lk?". 2 to 4: the resolver asks the root, then the .lk servers, then the servers that own kade.lk. 5: the answer comes back. Every step keeps a copy (cache) for a set time called the TTL, so the next lookup is much faster.

Wide by design - scroll it sideways.

Writing the HTTP request

Now the browser knows where to send. Next it writes the message itself: the HTTP request. HTTP is plain text with a fixed shape: one request line, then headers, then an empty line (and a body, for requests like POST).

GET /profile HTTP/1.1Method + path + version
Host: api.kade.lkWhich site on the server
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9…Who you are (auth)
Cookie: session_id=8f2a91cYour session (cookies)
Accept: application/jsonWhat format you want
User-Agent: Mozilla/5.0 …Which client is asking
(empty line)End of headers
The method (GET) says what to do. The path (/profile) says what to do it to. Headers carry extra facts about the request. Cookies are only attached when the browser's rules allow it for that site.

HTTP rides on TCP

HTTP only defines what the message says. It does not move anything by itself. The moving is done by TCP (Transmission Control Protocol). TCP makes delivery reliable and in order, using mechanisms like the three-way handshake, flow control and congestion control. Each of these gets a deep-dive chapter later.

HTTP

What you want to say.

"Give me /profile, here is my session."

TCP

The reliable courier that makes sure it arrives, complete and in order.

three-way handshakeflow controlcongestion control

Text becomes bytes

The network does not move text. It moves bytes. An encoder turns each character into its byte value. We usually write bytes in hexadecimal (base 16), so each byte is two characters from 00 to FF.

  1. G47
  2. E45
  3. T54
  4. space20
  5. /2F
  6. p70
  7. r72
  8. o6F
  9. f66
  10. i69
  11. l6C
  12. e65
  13. space20
  14. H48
  15. T54
  16. T54
  17. P50
  18. /2F
  19. 131
  20. .2E
  21. 131
  22. CR0D
  23. LF0A

The first line of the request: 23 characters become 23 bytes. CR LF (0D 0A) are the invisible “end of line” characters HTTP uses.

Here is the whole request as the raw bytes the OS receives, in the same layout the xxd tool prints: position on the left, bytes in the middle, readable characters on the right.

00000000: 4745 5420 2F70 726F 6669 6C65 2048 5454  GET /profile HTT00000010: 502F 312E 310D 0A48 6F73 743A 2061 7069  P/1.1..Host: api00000020: 2E6B 6164 652E 6C6B 0D0A 4175 7468 6F72  .kade.lk..Author00000030: 697A 6174 696F 6E3A 2042 6561 7265 7220  ization: Bearer 00000040: 6579 4A68 6247 6369 4F69 4A49 557A 4931  eyJhbGciOiJIUzI100000050: 4E69 4A39 0D0A 436F 6F6B 6965 3A20 7365  NiJ9..Cookie: se00000060: 7373 696F 6E5F 6964 3D38 6632 6139 3163  ssion_id=8f2a91c00000070: 0D0A 4163 6365 7074 3A20 6170 706C 6963  ..Accept: applic00000080: 6174 696F 6E2F 6A73 6F6E 0D0A 5573 6572  ation/json..User00000090: 2D41 6765 6E74 3A20 4D6F 7A69 6C6C 612F  -Agent: Mozilla/000000a0: 352E 300D 0A0D 0A 5.0....

167 bytes in total. The first three (47 45 54) are “GET”. The dots on the right are line breaks (0D 0A), which have no printable form. With HTTPS, these bytes are encrypted by TLS before TCP sees them.

The browser hands off to the OS

The browser prepares the message, but it never puts anything on the network itself. It hands the bytes to the operating system's TCP/IP stack: the part of macOS, Windows or Linux that implements TCP, UDP, ICMP and IP. The OS also picks the source port. When the data is ready to go, the stack passes it to the NIC (Network Interface Card): the hardware that turns bits into signals, electrical pulses on a cable or radio waves in the air.

user spacekernel (the OS)hardwaremediumBrowser (your app)writes the HTTP request, asks for DNS, asks the OS to sendhands over bytes (writes to a socket)OS TCP/IP stackimplements TCP, UDP, ICMP, IP · picks the source portsplits the data and adds the headers (next sections)passes finished framesto the driverNIC (network card)turns bits into physical signalswire or air: radio (WiFi), electrical (Ethernet), light (fiber)prepares themessagetransmits it

Wide by design - scroll it sideways.

Breaking data into chunks

Data cannot travel as one big blob. A 500-byte request or a 1 GB file is cut into many smaller chunks. Each chunk travels on its own and they are put back together at the destination. Say we have 4,000 bytes to send:

4,000 bytes of request datachunk 1 · 1,460 bytesbytes 1-1460 · seq 1chunk 2 · 1,460 bytesbytes 1461-2920 · seq 1461chunk 3 · 1,080bytes 2921-4000 · seq 2921each chunk travels in its own TCP segment, inside its own IP packet

Chunk size is limited by the MSS (below). Sequence numbers count bytes. Real connections start from a random number; we start from 1 to keep it readable.

Wide by design - scroll it sideways.

Two limits decide the chunk size:

  • MTU (Maximum Transmission Unit): the biggest packet a link can carry in one frame. On Ethernet and most WiFi this is 1,500 bytes.
  • MSS (Maximum Segment Size): the biggest chunk of data one TCP segment can hold. It is what is left of the MTU after the IP and TCP headers.
frame on the cable: 1,518 bytesIP packet: up to the MTU, 1,500 bytesEth14IP header20TCP header20Data: one chunkup to 1,460FCS4TCP segment: 20 + up to 1,460chunk: up to the MSS, 1,460 = 1,500 − 20 (IP) − 20 (TCP)

Sizes in bytes, not to scale.

Wide by design - scroll it sideways.

Wrap 1: the TCP segment

Now each chunk gets wrapped, like putting a letter in an envelope. First, TCP adds its header. The result is called a TCP segment. The header holds the source port (52814), the destination port (443) and, most importantly, a sequence number.

TCP headersrc port 52814dst port 443seq 1Data (the chunk)HTTP bytes, TLS-encryptedup to 1,460 bytesTCP segment (new)bytes go out left to right: outer headers first, then the data. Dashed boxes are wraps still to come.

Wide by design - scroll it sideways.

The sequence number is the heart of TCP's reliability. Chunks can take different paths and arrive out of order. The receiver uses the numbers to put them back in order, and to notice when one is missing:

sentseq 1 seq 1461 seq 2921
arrivedseq 1461 seq 2921 seq 1 order mixed up on the way
rebuiltseq 1 seq 1461 seq 2921 sorted by number, a gap would mean a lost chunk

Wrap 2: the IP packet

Next, the IP layer wraps the TCP segment and adds the source IP (192.168.1.23) and destination IP (34.87.120.15). The result is an IP packet.

IP headersrc 192.168.1.23dst 34.87.120.15TCP headersrc port 52814dst port 443seq 1Data (the chunk)HTTP bytes, TLS-encryptedup to 1,460 bytesTCP segmentIP packet (new)bytes go out left to right: outer headers first, then the data. Dashed boxes are wraps still to come.

Wide by design - scroll it sideways.

Every router on the way reads the destination IP and decides which way to forward the packet next. No router knows the full path. Each one only picks the next step, called a hop. The source IP is what lets the server know where to send the response.

  1. Laptop
  2. Home routernext hop?
  3. ISP routernext hop?
  4. … more …next hop?
  5. Google edgenext hop?
  6. VM
Each router asks one question of the destination IP 34.87.120.15: "which neighbour is closer to this?"

Wrap 3: the frame

The last wrap adds physical addresses, called MAC addresses. Every network card has one, for example a4:83:e7:2b:91:0c. The result is a frame. The frame also gets a small error check at the end (FCS) so the receiver can spot damaged frames.

Ethernet headerdst 3c:84:6a:10:ee:01src a4:83:e7:2b:91:0cIP headersrc 192.168.1.23dst 34.87.120.15TCP headersrc port 52814dst port 443seq 1Data (the chunk)HTTP bytes, TLS-encryptedup to 1,460 bytesFCSerrorcheckTCP segmentIP packetFrame (new)bytes go out left to right: outer headers first, then the data. Dashed boxes are wraps still to come.

Wide by design - scroll it sideways.

The source MAC is your laptop's NIC. But what is the destination MAC? The server is not on your local network, so you do not know its MAC, and could not use it anyway. Instead, the destination MAC is your default gateway: the home router, the device that forwards traffic out of your network.

This leads to an important pattern. IP addresses describe the whole trip. MAC addresses only describe the current step:

HopMAC addresses (frame)IP addresses (packet)
Laptop → home routera4:83:e7:2b:91:0c → 3c:84:6a:10:ee:01192.168.1.23 → 34.87.120.15
Home router → ISP routerrouter's WAN MAC → ISP router's MACyour public IP → 34.87.120.15
Every later hopnew pair every timestays the same
Bold marks what changed. The MAC pair is rewritten at every hop. The destination IP never changes. The source IP changes once, at your home router (NAT).

ARP, then onto the wire

The laptop knows the router's IP (192.168.1.1) from its network settings. But how does it learn the router's MAC? With ARP (Address Resolution Protocol). The laptop shouts a question to every device on the local network, and only the owner of that IP answers.

Your laptop192.168.1.23a4:83:e7:2b:91:0cPhone192.168.1.40 · not me, ignoreSmart TV192.168.1.52 · not me, ignoreHome router192.168.1.1 · that's me1 · broadcast to ff:ff:ff:ff:ff:ff (everyone)"who has 192.168.1.1? tell 192.168.1.23"2 · reply only to the laptop"192.168.1.1 is at 3c:84:6a:10:ee:01"

The laptop saves the answer in its ARP cache, so it does not ask again for every frame. ARP is like DNS for the local network: DNS maps a name to an IP, ARP maps an IP to a MAC.

Wide by design - scroll it sideways.

With the router's MAC filled in, the frame is complete. The OS hands it to the NIC, which sends it as physical signals:

WiFi

Radio waves through the air.

Ethernet

Electrical pulses on a copper cable.

Fiber

Pulses of light in a glass strand.

Where we are now

After all of this, the data has only reached your home router. We have covered roughly the first quarter of the trip.

  1. Browserapp
  2. OS stackkernel
  3. you are hereNIChardware
  4. Home routerfirst hop
  5. ISP + internetmany hops
  6. Google networkedge + VPC
  7. VMOS + NIC
  8. Node appyour code

Chapter 1: browser → NIC → first frame toward the routerstill ahead: routing, the GCP side, the server, the response

Everything above is unpacked, routed and repeated at every hop. The rest of these notes follow the frame from here to the Node app and back.

Summary

#StepWhat happensDone byNew terms
1Name the targetfetch() gives a URL; four values define the connectionBrowsersource/destination IP and port
2Find the IPResolver walks root → TLD → authoritativeBrowser, resolverDNS, TTL, cache
3Write the requestMethod, path, headers, auth, cookiesBrowserHTTP
4Pick a courierHTTP is carried by TCP (and encrypted by TLS)Browser, OSTCP, TLS
5EncodeText turned into bytesBrowserbytes, hexadecimal
6Hand offBytes given to the OS, which picks the source portOSTCP/IP stack, NIC
7ChunkData cut into pieces of at most 1 MSSOS (TCP)MTU, MSS
8Wrap 1Add ports + sequence numberOS (TCP)segment
9Wrap 2Add source + destination IPOS (IP)packet, hop
10Wrap 3Add source MAC + router's MACOS, driverframe, MAC, default gateway
11Find router MACBroadcast "who has 192.168.1.1?"OSARP, ARP cache
12TransmitFrame sent as radio, electrical or light signalsNICphysical medium

Try it yourself

Each of these shows one step of this chapter on your own machine.

DNS answer

  • What to look for: The IP the name resolves to. Run dig +trace example.com to see the root → TLD → authoritative chain.
  • Runs on: macOS / Linux · Windows: nslookup example.com
  • From section: 2. Finding the server: DNS
dig example.com +short

The HTTP request and response headers

curl -v https://example.com

Text as bytes

printf 'GET /profile' | xxd

Open TCP connections

  • What to look for: Each line shows the four values: local IP:port and remote IP:port. Note the random local ports.
  • Runs on: Linux · macOS: netstat -an -p tcp · Windows: netstat -an
  • From section: 1. The example we follow
ss -tn

Your IP and MAC

  • What to look for: inet is your IP, ether / link address is your MAC.
  • Runs on: Linux · macOS: ifconfig · Windows: ipconfig /all
  • From section: 10. Wrap 3: the frame
ip addr

Your default gateway

  • What to look for: The default line shows your router's IP.
  • Runs on: Linux · macOS: netstat -rn · Windows: route print
  • From section: 10. Wrap 3: the frame
ip route

Your ARP cache

  • What to look for: The router's IP next to its MAC address.
  • Runs on: macOS / Linux / Windows
  • From section: 11. ARP, then onto the wire
arp -a

The MTU limit

  • What to look for: 1472 + 8 (ICMP) + 20 (IP) = 1500 works. Try 1473: it fails because the packet no longer fits.
  • Runs on: macOS · Linux: ping -M do -s 1472 · Windows: ping -f -l 1472
  • From section: 7. Breaking data into chunks
ping -D -s 1472 1.1.1.1

Spotted a mistake, or something unclear? Suggest an edit on GitHub