Module 2 · Beginner · How data travels: network models

The OSI model, layer by layer

The OSI model explained layer by layer: physical, data link, network, transport, session, presentation and application, with devices, PDUs and troubleshooting.

By theprogrammers team · · 11 min read

A user says "the internet isn't working". That could mean a loose cable, a Wi-Fi password problem, a wrong IP address, a firewall blocking a port, a DNS server that's down, or a bug in the website itself. Network engineers sort through these quickly by asking "which layer is broken?" The map they use is the OSI model: seven layers, each with one job, stacked from the physical cable up to the app on your screen.

The OSI (Open Systems Interconnection) model was published in 1984 as a standard way to describe networks. The internet doesn't follow it exactly (it uses the simpler TCP/IP model), but OSI's layer numbers became the common language of networking: people talk about "layer 2 switches", "layer 4 load balancers" and "layer 7 firewalls" every day. This lesson explains the OSI model layer by layer, what each layer does, which protocols and devices belong to it, and how to use it to troubleshoot.

What you need first: what is a computer network? and networking terms every beginner should know.

Key terms

  • Layer: one level of a network model, with one job, that uses the layer below and serves the layer above.
  • PDU (protocol data unit): the name for a chunk of data at a particular layer: bits, frames, packets, segments, data.
  • Header: control information a layer adds in front of the data, such as addresses or port numbers.
  • Encapsulation: each layer wrapping the data from the layer above in its own header on the way down.
  • Peer layers: the same layer on two different devices, which "talk" to each other using that layer's protocol.

Why layers?

Sending a WhatsApp photo involves electrical signals, radio, addressing, routing across many networks, splitting into packets, reliability, encryption and the app's own format. No one could design all of that as one piece. Layers split the problem:

  • Each layer has one job and only needs to understand the layers next to it.
  • Layers can change independently. Your browser doesn't care whether you're on Wi-Fi, 5G or a fibre cable; that's a lower layer's problem.
  • Different companies can build different layers and still work together, because the boundaries are standard.

It's like sending a parcel through a courier: you pack it and write an address; the courier company decides the route; the van driver only knows the next stop. Nobody needs to know everyone else's job.

The seven layers

Layers are numbered from the bottom. A common way to remember them, from layer 1 up: "Please Do Not Throw Sausage Pizza Away" (Physical, Data link, Network, Transport, Session, Presentation, Application).

#LayerJobPDUExamplesDevices
7ApplicationNetwork services for appsDataHTTP, DNS, SMTP, SSH, FTPLayer 7 load balancers, web app firewalls
6PresentationFormat, encrypt, compressDataTLS encryption, character encoding, JPEG, JSON
5SessionStart, manage and end conversationsDataSessions, reconnecting, RPC
4TransportDeliver data between programs, reliably or quicklySegment (TCP) / datagram (UDP)TCP, UDP, portsLayer 4 load balancers, firewalls
3NetworkAddress and route packets across networksPacketIP, ICMP, routing protocolsRouters, layer 3 switches
2Data linkDeliver frames between devices on one linkFrameEthernet, Wi-Fi, MAC addresses, ARP, VLANsSwitches, access points, NICs
1PhysicalSend raw bits as signalsBitCables, fibre, radio, voltages, connectorsCables, hubs, repeaters, modems

Layer 1: Physical

Moves bits as physical signals: voltages on copper, pulses of light in fibre, radio waves for Wi-Fi and mobile. It defines cables, connectors (like the RJ45 plug), frequencies and speeds. It knows nothing about addresses or meaning, only 0s and 1s. Problems here: an unplugged or damaged cable, a weak Wi-Fi signal, a dead port.

Gets data from one device to the next device on the same link or LAN. It groups bits into frames, adds the sender's and receiver's MAC addresses, and checks for damaged frames with a checksum. Switches work here: they read MAC addresses and send each frame out of the right port. Ethernet and Wi-Fi are layer 2 (and layer 1) standards. Problems here: wrong VLAN, switch loops, Wi-Fi authentication failures.

Layer 3: Network

