Have you ever wondered how your messages, emails, or files travel seamlessly across the internet? Behind the scenes, the OSI (Open Systems Interconnection) model plays a crucial role, breaking down the complexities of network communication into manageable layers.
But why do we need such a communication model? Imagine if every application had to understand the specifics of its network medium—whether it’s Wi-Fi, Ethernet, LTE, or fiber. This would mean building separate versions of apps for each type of connection, an impractical and resource-intensive task.
The OSI model solves this problem by providing a standardized framework that abstracts the underlying network complexities. Applications can remain agnostic to the medium they operate on, allowing developers to focus on functionality rather than connectivity. Now we understand the need let’s dive deep.
OSI Model
The OSI stands for Open System Interconnect. It is a conceptual framework that standardizes the transfer of data on network into seven distinct layers. It was developed by the International Organization for Standardization (ISO).
It has 7 layer and each layer describe a specific networking component:-
Layer 7 - Application - HTTP/FTP/gRPC
Layer 6 - Presentation - Encoding, Serialization
Layer 5 - Session - Connection establishment, TLS
Layer 4 - Transport - UDP/TCP
Layer 3 - Network - IP
Layer 2 - Data Link - Frames, MAC Address, Ethernet
Layer 1 - Physical - Electric signals, Fiber or Radio Waves
To understand how each layer work we take a example of sending a POST request to an HTTPS webpage.
Sender Side(Client)
Layer 7 - Application
- POST request with JSON data to HTTPS server
Layer 6 - Presentation
- Serialize JSON to flat byte Strings
Layer 5 - Session
- Request to establish TCP connection/TLS
Layer 4 - Transport
- Sends SYN request target PORT 443
Layer 3 - Network
- SYN is placed an IP packet(s) and adds the source/dest. IPs
Layer 2 - Data Link
- Each packet goes into a single frame and adds the source/dest. MAC addresses
Layer 1 - Physical
- Each frame becomes string of bits which converted into either a radio signal (WIFI), electrical signal (ethernet), or light(fiber)
Receiver Side(Server)
Layer 1 - Physical
- Radio, electric or light is received and converted into digital bits
Layer 2 - Data Link
- The bits from layer 1 is assembled into frames
Layer 3 - Network
- The frames from layer 2 is assembled into IP packet
Layer 4 - Transport
The IP packets from layer 3 are assembled into TCP segment
Deals with the congestion control/flow control/retransmission in case of TCP
If segment is SYN we don’t need to go further into more layers as we are still processing the connection request
Layer 5 - Session
The connection session is established or identified
We only arrive at this layer when necessary (3 way handshake is done)
Layer 6 - Presentation
- Deserialize flat byte strings back to JSON for the app to consume
Layer 7 - Application
- Application understand the JSON POST request and your server receive event is triggered
Understanding the OSI model is crucial as it helps identify the specific layer where our applications and devices operate. For example, switches operate at Layer 2, while routers function at Layer 3, which is why switches typically perform faster than routers.
Shortcomings of the OSI Model
OSI Model has too many layers which can be hard to comprehend
Hard to argue about which layer does what
Simpler to deal with Layers 5-6-7 as just one layer, application
TCP/IP Model does just that
TCP/IP Model
A simpler alternative to the OSI model consists of just 4 layers:
Application Layer: Combines Layers 5, 6, and 7 of the OSI model.
Transport Layer: Corresponds to Layer 4 in the OSI model.
Internet Layer: Maps to Layer 3 in the OSI model.
Network Interface Layer: Covers Layer 2 of the OSI model.
The Physical Layer, while part of the OSI model, is not officially included in this simplified model.
TCP (Transmission Control Protocol)
Transmission Control Protocol (TCP) is the backbone of reliable communication on the internet. It ensures data is delivered accurately and in the correct order, making it essential for applications like web browsing, file transfers, and emails.
The communication through TCP work mainly in three stages:-
Setting Up the Connection (3-Way Handshake)
Transferring Data
Ending the Connection (4-Way Handshake)
Understanding each step is crucial, and we can better grasp it by using the analogy of sending a book.
1. Setting Up the Connection (3-Way Handshake)
Before data can be transferred, TCP establishes a connection between the sender and the receiver. This process, called the 3-way handshake, ensures both parties are ready to communicate:
SYN: The sender initiates the connection by sending a message: “Hey, can I send you a book?”.
SYN-ACK: The receiver acknowledges with: “Sure, I’m ready to receive it!”.
ACK: The sender confirms: “Great, let’s begin!”.
This handshake ensures both devices are synchronized and agree on initial sequence numbers, laying the groundwork for reliable data transfer.
2. Transferring Data
With the connection established, TCP handles the transmission of data as follows:
Packetization: The data (e.g., book) is split into smaller packets (chapters), each labeled with a unique sequence number to ensure they’re received in the correct order.
Acknowledgments: For every packet sent, the receiver sends an acknowledgment (ACK) confirming its receipt. For example, after receiving Chapter 1, the receiver might respond with: “Got Chapter 1!” (ACK).
Handling Lost Packets: If a packet (e.g., Chapter 2) is lost in transit, the sender detects this when it doesn’t receive an acknowledgment. The sender will then resend the lost packet until the receiver confirms it has arrived.
3. Ending the Connection (4-Way Handshake)
Once all the data has been transferred, TCP ensures a graceful termination of the connection through a 4-way handshake:
The sender signals: “That’s everything I wanted to send” (FIN).
The receiver responds: “Got your message, and I’m done receiving” (ACK).
The receiver then sends its own FIN: “I’m also done sending”.
The sender confirms with a final ACK: “Perfect, goodbye!”.
This orderly termination ensures no data is left in transit and both parties can close the connection cleanly.
Use case of TCP
Reliable Communication
Remote Shell
Database Connections
Web communications
Any bidirectional communication
UDP (User Datagram Protocol)
UDP is a connectionless protocol that operates at the transport layer of the OSI model. Unlike TCP, it doesn’t establish a connection before sending data, nor does it ensure that data packets arrive in order or even at all. This lightweight approach eliminates the overhead of error checking and retransmission, making UDP faster but less reliable.
How UDP Works
UDP functions by wrapping data in a simple packet structure and sending it to the destination without waiting for the acknowledgment. It is done in following order:
Data Encapsulation: As data reaches the transport layer UDP adds minimal header consisting Source Port, Destination Port, Length, Checksum(optional).
Packet Transmission: Packet is sent to the network layer for further transmission. Here no connection setup or handshakes required which reduces latency.
Receiving: The receiver process the incoming packet and forward payload to destination port. No acknowledgment or error correction is performed.
Use case of UDP
Video Streaming
VPN
DNS
WebRTC
TCP v/s UDP
Feature | TCP | UDP |
Connection Type | Connection-oriented (requires handshake) | Connectionless (no handshake) |
Reliability | Very reliable, resends lost data | Not reliable, no retransmission |
Data Order | Ensures data arrives in order | No guarantee of data order |
Error Checking | Error detection and correction | Error detection (no correction) |
Speed | Slower (due to error checking, retransmission) | Faster (no overhead) |
Reliability Mechanism | Retransmission, acknowledgment, sequencing | No retransmission, no acknowledgment |
Connection Setup | Three-way handshake (SYN, SYN-ACK, ACK) | No setup (data can be sent immediately) |
Use Case | File transfer, web browsing, email, etc. | Streaming, online gaming, VoIP, etc. |
When to use which?
Deciding which one is use when is important for this ask yourself what important here speed or accuracy if your answer is speed go with UDP and if your answer is accuracy go with TCP.
we can understand it further by discussing the cases:
CASE 1 : Live Telecast of Cricket - In cricket streaming we might think as it real-time UDP is used but while minimal delay is important, ensuring the content reaches viewers without errors is a priority. Therefore, TCP is often preferred, as it guarantees reliable transmission, error correction, and the accurate delivery of video and audio, even if it means handling slight delays to maintain stream integrity.
CASE 2 : Video Calling with Friends - Video calls require real-time communication, but they also need to prioritize low latency. Losing a few frames of video or audio during a call won’t significantly impact the experience, but a delay could be disruptive. Hence, UDP is used to ensure smooth and fast communication.
CASE 3 : YouTube Live – YouTube Live broadcasts, on the other hand, focus on delivering the stream with accuracy and consistency, as users expect a clear and uninterrupted experience. Since video content is being streamed in real-time, but the need for accurate delivery is critical, TCP is often used to avoid errors and ensure a high-quality stream.
Conclusion
In short, TCP is perfect for tasks that need reliable, ordered data delivery, like file transfers and emails. On the other hand, UDP is ideal when speed matters more than accuracy, such as in gaming or live streaming. So, it all comes down to whether you prioritize reliability (TCP) or speed (UDP) for your application.