We’re Officially Live β€” Lifetime Founding Membership Available for a Limited Time
Free preview

OSI vs TCP/IP Model

Why networking is split into stacked layers, how the 7-layer OSI reference maps onto the 4-layer TCP/IP model the internet actually runs on, and how encapsulation wraps your data.

20 min read

In this lesson:


Why layers

Sending a sensor reading to a cloud server involves dozens of concerns: voltage on a wire, addressing, reliable delivery, encryption, message format. Trying to solve all of that in one blob is unmanageable. Networking is instead split into layers, each handling one job and talking only to the layer directly above and below it.

The payoff is modularity: swap the bottom layer (WiFi instead of Ethernet) and everything above keeps working unchanged; improve TCP without touching your application. Each layer is a contract.


Two models

OSI (Open Systems Interconnection) is the 7-layer reference model, a teaching and design framework. TCP/IP is the 4-layer model the real internet runs on. They map onto each other:

   OSI (7-layer reference)            TCP/IP (4-layer, real internet)
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ 7 Application          β”‚          β”‚                       β”‚
 β”‚ 6 Presentation         β”‚ ───────▢ β”‚ Application           β”‚  HTTP, MQTT, CoAP, TLS
 β”‚ 5 Session              β”‚          β”‚                       β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€          β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ 4 Transport            β”‚ ───────▢ β”‚ Transport             β”‚  TCP, UDP
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€          β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ 3 Network              β”‚ ───────▢ β”‚ Internet              β”‚  IP, ICMP
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€          β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ 2 Data Link            β”‚ ───────▢ β”‚ Link                  β”‚  Ethernet, WiFi MAC
 β”‚ 1 Physical             β”‚          β”‚                       β”‚  (cables, radio, PHY)
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

What each TCP/IP layer does:

Layer Job Examples
Application the actual service / message format HTTP, MQTT, CoAP, DNS, TLS
Transport end-to-end delivery between programs (ports) TCP (reliable), UDP (best-effort)
Internet addressing and routing between networks IP, ICMP
Link move frames over one physical link Ethernet, WiFi, PHY

OSI's upper three (session, presentation, application) collapse into TCP/IP's single Application layer; in practice the internet is TCP/IP, and OSI is the vocabulary everyone uses to discuss it.


Encapsulation: wrapping on the way down

When you send data, each layer wraps the data from the layer above in its own header (and the link layer adds a trailer). The receiver peels them off in reverse.

 your data:                                   [ payload ]
 Transport (TCP): add TCP header        [TCP|  payload ]   β†’ "segment"
 Internet (IP):   add IP header     [IP |TCP|  payload ]   β†’ "packet"
 Link (Ethernet): add MAC hdr+FCS [ETH|IP |TCP| payload|FCS] β†’ "frame"
 Physical:        send as bits     β–β–ˆβ–β–ˆβ–β–ˆβ–β–ˆ...

Each layer only reads its header and treats everything inside as opaque payload. That's why a router (Internet layer) inspects the IP header but doesn't care about your TCP or application data, and a switch (Link layer) looks only at MAC addresses. The names matter in interviews: segment (transport) β†’ packet (IP) β†’ frame (link).

Encapsulation also has a cost: every header is overhead. On a constrained IoT link with a small MTU (maximum transmission unit), TCP+IP+link headers can be a big fraction of a small message, a reason lightweight stacks and CoAP-over-UDP exist.


Where an embedded stack fits

On an MCU you typically run a small TCP/IP stack like lwIP (lightweight IP) that implements the Link, Internet, and Transport layers (Ethernet/WiFi driver β†’ IP β†’ TCP/UDP), exposing a socket-like API. You write the Application layer, the MQTT client, the CoAP handler, the sensor protocol. The stack is trimmed for RAM (fewer buffers, smaller windows) compared to a desktop OS's stack.


Gotchas


TL;DR

This is just the start

Sign up free to track progress on this lesson, get coding problems with the Run button, and unlock the full interview Q&A.

More in Networking & IoT

Ethernet BasicsIP Addressing
TCP vs UDP
Sockets
MQTT
CoAP