The OSI model splits network communication into seven layers: physical, data link, network, transport, session, presentation and application. Each layer adds its own header to data from the layer above, and the receiver removes them in reverse. It is a teaching model; the internet actually runs on the four-layer TCP/IP stack.
On this page
The 7 layers of the OSI modelOSI vs TCP/IP: how the two models mapWhat happens when you type a URL: one request through the layersA socket echo demo on localhostMistakes people make with the OSI modelFrequently asked questionsMost OSI pages stop at a list of seven names and a mnemonic. This one is for readers who need to use the model: place a protocol on a layer, say what the data unit is called, and trace one real request through the stack, which is the follow-up interviewers ask after you recite the layers. The socket demo was executed on 1 December 2026 with Python 3.9.6.
It sits between process vs thread, which covers the program on each end, and TCP vs UDP and how HTTPS works, which zoom into two of the layers. For question practice see computer networks interview questions.
The 7 layers of the OSI model
OSI stands for Open Systems Interconnection. Read the layers from the top (closest to your program) to the bottom (closest to the wire). The data unit at each layer is its protocol data unit (PDU).
| # | Layer | Job | PDU | Examples |
|---|---|---|---|---|
| 7 | Application | Services an application talks to the network through | Data | HTTP, DNS, SMTP, SSH |
| 6 | Presentation | Data format, encoding, compression, encryption | Data | TLS is often placed here, JPEG, character encodings |
| 5 | Session | Opening, keeping and closing a dialogue | Data | Session handling in RPC, NetBIOS (rarely separate in practice) |
| 4 | Transport | Process-to-process delivery by port number; reliability if chosen | Segment (TCP) or datagram (UDP) | TCP, UDP, QUIC (over UDP) |
| 3 | Network | Host-to-host delivery across networks: addressing and routing | Packet | IP (IPv4, IPv6), ICMP, routers |
| 2 | Data link | Delivery between neighbouring devices on one link; MAC addresses | Frame | Ethernet, Wi-Fi (802.11), ARP, switches |
| 1 | Physical | Signals on the medium | Bits | Cables, radio, fibre, hubs |
TLS does not fit neatly. It runs on top of TCP and below HTTP, so textbooks place it at layer 6 or 5, while the TCP/IP view simply calls it part of the application layer. If an interviewer asks, say that and move on; both answers are accepted when explained.
A memory aid for the order from the bottom is Please Do Not Throw Sausage Pizza Away. The useful shortcut is more practical: a switch works at layer 2, a router at layer 3, a load balancer is called layer 4 or layer 7 depending on whether it looks at ports or at HTTP content.
OSI vs TCP/IP: how the two models map
The model the internet actually uses is the TCP/IP model, described in RFC 1122 as four layers: link, internet, transport and application. OSI was designed as a reference model and is used for teaching and for the vocabulary ("layer 7 load balancer"). The two align like this:
| TCP/IP layer | OSI layers covered | Example |
|---|---|---|
| Application | Application, presentation, session (5 to 7) | HTTP, DNS, TLS handled by the application |
| Transport | Transport (4) | TCP, UDP |
| Internet | Network (3) | IP, ICMP |
| Link | Data link and physical (1 and 2) | Ethernet, Wi-Fi |
- OSI: 7 layers, defined before most protocols existed, strict separation. TCP/IP: 4 layers, protocols came first and the model describes them.
- Some books use a five-layer hybrid that splits TCP/IP's link layer into data link and physical. Ask which one your exam follows.
- Real protocols often cross layers (ARP is described at the boundary of layers 2 and 3), so do not force every protocol into exactly one box.
What happens when you type a URL: one request through the layers
Take https://example.com/ typed into a browser on a laptop on home Wi-Fi. Each step below names the layer it belongs to. The sequence is the same in outline for any HTTPS site; details such as which DNS transport is used or whether HTTP/3 replaces HTTP/2 depend on the browser and the site.
- Application: the browser parses the URL into scheme (https), host (example.com), port (443 by default) and path. It checks its cache and its HSTS list, which can force https.
- Application (DNS): the host name must become an IP address. The browser or operating system asks a DNS resolver, usually the router or your provider's resolver. DNS queries normally go over UDP port 53 and can fall back to TCP; some browsers use DNS over HTTPS instead.
- Network and data link: the packet to the resolver is addressed to a destination IP. On the local network, ARP finds the MAC address of the next hop, the home router, and a frame is sent to that MAC.
- Transport (TCP): with the IP in hand, the browser opens a TCP connection to port 443 with the three-way handshake, covered in TCP vs UDP. If the site supports HTTP/3, the browser may use QUIC over UDP instead.
- Presentation or session (TLS): a TLS handshake agrees on keys and checks the server certificate for the host name. Everything after this is encrypted; see how HTTPS works.
- Application (HTTP): the browser sends a request such as
GET / HTTP/1.1with a Host header, inside the encrypted channel. The server replies with a status code, headers and HTML. - Down and across the network: at each hop a router strips the old frame header, reads the destination IP, picks the next hop and wraps the packet in a new frame. The IP addresses stay end to end, apart from your home router's network address translation (NAT), which rewrites your private address to the public one. The MAC addresses change at every hop.
- Back up the stack: the server's response travels the same way in reverse, TCP puts the bytes back in order, TLS decrypts them, and the browser renders the page.
Encapsulation of one HTTP request
| Layer | What is added | Contains |
|---|---|---|
| Application | HTTP request line and headers | GET / HTTP/1.1, Host: example.com |
| Transport | TCP header (ports, sequence numbers, flags) | Source port (random high), destination port 443 |
| Network | IP header (source and destination IP, TTL) | Your IP, the server's IP |
| Data link | Ethernet or Wi-Fi header and trailer (source and destination MAC) | Your MAC, the router's MAC |
| Physical | Nothing is added; the frame is sent as signals | Radio waves or electrical pulses |
In real life TLS wraps the HTTP bytes before TCP sees them, so the HTTP row is encrypted application data inside a TLS record. A network observer on your Wi-Fi sees the IP addresses, ports and (usually) the server name in the TLS handshake, but not the path or the page.
A socket echo demo on localhost
A socket is the programming interface to the transport layer. This program starts a TCP server on a free port on 127.0.0.1, connects a client, sends text and prints the reply. The server's accept() returns only after the three-way handshake has completed inside the kernel; your code never sees the SYN packets. Everything below layer 4 (IP, Ethernet, Wi-Fi) is handled by the operating system, which is the layering idea made concrete.
import socket, threading
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.bind(("127.0.0.1", 0)) # port 0: let the OS pick a free port
srv.listen()
port = srv.getsockname()[1]
def serve_once():
conn, addr = srv.accept() # returns after the three-way handshake is done
with conn:
data = conn.recv(1024)
conn.sendall(data.upper()) # "echo", shouted so we can see the server touched it
t = threading.Thread(target=serve_once); t.start()
with socket.create_connection(("127.0.0.1", port)) as c:
print("connected from port", c.getsockname()[1], "to server port", port)
c.sendall(b"hello osi")
print("reply:", c.recv(1024))
t.join(); srv.close()Output (the port numbers differ on every run):
connected from port 49399 to server port 49398
reply: b'HELLO OSI'Because the traffic stays on the loopback interface, no physical layer is involved. To see the layers for real, run Wireshark or tcpdump on a capture of your own traffic and expand the Ethernet, IP and TCP headers of one packet. Only capture on networks you own or have permission to monitor; the cybersecurity path covers this properly.
Mistakes people make with the OSI model
- Treating it as how the internet is built. The internet is TCP/IP; OSI is the vocabulary.
- Saying 'packet' for everything. Use segment, packet and frame for layers 4, 3 and 2.
- Calling a switch a layer 3 device. Plain switches are layer 2; layer 3 switches exist and route.
- Placing HTTPS at one fixed layer without explaining that TLS sits between transport and application.
Once the layers are clear, the backend developer roadmap shows where they appear in everyday work: ports, DNS, timeouts, reverse proxies and load balancers.
Frequently asked questions
What are the 7 layers of the OSI model?
From top to bottom: application, presentation, session, transport, network, data link and physical. Their data units are data, data, data, segment or datagram, packet, frame and bits. Layers 5 to 7 are folded into one application layer in the TCP/IP model.
What is the difference between the OSI model and the TCP/IP model?
OSI is a seven-layer reference model used for teaching and vocabulary. TCP/IP is a four-layer model (link, internet, transport, application) that describes the protocols the internet actually runs. OSI's session, presentation and application layers map to TCP/IP's single application layer.
What happens when you type a URL in the browser?
The browser parses the URL, resolves the host name to an IP with DNS, opens a TCP connection (or QUIC) to the server, performs a TLS handshake for https, sends an HTTP request, receives the response and renders it. Routers forward the packets between each step.
At which layer do routers and switches work?
A conventional switch works at layer 2, the data link layer, using MAC addresses. A router works at layer 3, the network layer, using IP addresses. Layer 3 switches combine both, and load balancers are described as layer 4 or layer 7 depending on what they inspect.
What is a PDU in networking?
A protocol data unit is the name for the data at a given layer: data at the application layers, segment (TCP) or datagram (UDP) at transport, packet at network, frame at data link, and bits at physical. Each layer wraps the PDU from above with its own header.
Is the OSI model still used?
Yes, as a way to talk about problems and devices, such as layer 2 switching or a layer 7 load balancer. It is not how the internet is implemented, which follows TCP/IP, so exam answers should say which model they use.
Learn this with mentors
Programmes for builders and engineers with admissions open: live classes, mentors and project work.


