Skip to content
๐Ÿ–ฅ๏ธThe OSI Model & TCP vs UDP

The OSI Model & TCP vs UDP

beginner Python 14 minOSI modelTCPUDP
What you'll learn
  • OSI model
  • TCP
  • UDP
  • protocols
  • networking layers
  • encapsulation
  • DNS
  • SSH

How the Internet Works - OSI, TCP & UDP ๐ŸŒ

The scenario. Your Wi-Fi says "connected." The website won't load. A beginner says "the internet is broken" and reboots the router. A pro asks one question: which layer is failing? That question is the whole job, and the OSI model is the map that answers it.

๐Ÿง  The mental model: posting a letter

Sending data across a network is like mailing a letter through the postal system. At each step your message gets wrapped in another envelope (this is called encapsulation):

  • You write the letter (your app data).
  • It goes in an envelope with an address (the network layer).
  • The post office puts it in a mail truck (the physical layer).
  • On the other side, each envelope is opened in reverse.

The OSI model just names these 7 wrapping steps.

The 7 OSI layers (top โ†’ bottom)

#LayerIts jobYou'll see
7Applicationwhat the app speaksHTTP, DNS, SSH
6Presentationencoding & encryptionTLS, JPEG
5Sessionkeeping a conversation opensockets
4Transportreliable vs fast deliveryTCP, UDP
3Networkaddressing & routingIP
2Data Linkthe local hopEthernet, MAC
1Physicalactual bits on the wirecables, Wi-Fi

Mnemonic (layer 7 โ†’ 1): All People Seem To Need Data Processing.

Two layer-7 names you will hear daily: DNS (Domain Name System) is the internet's address book: it turns a name such as example.com into the IP address your computer actually connects to. SSH (Secure Shell) gives you an encrypted terminal on a remote server, so you can run commands on it from your laptop. DNS lookups usually travel over fast UDP, SSH sessions over reliable TCP.

In real life you debug three layers: Layer 7 (is my URL/request right?), Layer 4 (is the port open, TCP or UDP?), and Layer 3 (can I even route to that IP?).

Layer 4: TCP vs UDP - the most important comparison in networking

Both deliver your data. They make the opposite trade-off, and this is the interview question.

๐Ÿง  The analogy that makes it stick: TCP is a phone call. You dial, the other side says "hello?" (a handshake), you talk in order, and if they miss a word they say "sorry, repeat that" (lost packets are re-sent). Reliable, but there's overhead. UDP is shouting across a crowded room (or mailing postcards). You just blast the message out - fast, no setup, but some words may get lost and nobody re-sends them.

TCP ๐Ÿ“žUDP ๐Ÿ“ฃ
Reliable?Yes - ordered, re-sends lost dataNo - best-effort
SpeedSlower (handshake + acknowledgements)Fast, tiny overhead
Use forWeb, email, files, databasesVideo/voice calls, gaming, DNS

The decision rule: If losing data is unacceptable โ†’ TCP. If being late is worse than losing a packet โ†’ UDP. (A dropped video frame is fine; a re-sent one arriving a second late ruins the call.)

๐Ÿ”ง See it for real

bash
# Watch the TCP handshake happen when you load a page (the "* Connected" line is TCP completing):
curl -v https://example.com
# Check if a TCP port is open (443 = HTTPS):
nc -zv example.com 443

Your Task

For a few common protocols, print whether each runs over TCP (reliable) or UDP (fast). Then predict before you run: which two are UDP?

Related terms in the glossary