π°οΈ Protocols - TCP, UDP, & WebSockets
Networking is a series of trade-offs between speed and certainty. A Programmer understands that the βoverheadβ of a protocol isnβt just bytes on the wireβitβs the time spent waiting for acknowledgments and the impact of packet loss on the user experience.
ποΈ TCP (Transmission Control Protocol)
Section titled βποΈ TCP (Transmission Control Protocol)βTCP is the βReliableβ protocol. It ensures that every packet sent arrives in the correct order and without errors.
- Three-Way Handshake:
SYNβSYN-ACKβACK. This establishes a connection before any data flows. - Flow Control & Congestion Control: TCP dynamically slows down if the network is congested to prevent βpacket storms.β
- Error Correction: If a packet is lost, TCP retransmits it.
- The Cost: This reliability comes at the price of latency (the overhead of the handshake and potential retransmissions).
β‘ UDP (User Datagram Protocol)
Section titled ββ‘ UDP (User Datagram Protocol)βUDP is the βFastβ protocol. It sends packets (βdatagramsβ) without establishing a connection or checking if they arrived.
- Fire and Forget: There is no handshake and no acknowledgment.
- Low Latency: Minimum overhead makes it ideal for time-sensitive data.
- Insight: If you need reliability over UDP (like HTTP/3 does with QUIC), you must implement it yourself at the Application Layer.
| Feature | TCP | UDP |
|---|---|---|
| Reliability | Guaranteed | Best-effort |
| Ordering | Guaranteed | Not guaranteed |
| Speed | Slower (due to overhead) | Extremely Fast |
| Use Case | Web, Email, SSH, Databases | Gaming, VoIP, Video Streaming, DNS |
π WebSockets (Full-Duplex Communication)
Section titled βπ WebSockets (Full-Duplex Communication)βWhile standard HTTP is a βPullβ protocol (client asks, server answers), WebSockets allow for a persistent, βPushβ connection.
- The Upgrade: Starts as an HTTP request with an
Upgrade: websocketheader. Once accepted, the connection switches from HTTP to a binary, bi-directional stream. - Low Overhead: Unlike HTTP polling, WebSockets donβt send heavy headers with every message.
- Stateful: The server must keep the connection open in memory.
Challenges with WebSockets:
Section titled βChallenges with WebSockets:β- Scaling: Load balancers must support βSticky Sessionsβ or use a Pub/Sub (like Redis) to sync messages across multiple server instances.
- Zombie Connections: You must implement Heartbeats (Ping/Pong) to detect and close dead connections that havenβt been properly terminated.
βοΈ Decision Framework
Section titled ββοΈ Decision FrameworkβWhen to use WebSockets vs. SSE (Server-Sent Events)?
Section titled βWhen to use WebSockets vs. SSE (Server-Sent Events)?β- WebSockets: Use when you need bi-directional real-time data (e.g., a Chat app or a Collaborative Editor).
- SSE: Use when you only need one-way updates from server to client (e.g., a Stock ticker or a Live news feed). SSE is simpler, works over standard HTTP, and has automatic reconnection.
The βHead-of-Lineβ Blocking Problem
Section titled βThe βHead-of-Lineβ Blocking ProblemβIn TCP, if packet #1 is lost, packets #2 and #3 must wait in the buffer until #1 is retransmitted, even if they arrived perfectly.
- Solution: This is why the industry moved to QUIC/HTTP3. It uses UDP to allow packets to be processed independently, eliminating this bottleneck.
π‘ Note: Before reaching for WebSockets, ask: βCan this be solved with Long Polling or SSE?β WebSockets introduce significant complexity in infrastructure (connection limits, load balancing, and state management). Donβt pay that complexity tax unless you truly need two-way real-time communication.