Layers: how a request is wrapped
Sending a letter works in layers. You write the letter (the content). You put it in an envelope with an address. The post office puts many envelopes in a bag for the next city, and a truck carries the bag. The truck driver never reads your letter, and you never think about the truck.
Networks work the same way. Each layer solves one problem and relies on the layer below for everything else:
| Layer | Its job | Examples | Identifies things by |
|---|---|---|---|
| Application | What the programs are saying | HTTP, DNS, SMTP | URLs, names |
| Transport | Which program gets the data; reliable delivery if needed | TCP, UDP | Ports |
| Internet | Getting a packet across many networks to the right computer | IP | IP addresses |
| Link | One hop: from this device to the next one | Ethernet, Wi‑Fi | MAC addresses |
When your app sends a request, each layer wraps the data from the layer above with its own header, like putting an envelope in a bag:
[ Wi-Fi header [ IP header [ TCP header [ HTTP request: GET /orders ... ] ] ] ]Your router reads only the outer layers: it looks at the IP address and passes the packet on. Only the server, at the other end, unwraps everything and reads the HTTP request.
Data doesn’t travel in one piece. It’s cut into packets, and each link has a maximum packet size, the MTU (maximum transmission unit). On Ethernet and most Wi‑Fi it’s 1,500 bytes, including the IP and TCP headers. A 2 MB image is sent as well over a thousand packets, which may take different routes and arrive in a different order. Putting them back together is the transport layer’s job.
You’ll also hear about the OSI model, which splits the same idea into seven layers. People use its numbers as names: “layer 3” is IP, “layer 4” is TCP and UDP, “layer 7” is the application. That’s where “an L7 load balancer” comes from.
- Models: the TCP/IP model has four layers (link, internet, transport, application); OSIOSI modelA seven-layer model of networking whose layer numbers are used as jargon (L3, L4, L7).Open in glossary → has seven, with session and presentation between transport and application, which real protocols don’t separate cleanly (TLS spans “5–6”). The numbers survive as jargon: L2 switch, L3 router, L4 and L7 load balancers.
- Encapsulation overhead: IPv4 header 20 bytes (IPv6 40), TCP 20 (plus options, usually 32 with timestamps), UDP 8. With a 1,500-byte MTU, a TCP segment carries at most 1,460 bytes of payload (the MSS), less with options.
- Smaller MTUs appear on PPPoE home links (1,492), VPNs and tunnels (WireGuard, IPsec, VXLAN overlays in Kubernetes) and some mobile networks. IPv4 routers can fragment; IPv6 routers never do.
- Path MTU discovery sends packets with “don’t fragment” and lowers the size when a router replies “fragmentation needed” over ICMP. Firewalls that drop all ICMP create PMTU black holes: the TCP handshake (small packets) works, then large responses hang. Fixes: allow ICMP type 3 code 4 (IPv4) and ICMPv6 Packet Too Big, or clamp the MSS on the tunnel.
- Where things live: TLS sits between TCP and HTTP; QUIC (HTTP/3) runs over UDP and does transport and TLS together; ARP (IPv4) and Neighbor Discovery (IPv6) map IP addresses to MAC addresses on the local link.
Predict the output
ping -D sets 'don't fragment', and -s is the data size; ICMP and IP headers add 28 bytes. On a home connection that uses PPPoE (MTU 1,492 on the link to the provider), what happens to the two pings?
# macOS (Linux: ping -M do -s ...)
ping -c 1 -D -s 1464 1.1.1.1
ping -c 1 -D -s 1472 1.1.1.11,464 + 28 = 1,492 bytes fits exactly. 1,472 + 28 = 1,500 fits the Wi-Fi or Ethernet link to the router, but not the PPPoE link after it, and the packet may not be fragmented, so the home router answers 'fragmentation needed' and names the MTU. This is how path MTU discovery finds the largest packet a route can carry, and why a firewall that drops these replies breaks large transfers.
# Application layer: an HTTP request, with the connection details curl sees
curl -v https://example.com/ -o /dev/null
# Internet layer: the routers (hops) between you and a server
traceroute example.com # Windows: tracert
# Link layer: your network interfaces, their MAC and IP addresses, and the MTU
ifconfig en0 # Linux: ip addr show
# Every layer at once: capture packets to port 443 (needs sudo)
sudo tcpdump -n -i en0 'tcp port 443' -c 10Worked example: one request, layer by layer
Your phone, on home Wi‑Fi, opens https://shop.example/orders. Follow the first packet of the request.
- Application. The app builds the HTTP request:
GET /orders, with headers. TLS encrypts it, so nobody on the way can read it. - Transport. TCP adds a header with two ports: the server’s port 443 (HTTPS), and a random port on your phone so the reply finds the right app. It also adds a sequence number so the server can put packets in order.
- Internet. IP adds a header with your phone’s address and the server’s address. This header is what every router on the way reads.
- Link. Wi‑Fi adds a header with the MAC address of your home router, the next hop. That’s all the Wi‑Fi radio needs to know.
- Router. Your router removes the Wi‑Fi header, reads the IP address, and sends the packet on to your internet provider inside a new link-layer header. Every router on the way repeats this step with a fresh link header; the IP header stays.
- Server. The last router delivers the packet to the server. Its operating system removes the headers one by one: the IP address matches, TCP port 443 belongs to the web server, and the web server finally sees
GET /orders.
Twenty routers might handle the packet, and none of them looked further than the IP header.
Deep diveL4 vs L7 load balancersInterview favorite
A layer 4 load balancer sees IP addresses and ports. It picks a backend when a TCP connection (or UDP flow) starts and forwards packets for that connection without reading them. It’s fast, protocol-agnostic (databases, MQTT, gRPC, anything over TCP) and can pass TLS through untouched, so certificates stay on the backends. It can’t route by URL, retry a failed HTTP request, or balance individual requests on a long-lived connection: one HTTP/2 or gRPC connection carrying thousands of requests all lands on one backend. Examples: AWS NLB, Google’s network load balancer, IPVS, HAProxy in TCP mode.
A layer 7 load balancer terminates the connection, reads the HTTP request and opens its own connection to a backend. It can route /api and /static to different services, balance per request (fixing the gRPC problem), add headers like X-Forwarded-For, retry, rate limit, compress and terminate TLS in one place. It costs more CPU and latency, and it must understand the protocol. Examples: AWS ALB, Envoy, NGINX, HAProxy in HTTP mode, Cloudflare.
Typical answers: L4 in front of everything for raw throughput and non-HTTP traffic, L7 behind it (or alone) for HTTP routing. The backend behind an L7 proxy sees the proxy’s IP, so client IPs come from X-Forwarded-For (trust only what your own proxy set) or, for L4, from the PROXY protocol. See Scaling and load balancing.
- TCP/IP: link, internet (IP), transport (TCP/UDP, ports), application. OSI numbers as jargon: L3 IP, L4 TCP/UDP, L7 HTTP.
- MTU 1,500 on Ethernet; MSS about 1,460; PPPoE 1,492, tunnels less. PMTU discovery needs ICMP; dropping it causes black holes (handshake works, big responses hang).
- L4 balancers forward connections (fast, any protocol, TLS passthrough, one backend per connection); L7 balancers terminate and route per request (paths, retries, headers, gRPC balancing).
ping -c 1 -D -s 1472 host # largest unfragmented IPv4 payload on a 1,500 MTU (Linux: -M do)
traceroute host # the L3 path
sudo tcpdump -n 'tcp port 443' # see the packetsCheck this part
Which layer gets a packet across many networks to the right computer?
What does a router on the way read to forward your packet?
The TCP handshake to a server over a VPN works, but large responses hang forever. What is a likely cause?
gRPC clients each keep one HTTP/2 connection, and one backend behind an L4 balancer gets all the load. Why?