Gets packets from the source device to the destination device across many networks. It adds IP addresses and lets routers choose the path, hop by hop. Layer 2 moves data one hop; layer 3 plans the whole journey. ICMP (used by ping) is here too. Problems here: wrong IP address, wrong subnet mask, missing default gateway, routing errors.

Layer 4: Transport

Gets data to the right program on the destination, using port numbers, and decides how carefully to deliver it:

  • TCP: sets up a connection, numbers every segment, resends lost ones, delivers in order.
  • UDP: just sends; faster, with no guarantees.

Problems here: a firewall blocking a port, nothing listening on the port ("connection refused"), timeouts.

Layer 5: Session

Manages the conversation between two programs: starting it, keeping it going, and ending it, possibly with checkpoints so a long transfer can resume. In practice, the internet mostly handles this inside applications and TCP, so it's the least distinct layer.

Layer 6: Presentation

Makes sure data arrives in a form the receiving app understands: encoding (such as UTF-8 text), compression and encryption. TLS encryption is often placed here, though on the real internet it sits between TCP and the application.

Layer 7: Application

The protocols that applications use directly: HTTP for the web, DNS for names, SMTP and IMAP for email, SSH for remote login. Note that the application layer is the protocol, not the app itself: Chrome isn't layer 7, but the HTTP it speaks is. Problems here: a website returning 500 errors, a DNS name that doesn't exist, a wrong API key.

Going down and up the stack

When you send data, it goes down the layers on your device: each layer adds its own header (encapsulation). At the other end, it goes up: each layer reads and removes its header. Each layer on one device effectively talks to the same layer on the other device:

 Your laptop                                  Web server
 7 Application  HTTP request   <---------->   7 Application
 4 Transport    + TCP header   <---------->   4 Transport
 3 Network      + IP header    <---------->   3 Network
 2 Data link    + Ethernet hdr <-->[switch]   2 Data link
 1 Physical     bits on wire   <-->[router]   1 Physical
         \___________ down          up ___________/

Devices in the middle only go as high as their job needs: a switch looks up to layer 2, a router up to layer 3. Neither reads your HTTP request. Packets, frames and encapsulation follows one request through every header.

A small Python model of which layer each familiar thing belongs to:

OSI = {
    7: ("Application", ["HTTP", "DNS", "SSH", "SMTP"]),
    6: ("Presentation", ["TLS", "UTF-8", "JPEG"]),
    5: ("Session", ["session setup", "RPC"]),
    4: ("Transport", ["TCP", "UDP", "ports"]),
    3: ("Network", ["IP", "ICMP", "routers"]),
    2: ("Data link", ["Ethernet", "Wi-Fi", "MAC", "switches"]),
    1: ("Physical", ["cables", "fibre", "radio"]),
}

def layer_of(thing):
    for number, (name, examples) in OSI.items():
        if thing in examples:
            return f"{thing}: layer {number} ({name})"
    return f"{thing}: not in the table"

for thing in ["DNS", "routers", "MAC", "UDP", "fibre"]:
    print(layer_of(thing))
DNS: layer 7 (Application)
routers: layer 3 (Network)
MAC: layer 2 (Data link)
UDP: layer 4 (Transport)
fibre: layer 1 (Physical)

Troubleshooting by layer

The real value of the model: check layers in order and you stop guessing.

CheckLayerCommand or actionIf it fails
Is the cable in, Wi-Fi connected, link light on?1Look; check Wi-Fi statusReconnect, swap cable, move closer
Do I have a valid IP address and gateway?3 (and 2)ipconfig (Windows), ip addr (Linux), ifconfig (Mac)DHCP problem; reconnect or check router
Can I reach my router?3ping your default gatewayLocal network problem
Can I reach the internet by IP?3ping 8.8.8.8Router or ISP problem
Do names resolve?7nslookup example.comDNS problem; try another DNS server
Is the port open?4curl -v https://example.com, Test-NetConnection on WindowsFirewall, or service down
Does the app respond correctly?7Browser DevTools, HTTP status codeApplication bug or server error

"Can ping 8.8.8.8 but websites don't open" points straight at DNS (layer 7), not your Wi-Fi. That's the kind of shortcut the model gives you. The tools module of this course covers each command.

