Transmission Control Protocol (TCP)
TCP Principles and Mechanisms
Key features:
- Connection Establishment: Three-way handshake (SYN, SYN-ACK, ACK)
- Reliable Delivery: Acknowledgment and retransmission
- Flow Control: Sliding window mechanism
- Congestion Control: Slow start, congestion avoidance, fast retransmit, and fast recovery
- Ordered Data Transfer: Sequence numbers
- Error Detection: Checksum
Example of TCP header:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
TCP Performance Characteristics
Throughput:
- Affected by Round-Trip Time (RTT) and packet loss
- Theoretical max: Window Size / RTT
Latency:
- Connection setup time: 1.5 RTT (three-way handshake)
- Data transfer: At least 1 RTT per request-response cycle
Reliability:
- Guaranteed delivery through acknowledgments and retransmissions
- Ordered delivery ensures data integrity
Scalability:
- Limited by connection state maintenance on servers
- C10K problem: difficulty in handling 10,000+ concurrent connections
TCP Use Cases and Limitations
Use Cases:
- Web browsing (HTTP)
- Email (SMTP, IMAP, POP3)
- File transfers (FTP, SFTP)
- Remote administration (SSH)
Limitations:
- Head-of-line blocking in multiplexed scenarios
- Performance degradation in high-latency networks
- Overhead for small, frequent transmissions
User Datagram Protocol (UDP)
UDP Core Concepts
Key features:
- Connectionless: No handshake required
- Unreliable: No guarantee of delivery, ordering, or duplicate protection
- Lightweight: Minimal protocol overhead
- Stateless: No connection state tracking
UDP header structure:
0 7 8 15 16 23 24 31
+--------+--------+--------+--------+
| Source | Destination |
| Port | Port |
+--------+--------+--------+--------+
| | |
| Length | Checksum |
+--------+--------+--------+--------+
|
| data octets ...
+---------------- ...
UDP Performance Analysis
Throughput:
- Higher potential throughput than TCP due to less overhead
- Not limited by congestion control mechanisms
Latency:
- Lower latency than TCP for initial data transfer (no handshake)
- Consistent latency due to lack of retransmission delays
Packet Loss:
- No built-in recovery from packet loss
- Application must implement its own reliability if needed
Scalability:
- Excellent for broadcast and multicast scenarios
- Efficient for large numbers of small transactions
UDP Applications and Constraints
Applications:
- Real-time gaming
- Voice over IP (VoIP)
- Streaming media
- DNS lookups
Constraints:
- Lack of built-in reliability mechanisms
- No congestion control (potential for network flooding)
- Limited message size (65,507 bytes maximum)
Hypertext Transfer Protocol (HTTP)
WebSocket Protocol
WebSocket Full-Duplex Communication
WebSocket provides a persistent, full-duplex communication channel over a single TCP connection.
Key features:
- Bi-directional: Both client and server can send messages
- Low-latency: Reduced overhead after initial handshake
- Real-time: Immediate message delivery
WebSocket Performance Metrics
Connection Overhead:
- Initial handshake: Similar to HTTP
- Subsequent messages: Minimal frame overhead (2-14 bytes)
Latency:
- Low latency for real-time updates
- No need for polling or long-polling
Scalability:
- Efficient for large numbers of concurrent connections
- Challenges with very high connection counts (C10K problem)
Bandwidth Usage:
- Reduced compared to polling techniques
- Efficient for small, frequent updates
WebSocket Use Cases and Limitations
Use Cases:
- Real-time collaborative applications
- Live sports updates
- Financial trading platforms
- Multiplayer games
Limitations:
- Not supported in older browsers
- Potential for abuse (e.g., bypassing same-origin policy)
- Challenges with load balancing and scaling
Comparative Analysis
Feature | TCP | UDP | HTTP | WebSocket |
---|---|---|---|---|
Connection | Connection-oriented | Connectionless | Connection-oriented | Persistent connection |
Reliability | Guaranteed delivery | Best-effort | Reliable (over TCP) | Reliable (over TCP) |
Ordering | Ordered delivery | No ordering | Ordered (HTTP/2 streams) | Ordered |
Speed | Moderate | Fast | Varies (1.1 vs 2 vs 3) | Fast after handshake |
Overhead | Moderate | Low | High (headers) | Low after handshake |
Use Case | Most internet apps | Real-time, UDP | Web, API | Real-time, push |