CMD Guide
HomeSystem DesignNetworking Fundamentals

The Layered Network Model — OSI, TCP/IP, and L4 vs L7

Why a layered model?

Networking is hard because a single request crosses many concerns at once: physical signalling, addressing, reliable delivery, encryption, and application semantics. The industry tames this with layering — each layer solves one problem and hands a clean abstraction to the layer above. You will not memorise the OSI model to build systems, but the vocabulary it gives you ("that's an L4 load balancer", "TLS sits at the session/presentation boundary", "this is an L7 routing rule") is how engineers talk about where a behaviour lives.

The OSI seven layers

OSI is the conceptual reference. Read it bottom-up — bits become frames, frames become packets, packets become reliable streams, streams carry application messages:

  1. Physical — raw bits over a medium (copper, fibre, radio).
  2. Data link — framing and MAC addressing on a local segment (Ethernet, Wi-Fi).
  3. Network — logical addressing and routing across networks (IP).
  4. Transport — end-to-end delivery, ports, reliability (TCP, UDP).
  5. Session — establishing, managing, and tearing down connections.
  6. Presentation — encoding, serialization, encryption (where TLS conceptually lives).
  7. Application — the protocol your software speaks (HTTP, gRPC, SMTP, DNS).
The seven layers of the OSI model
The seven layers of the OSI model

TCP/IP — the model the internet actually runs on

The OSI model is a teaching tool; the internet is built on the leaner TCP/IP stack, which collapses OSI's top three layers into a single Application layer: Link → Internet (IP) → Transport (TCP/UDP) → Application. When people say "the network stack", this is usually what they mean.

TCP vs UDP — the transport choice that matters most

At the transport layer you pick reliability or speed. TCP is connection-oriented: a three-way handshake, ordered delivery, retransmission, and flow/congestion control. It is the right default for anything where correctness beats latency — web pages, APIs, file transfer.

TCP connection-oriented delivery with handshake and acknowledgements
TCP connection-oriented delivery with handshake and acknowledgements

UDP is connectionless: fire-and-forget datagrams with no handshake, no ordering, and no retransmission. You trade guarantees for the lowest possible latency, which is why it underpins DNS lookups, real-time voice/video, and gaming — and why QUIC (the basis of HTTP/3) is built on UDP while re-implementing reliability in user space.

UDP connectionless datagram delivery
UDP connectionless datagram delivery

L4 vs L7 — the distinction you will use constantly

"Layer 4" and "Layer 7" are shorthand for how much of the request a piece of infrastructure understands:

L4 (Transport)L7 (Application)
SeesIP addresses + TCP/UDP portsThe full request: URL, headers, cookies, body
DecisionsForward this connection to a backendRoute /api/* here, /img/* there; rewrite headers; terminate TLS
CostCheap, extremely fastMore CPU, but content-aware
ExampleAn L4 TCP load balancerAn L7 reverse proxy / API gateway

This is exactly why your load balancer can operate at L4 or L7, and why an API gateway is an L7 device — it has to read paths and headers to route. TLS termination happening "at the edge" means the L7 proxy decrypts so it can see the application data underneath.

Takeaways

What breaks at each layer

Layering is not magic; every layer has its own failure mode. Physical links can corrupt bits; Ethernet at the data-link layer drops frames that fail a CRC check. IP is best-effort, so packets can be lost, duplicated, or reordered. TCP hides most of that by retransmitting, but recovery takes time (duplicate ACKs or a retransmission timeout), so the application can still see stalls. TLS handshake failures, DNS resolution timeouts, and application-level request timeouts all happen on top of a working stack. The lesson: reliability at one layer does not remove the need for timeouts and retries at the next.

Selection & trade-offs: when not to use each choice

Trace an HTTPS request through the stack

When your browser requests https://example.com/index.html from 192.168.1.10 to server 93.184.216.34:

LayerWhat it addsKey value
ApplicationHTTP GET /index.html + Host headerthe resource path
TransportTCP segment, source port 54321, destination port 443; TLS record insideports & reliability
NetworkIP packet, source 192.168.1.10, destination 93.184.216.34host-to-host routing
LinkEthernet frame with source NIC MAC and gateway MAClocal delivery

At the receiver each layer strips its header and hands the payload up. The same layering that makes the request easy to reason about also makes failures local: a bad cable, a full NAT table, or a lost IP packet only affects the layers that must recover from it.

Failure-mode table per layer

LayerWhat can breakSymptomHow to detect / fix
PhysicalCut cable, bad transceiver, interferenceNo link light, packet loss, FCS errorsCheck optics/cables, error counters, replace hardware
Data linkDuplex mismatch / MAC-table exhaustion, spanning-tree loop, MTU mismatchIntermittent connectivity, broadcast stormsSwitch logs, STP status, verify MTU end-to-end
NetworkRouting loop / TTL expiryTimeouts; traceroute shows repeating hops; ICMP Time ExceededTraceroute, routing tables
NetworkStale / misconfigured routePackets reach the wrong host or are blackholedRouting tables, traceroute
NetworkNAT table fullNew outbound connections hang or fail while established flows keep workingNAT session counters, connection pooling, more egress IPs
TransportPort unreachable, SYN flood, congestion lossTimeouts, retransmits, slow throughputTcpdump, ss/netstat, congestion algorithm, firewall rules
Session/PresentationTLS certificate expiry, cipher mismatchHandshake failure, certificate warningsCertificate monitoring, TLS version policy
ApplicationBug, rate limit, bad payloadHTTP 5xx, 4xx, malformed responsesApplication logs, metrics, health checks

A dated entry you will still hear in this table's slot: "MAC collisions." Collisions only existed on shared half-duplex Ethernet (CSMA/CD); on today's switched full-duplex links the analogous failure is a duplex mismatch — one side believes the link is half-duplex and reports late collisions and CRC errors while the other side sees mysterious slowness.

L4 vs L7 decision matrix

RequirementUse L4Use L7
Route by IP/port onlyYesOverkill; slower and more expensive
Route by URL path or headerNoYes
Terminate TLS at the edgeCannot inspect payloadYes
Lowest latency / highest throughputYes (kernel bypass possible)Usually slower
WAF, auth, rate limiting per userNoYes
Preserve client source IPYes (with DNAT)Needs PROXY protocol or X-Forwarded-For

Interviewer traps

Four follow-ups that separate "memorised the table" from "understands the stack":


Re-authored for this guide, with concepts and diagrams adapted from Karan Pratap Singh’s System Design (course, MIT licence) and the System Design Primer (CC BY 4.0). Diagrams © their respective authors.

🤖 Don't fully get this? Learn it with Claude

Stuck on The Layered Network Model — OSI, TCP/IP, and L4 vs L7? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **The Layered Network Model — OSI, TCP/IP, and L4 vs L7** (System Design) and want to truly understand it. Explain The Layered Network Model — OSI, TCP/IP, and L4 vs L7 from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **The Layered Network Model — OSI, TCP/IP, and L4 vs L7** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **The Layered Network Model — OSI, TCP/IP, and L4 vs L7** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **The Layered Network Model — OSI, TCP/IP, and L4 vs L7** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes