Back to All Concepts
NetworkingProtocolsTCPPerformanceAdvanced

TCP Handshake & Congestion

The 3-way handshake that powers the internet. SYN, SYN-ACK, ACK. Flow control vs Congestion control, and modern algorithms like BBR.

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.

mermaid
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...
Click to expand code...
  1. SYN: "Let's talk. My sequence starts at xx."
  2. SYN-ACK: "I hear you (x+1x+1). My sequence starts at yy."
  3. ACK: "I hear you (y+1y+1)."

Connection Termination (4-Way Wave)

  1. Client: FIN "I'm done."
  2. Server: ACK "Roger."
  3. Server: FIN "I'm done too." (Can happen later).
  4. Client: ACK "Bye."

Flow Control vs Congestion Control

These are often confused.

FeatureProblem SolvedMechanism
Flow ControlReceiver is too slow (Buffer overflow)Receive Window (rwnd) sent in ACK headers.
Congestion ControlNetwork (Router) is congestedCongestion Window (cwnd) estimated by Sender.

Congestion Algorithms

  1. Slow Start: Start fast, double speed every RTT. (1 packet -> 2 -> 4 -> 8...)
  2. 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().

python
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())
Click to expand code...

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

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