TCP provides a reliable byte stream
- TCP: A reliable, ordered byte stream while the connection operates. A connection can fail, and an acknowledgment does not prove that the receiving application processed the data.
- UDP: Datagrams without built-in delivery or ordering guarantees. Applications may add reliability, as QUIC does. TCP specification: RFC 9293.
The 3-Way Handshake Connection
Before sending data, Alice and Bob must agree on Sequence Numbers.
- 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: Grow the congestion window rapidly as acknowledgments arrive, roughly doubling per RTT under suitable conditions, until a threshold or congestion signal is reached.
- Packet Loss: Oops, router dropped a packet.
- Reno/CUBIC: Use loss signals but have different window-reduction and growth rules; they do not always halve the sending rate.
- 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 approach: Builds a model of bottleneck bandwidth and propagation delay to pace traffic. Its behavior depends on implementation/version; it does not eliminate queues or loss.
Code example: a bounded Python TCP request
This minimal plaintext HTTP example illustrates stream I/O. It is not a full HTTP parser; production HTTP clients should handle TLS, body framing, limits and redirects.
import socket
# 1. Create Socket (IPv4, TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
# 2. Connect (Triggers 3-Way Handshake)
# Sends SYN
s.connect(("example.com", 80))
# Returns once the local TCP connection is established
# 3. Send Data (Push)
msg = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\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("utf-8", errors="replace"))
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).
- "Nagle’s Algorithm" — Buffers small writes to send full packets. Bad for real-time games (disable with
TCP_NODELAY). - "TIME_WAIT" — The endpoint that actively closes normally retains connection state for 2×MSL. The duration and listener-rebind behavior depend on the OS and socket configuration.
Failure exercise: did the order succeed?
A client sends an order, then the TCP connection closes before the response arrives. Transport reliability cannot resolve whether the server committed the order. Retrying blindly can create a duplicate. Give the operation an idempotency key and provide a way to retrieve its result. This separates delivery of bytes from the application’s business outcome.
Related Concepts
- HTTP over TCP and QUIC
- HTTP Evolution (Why QUIC replaces TCP)
- OS Networking Stack
Keep reading
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.
Content Delivery Networks: Cache Keys, Freshness and Origin Load
Choose safe cache policies, calculate origin offload, and test personalized responses, invalidation and cold-cache recovery.
DNS Architecture
The phonebook of the internet. How Domain Name System works, the DNS hierarchy, 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.
WebRTC (Peer-to-Peer Communication)
Browser APIs and protocols for real-time audio, video and data, with signaling, ICE connectivity checks, TURN relay fallback and multiparty media-server tradeoffs.
BitTorrent: Pieces, Peer Discovery and Swarm Limits
Understand verified piece exchange, v1 versus v2 metadata, peer discovery, and the capacity limits of cooperative distribution.