Use TCP when Accuracy > Speed
- TCP: Guaranteed delivery, ordered packets. (Web, Email, File Transfer).
- UDP: Fire and forget. (Video Streaming, VoIP, Gaming, DNS).
The 3-Way Handshake Connection
Before sending data, Alice and Bob must agree on Sequence Numbers.
sequenceDiagram
participant C as Client (Alice)
participant S as Server (Bob)
Note left of C: State: CLOSED
Note right of S: State: LISTEN
C->>S: SYN (Seq=x)
Note left of C: State: SYN_SENT
S->>C: SYN (Seq=y), ACK (Ack=x+1)
Note right of S: State: SYN_RCVD
C->>S: ACK (Ack=y+1)
Note left of C: State: ESTABLISHED
Note right of S: State: ESTABLISHED
Note over C,S: Data Transfer Begins...
- SYN: "Let's talk. My sequence starts at ."
- SYN-ACK: "I hear you (). My sequence starts at ."
- ACK: "I hear you ()."
Connection Termination (4-Way Wave)
- Client:
FIN"I'm done." - Server:
ACK"Roger." - Server:
FIN"I'm done too." (Can happen later). - Client:
ACK"Bye."
Flow Control vs Congestion Control
These are often confused.
| Feature | Problem Solved | Mechanism |
|---|---|---|
| Flow Control | Receiver is too slow (Buffer overflow) | Receive Window (rwnd) sent in ACK headers. |
| Congestion Control | Network (Router) is congested | Congestion Window (cwnd) estimated by Sender. |
Congestion Algorithms
- Slow Start: Start fast, double speed every RTT. (1 packet -> 2 -> 4 -> 8...)
- Packet Loss: Oops, router dropped a packet.
- Reno/CUBIC (Loss-based): Cut speed in half. "Loss signals congestion."
- BBR (Model-based): Google's algo. Measures Bandwidth and RTT. "Loss isn't always congestion."
The "Bufferbloat" Problem
Routers have large queues (buffers).
- Packet Loss algo: Fills the buffer -> Latency increases -> Finally drops packet -> Sender slows down.
- Result: High latency (Lag) before speed adjusts.
- BBR Solution: Detects when RTT rises (buffer filling) and slows down before packet loss.
Code Example: Socket Programming (C-style)
This is what happens under the hood of http.Get().
import socket
# 1. Create Socket (IPv4, TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. Connect (Triggers 3-Way Handshake)
# Sends SYN
s.connect(("google.com", 80))
# Returns after ACK received (ESTABLISHED)
# 3. Send Data (Push)
msg = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"
s.sendall(msg.encode())
# 4. Receive Data
response = b""
while True:
chunk = s.recv(4096)
if not chunk: break # Server sent FIN
response += chunk
s.close() # Sends FIN
print(response.decode())
Interview Tips š”
- "Why 3-way handshake?" ā To prevent old duplicate connection initiations from confusing the server. Both sides must confirm readiness.
- "SYN Flood Attack" ā Attacker sends thousands of SYNs but never ACKs. Server memory fills up waiting.
- Defense: SYN Cookies (Stateless handshake until ACK).
- "Nagel's Algorithm" ā Buffers small writes to send full packets. Bad for real-time games (disable with
TCP_NODELAY). - "TIME_WAIT" ā Why can't I restart my server immediately? The OS holds the port for 2 mins to ensure stray packets from the old connection die.
Related Concepts
- UDP vs TCP
- HTTP Evolution (Why QUIC replaces TCP)
- OS Networking Stack
About ScaleWiki
ScaleWiki is an interactive educational platform dedicated to demystifying distributed systems, software architecture, and system design. Our mission is to provide high-quality, technically accurate resources for software engineers preparing for interviews or solving complex scaling challenges in production.
Read more about our Editorial Guidelines & Authorship.
Educational Disclaimer: The architectural patterns and system designs discussed in this article are based on common industry practices, technical whitepapers, and public engineering blogs. Actual implementations in enterprise environments may vary significantly based on specific product requirements, legacy constraints, and evolving technologies.
Related Articles
HTTP Evolution (H1 to H3)
From text-based HTTP/1.1 to binary HTTP/2 and UDP-based HTTP/3 (QUIC). Why we needed upgrades and how they solve Head-of-Line Blocking.
DNS Architecture
The phonebook of the internet. How Domain Name System works, the hierarchy of Route 53, and recursive vs iterative resolution strategies.
Load Balancing
Layer 4 vs Layer 7 Load Balancing. Algorithms (Round Robin, Least Connections, Consistent Hashing). Health checks and real-world implementation with Nginx.