Where OSI is right and where it isn't

  • Right: layers 1 to 4 match real networks very well, and their numbers are used everywhere.
  • Not quite: layers 5, 6 and 7 are blurred together on the internet. HTTP, TLS and your app's data format all live in what TCP/IP calls one application layer.
  • The OSI protocols themselves (a full protocol suite designed alongside the model) lost to TCP/IP and are rarely used. The model survived as a teaching and troubleshooting tool.

Practice

  1. Put these in order from layer 1 to layer 7: Transport, Physical, Application, Network, Session, Data link, Presentation.
Show the answer

Physical (1), Data link (2), Network (3), Transport (4), Session (5), Presentation (6), Application (7).

  1. Which layer does each belong to: a switch reading MAC addresses; a router choosing a path; TCP resending a lost segment; a fibre cable?
Show the answer

Layer 2 (data link), layer 3 (network), layer 4 (transport), layer 1 (physical).

  1. You can ping 8.8.8.8 successfully, but ping google.com says "could not find host". Which layer is the problem?
Show the answer

Layer 7, DNS. Reaching an IP address proves layers 1 to 3 work; failing to turn a name into an address is a DNS problem.

  1. Add "VLAN" and "QUIC" to the Python OSI dictionary at the right layers, and look them up.
Show the answer

VLANs are layer 2; QUIC is a transport protocol, layer 4.

OSI = {4: ("Transport", ["TCP", "UDP", "QUIC"]), 2: ("Data link", ["Ethernet", "VLAN"])}
for thing in ["VLAN", "QUIC"]:
    for number, (name, examples) in OSI.items():
        if thing in examples:
            print(thing, "layer", number, name)
VLAN layer 2 Data link
QUIC layer 4 Transport
  1. What's the difference between a layer 4 and a layer 7 load balancer?
Show the answer

A layer 4 load balancer decides using IP addresses and ports only, without reading the content. A layer 7 load balancer reads the application data, such as the HTTP path or headers, so it can send /api requests to one group of servers and /images to another.

Interview questions

Explain the OSI model.

A seven-layer reference model for network communication: physical (signals), data link (frames between adjacent nodes, MAC addresses), network (packets across networks, IP), transport (end-to-end delivery between processes, TCP/UDP), session (dialogue control), presentation (data format, encryption) and application (protocols used by apps such as HTTP and DNS). Each layer serves the one above and uses the one below.

At which layers do switches and routers operate?

Traditional switches operate at layer 2, forwarding frames by MAC address. Routers operate at layer 3, forwarding packets by IP address between networks. Layer 3 switches combine both, routing between VLANs in hardware.

What is the PDU at each layer?

Bits at layer 1, frames at layer 2, packets at layer 3, segments (TCP) or datagrams (UDP) at layer 4, and data or messages at layers 5 to 7.

Key takeaways

  • The OSI model splits networking into seven layers, each with one job, from physical signals (1) to application protocols (7).
  • Layer 2 moves frames between neighbours using MAC addresses (switches); layer 3 routes packets across networks using IP (routers); layer 4 delivers to programs using ports (TCP, UDP).
  • Data goes down the layers on the sender (encapsulation) and up on the receiver.
  • Troubleshoot by checking layers from the bottom up; the first failing layer is usually the problem.
  • The internet uses TCP/IP, but OSI's layer numbers are the shared vocabulary of networking.

Frequently asked questions

Is the OSI model still used?

As a model and a vocabulary, yes, everywhere: "layer 2", "layer 4 load balancer" and "layer 7 firewall" all come from it. As an actual set of protocols, no; the internet runs on TCP/IP.

Which OSI layer is HTTPS?

HTTP is layer 7 (application). The TLS encryption that makes it HTTPS is usually placed at layer 6 (presentation), though in real implementations it sits between TCP and the application.

What is the easiest way to remember the OSI layers?

A sentence whose words start with the layers' letters. From layer 1 up: "Please Do Not Throw Sausage Pizza Away". From layer 7 down: "All People Seem To Need Data Processing".

What to learn next

  1. See the model the internet actually uses in the TCP/IP model and how it maps to OSI.
  2. Follow data through every layer in packets, frames and encapsulation.
  3. Revise the vocabulary in networking terms every beginner should know.