Skip to main content

OSI Model & TCP/IP: Layered Architecture

Easy

Interview Question: "What is the OSI model, and why do we use a layered architecture? What is the Protocol Data Unit (PDU) at each layer, and how does OSI map onto the 4-layer TCP/IP model actually used in practice?"

Imagine a bank sending a secure legal contract to an overseas branch:

  • The lawyers draft the contract, translate it, and encrypt it.
  • The courier stamps a tracking number on the envelope so missing pages can be detected.
  • Dispatch glues on the recipient's address and country code.
  • The driver figures out the actual streets to drive.
  • The truck rolls on the physical road.

If the truck switches from driving to flying, the lawyers don't touch the contract at all — the layer that changed (the road) is completely separate from the layer that didn't (the legal content). That's the whole point of a layered network architecture: each layer only has to do its own job.


Why split it into layers at all?​

  1. You can change one layer without touching the others. Swap Wi-Fi for 5G, and your browser doesn't need a single line changed.
  2. Different vendors can interoperate. Windows, Linux, a Cisco router, and an iPhone all speak the same layered standards.
  3. It makes debugging targeted. If a cable's unplugged (Layer 1), you're not digging through HTTP payloads (Layer 7) to find the problem.

What data actually looks like at each layer​

As data moves down the stack, each layer wraps it in its own header — that's encapsulation. On the way back up, each header gets stripped off in reverse — decapsulation.

Application
HTTP / JSON data
+ TCP header
Transport
Segment
+ IP header
Network
Packet
+ MAC header
Data Link
Frame
bits
Physical
0110100...
LayerWhat it's called hereWhat gets addedProtocols
7. ApplicationDataRequest/response payloadHTTP, DNS, SSH, SMTP
6. PresentationDataFormatting, compression, encryptionTLS/SSL, JPEG
5. SessionDataSession trackingRPC, Sockets
4. TransportSegment (TCP) / Datagram (UDP)Ports, sequence numbersTCP, UDP, QUIC
3. NetworkPacketSource/destination IP, TTLIPv4, IPv6, ICMP
2. Data LinkFrameMAC addresses, checksumEthernet, Wi-Fi, ARP
1. PhysicalBitsRaw voltage / light pulsesCables, radio

Why the real internet uses 4 layers, not 7​

Application
Presentation
Session
Transport
Network
Data Link
Physical
Application
merges 5, 6, 7
Transport
Internet
Network Access
merges 1, 2

OSI was designed by a committee before anyone built it. TCP/IP was built directly into working software (ARPANET, BSD Unix) and won because it shipped. In practice, nobody needed separate Session and Presentation layers — compression and encryption just happen in the application itself now.


Say this in the interview​

"The OSI model is the 7-layer theoretical framework; TCP/IP is the 4-layer model the real internet actually runs on. Going down the stack, data gets wrapped at each layer — Application data becomes a Transport Segment (adds ports), a Network Packet (adds IP addresses), a Data Link Frame (adds MAC addresses), and finally raw Physical bits. TCP/IP won because it was built and shipped, while OSI stayed a committee spec."


Code: watching the layers in action​

import socket
import struct

def demonstrate_socket_encapsulation():
# 1. Application Layer: Construct HTTP Request Data
http_payload = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n"
print(f"[Application Layer] Created HTTP Payload ({len(http_payload)} bytes)")

# 2. Transport Layer: OS socket wraps data into TCP Segments with port numbers
# socket.AF_INET = IPv4 (Network Layer), socket.SOCK_STREAM = TCP (Transport Layer)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 3. Network & Link Layer: Resolves DNS, wraps into IP Packets and Ethernet Frames
target_host = "example.com"
target_port = 80
print(f"[Transport/Network Layer] Connecting to {target_host}:{target_port} via TCP/IP...")

client_socket.connect((target_host, target_port))
client_socket.sendall(http_payload.encode('utf-8'))

# Receive response through the decapsulation pipeline
response = client_socket.recv(512)
print("\n[Application Layer] Received Decapsulated Response:")
print(response.decode('utf-8', errors='ignore').split('\r\n')[0]) # Status line

client_socket.close()

if __name__ == "__main__":
demonstrate_socket_encapsulation()