The OSI Model & TCP vs UDP
- OSI model
- TCP
- UDP
- protocols
- networking layers
- encapsulation
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)
| # | Layer | Its job | You'll see |
|---|---|---|---|
| 7 | Application | what the app speaks | HTTP, DNS, SSH |
| 6 | Presentation | encoding & encryption | TLS, JPEG |
| 5 | Session | keeping a conversation open | sockets |
| 4 | Transport | reliable vs fast delivery | TCP, UDP |
| 3 | Network | addressing & routing | IP |
| 2 | Data Link | the local hop | Ethernet, MAC |
| 1 | Physical | actual bits on the wire | cables, Wi-Fi |
Mnemonic (layer 7 โ 1): All People Seem To Need Data Processing.
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 data | No - best-effort |
| Speed | Slower (handshake + acknowledgements) | Fast, tiny overhead |
| Use for | Web, email, files, databases | Video/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
# Watch the TCP handshake happen when you load a page:
curl -v https://example.com # the "* Connected" line is TCP completing
# Check if a TCP port is open:
nc -zv example.com 443 # 443 = HTTPS
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?