In this article, we'll cover the basics of network protocols, the client-server model, and the peer-to-peer (P2P) model, along with their practical use cases. For instance:
If you're building something like WhatsApp, which protocol should you use?
For a search engine like Google, what works best?
If you want to enable video calls or live-streaming, which protocol should you choose?
By the end, you'll understand the core differences and know when to use protocols like WebSockets, WebRTC, HTTP, TCP, UDP, FTP, and SMTP.
What is a Network Protocol?
A network protocol is a set of rules that allows two systems to communicate with each other. Think of it as a "common language" for devices—without a shared protocol, communication would be impossible.
For example, when you send a message online, the sender and receiver must follow the same protocol to ensure the message is coded and decoded correctly.
Layers of Network Protocols
The OSI model categorizes network protocols into seven layers:
Application Layer
Presentation Layer
Session Layer
Transport Layer
Network Layer
Data Link Layer
Physical Layer
However, for high-level system design, we focus on the two most important layers:
Application Layer: Responsible for user-facing protocols like HTTP, FTP, SMTP, and WebRTC.
Transport Layer: Handles data transmission using protocols like TCP and UDP.
Application Layer
The application layer is further divided into two categories based on communication models:
Client-Server Protocols: HTTP, FTP, SMTP, WebSockets
Peer-to-Peer Protocols: WebRTC
Client-Server Model
In the client-server model, a client sends requests, and the server responds. This is a one-way communication initiated by the client. Common protocols include:
HTTP:
Connection-oriented protocol.
Maintains a single connection for accessing web pages using hypertext.
FTP:
Uses two connections:
Control connection (always active).
Data connection (connects/disconnects as needed).
SMTP:
Used for sending emails.
Works with either:
POP: Downloads emails and deletes them (rarely used now).
IMAP: Allows access to emails on multiple devices without deletion.
WebSockets
WebSockets differ from traditional client-server protocols because they enable bidirectional communication. Both the client and the server can initiate communication.
For example:
Client 1 ↔ Server ↔ Client 2: The server facilitates communication between clients, but the clients cannot directly talk to each other, that makes it client server not peer to peer.
This makes WebSockets ideal for messaging apps like WhatsApp and Telegram, where the server informs the client of new messages in real-time. Without WebSockets, the client would need to repeatedly ask the server if new messages are available, which is inefficient.
Peer-to-Peer (P2P) Model
In the peer-to-peer model, clients can communicate directly with each other without always routing through a server.
WebRTC:
WebRTC is a true peer-to-peer protocol.
Clients can exchange data directly, making it faster and more efficient.
Common use cases include video calls and live-streaming, where latency is critical.
read about webRTC in detail here.
Transport Layer
The transport layer ensures the reliable delivery of data and uses two key protocols:
TCP (Transmission Control Protocol):
Maintains a virtual connection.
Data is split into packets and sent in sequence.
If a packet is lost, an acknowledgment is sent, and the packet is retransmitted.
Used when reliability and sequencing are crucial (e.g., loading a web page).
UDP (User Datagram Protocol):
No connection is maintained.
Packets are sent independently and may arrive out of order.
No acknowledgments are sent for lost packets, making it faster.
Ideal for use cases like live-streaming or video calls, where minor data loss (e.g., a missing video frame) is acceptable.
Example Use Cases:
Google Meet and Zoom use UDP for video calls, prioritizing speed over perfect data accuracy.
WebRTC also relies on UDP for fast, low-latency communication.
When to Use What?
For messaging apps like WhatsApp: Use WebSockets for real-time, bidirectional communication.
For live-streaming or video calls: Use WebRTC (based on UDP) for direct, fast communication between clients.
For standard web browsing: Use HTTP (or HTTPS) for reliable, one-way communication.
For file transfers: Use FTP, though modern systems prefer encrypted alternatives.
For email services: Use SMTP with IMAP for efficient email management.
Conclusion
By now, we’ve covered the basics of network protocols, the client-server model, and the peer-to-peer model. We've also explored how different protocols like WebSockets, WebRTC, HTTP, TCP, and UDP fit into practical use cases.
Knowing when to use a particular protocol is fundamental to system design. For example:
WebSockets for messaging apps.
WebRTC for live-streaming or video calls.
HTTP for standard web applications.
These are the foundational concepts that will guide us as we dive deeper into system design.