TCP/IP & the Layers That Matter
TCP/IP & the Layers That Matter
Every production incident involving connectivity — a pod that cannot reach its database, a load balancer timing out, a microservice throwing Connection refused — traces back to one of four conceptual layers. Before you reach for tcpdump or open a firewall rule, you need a precise mental model of where in the stack the failure lives. That model is the TCP/IP four-layer stack.
The Four Layers
Each layer adds a header (and sometimes a trailer) to the payload handed down from the layer above. This process is called encapsulation. On the receiving end, each layer strips its own header and passes the remainder up — decapsulation.
Layer 1 — Network Access (Link Layer)
This layer deals with physical transmission between two directly connected nodes — the NIC driver, Ethernet frames, MAC addresses, and VLANs. In cloud environments the "physical" medium is virtual: VXLAN tunnels (used by Kubernetes CNI plugins like Flannel and Calico) wrap IP packets inside UDP datagrams so overlay networks span multiple physical hosts.
DevOps engineers rarely configure this layer directly, but its failure modes surface constantly: MTU mismatches cause mysterious packet drops that look like application timeouts. Check MTU with:
Layer 2 — Internet (Network Layer)
IP is the universal glue. Every packet carries a source IP, a destination IP, a TTL (Time To Live, decremented by each router hop), and a protocol field (6 = TCP, 17 = UDP, 1 = ICMP). Routing decisions happen here — the kernel consults its routing table and either delivers locally or forwards to a gateway.
traceroute stops at hop 5, a firewall or black-hole route exists there. If TTL reaches the destination but connections still fail, the problem is at Layer 3 (Transport) or above.
Layer 3 — Transport
Transport adds end-to-end delivery semantics. The two dominant protocols:
- TCP — connection-oriented, reliable, ordered. Uses a three-way handshake (SYN / SYN-ACK / ACK) before data flows. Retransmits lost segments. Used by HTTP/1.1, HTTP/2, gRPC, PostgreSQL, Redis.
- UDP — connectionless, unreliable, low-latency. No handshake, no retransmit. Used by DNS lookups, QUIC/HTTP/3, VoIP, game servers, and some monitoring agents.
Ports live at this layer. A socket is identified by the four-tuple: (src IP, src port, dst IP, dst port). The OS uses this tuple to demultiplex incoming segments to the right process. Ephemeral ports (typically 32768–60999 on Linux) are assigned by the kernel for outbound connections.
Layer 4 — Application
Everything above TCP/UDP is the application layer: HTTP, TLS (treated as an application-level concern in this model), DNS, SSH, gRPC, and your own microservices. This is where request routing, authentication, and business logic live.
Encapsulation in Action: A Real HTTP Request
When your application calls POST https://api.example.com/orders, here is what actually happens at each layer on the sending machine:
- Application: HTTP library serialises headers + JSON body into bytes.
- Transport: Kernel TCP stack wraps in a TCP segment with src/dst ports, sequence number, flags.
- Internet: IP layer prepends the IP header with src/dst IP addresses and TTL.
- Network Access: NIC driver wraps in an Ethernet frame with src/dst MAC addresses and sends on the wire (or VXLAN tunnel in cloud).
On the receiving server each layer peels its header, validates checksums, and passes the payload up. The final payload — the HTTP bytes — lands in the server process's socket buffer.
Capturing Packets Across Layers
tcpdump on a busy interface with no filter can generate hundreds of MB of output per second and spike CPU on the host. Always add a host or port filter, use -c <count> to limit packet capture, and write to a file with -w rather than printing to the terminal. On Kubernetes, use kubectl debug with an ephemeral container running tcpdump — never install tools directly in production containers.
Key Takeaways
- The TCP/IP model has four layers; each adds a header during encapsulation and removes it during decapsulation.
- Layer 1 (Network Access): physical delivery, MAC addresses, MTU — VXLAN in cloud/Kubernetes overlays.
- Layer 2 (Internet): IP addresses, routing tables, TTL, ICMP — where
ip routeandtracerouteoperate. - Layer 3 (Transport): TCP (reliable) vs UDP (low-latency), ports, the four-tuple socket identifier — where
ssand firewall rules operate. - Layer 4 (Application): HTTP, DNS, TLS, gRPC — your code and most of your config.
- Triage incidents by layer: confirm physical reachability first (
ping), then routing (traceroute), then port reachability (telnet/nc), then application protocol (curl -v).