← Interview Prep

TCP — Theory & Mechanisms

Everything worth being able to explain about TCP at a whiteboard: connection lifecycle, reliability, flow and congestion control.

TCP turns an unreliable, unordered, best-effort IP layer into a reliable, ordered, byte-stream between two endpoints. Nearly every TCP interview question reduces to one of four jobs it performs, so keep them straight:

Reliability (sequence numbers + ACKs + retransmission), ordering (reassembly by sequence number), flow control (don't overrun the receiver — the advertised window), and congestion control (don't overrun the network — the congestion window). Flow control and congestion control are different problems solved by different windows.

Segment header

TCP is a Layer-4 protocol (IP protocol number 6). Its header is 20 bytes without options, up to 60 with them.

FieldSizePurpose
Source / Destination port16 bits eachIdentify the endpoints; the 4-tuple (src IP, src port, dst IP, dst port) uniquely identifies a connection.
Sequence number32 bitsByte offset of the first data byte in this segment (or the ISN during SYN).
Acknowledgment number32 bitsNext byte the sender expects to receive — cumulative. Valid only when ACK is set.
Data offset4 bitsHeader length in 32-bit words (hence the 60-byte max).
FlagsSYN, ACK, FIN, RST, PSH, URG, plus ECE/CWR for ECN and NS.
Window16 bitsReceiver's free buffer — the flow-control advertisement (scaled by the window-scale option).
Checksum16 bitsCovers header + data + a pseudo-header containing the IP addresses (why NAT must recompute it).
Urgent pointer16 bitsOffset of urgent data when URG is set (rarely used).
Options0–40 bytesMSS, Window Scale, SACK-permitted/SACK, Timestamps (RTTM + PAWS).

Connection establishment — the three-way handshake

Client                              Server
  │   SYN  seq=x                      │   (client: SYN-SENT)
  │ ────────────────────────────────▶│   (server: SYN-RECEIVED)
  │   SYN, ACK  seq=y, ack=x+1        │
  │ ◀────────────────────────────────│
  │   ACK  seq=x+1, ack=y+1           │   (both: ESTABLISHED)
  │ ────────────────────────────────▶│

Things interviewers push on

Connection termination & TIME_WAIT

  │  FIN  seq=u                 │   active closer: FIN_WAIT_1
  │ ──────────────────────────▶│   passive: CLOSE_WAIT
  │  ACK  ack=u+1              │
  │ ◀──────────────────────────│   active: FIN_WAIT_2
  │  FIN  seq=v                 │   (passive app closes) LAST_ACK
  │ ◀──────────────────────────│
  │  ACK  ack=v+1              │   active: TIME_WAIT ── 2·MSL ──▶ CLOSED
  │ ──────────────────────────▶│   passive: CLOSED

TCP is full-duplex, so each direction is closed independently — hence four segments (the middle two can coalesce, giving three). The side that sends the first FIN is the active closer.

State machine (the states to know)

CLOSED → LISTEN → SYN-RECEIVED → ESTABLISHED (server) and CLOSED → SYN-SENT → ESTABLISHED (client); teardown walks FIN-WAIT-1/2 → TIME-WAIT → CLOSED (active) and CLOSE-WAIT → LAST-ACK → CLOSED (passive).

Reliability: ACKs, retransmission, RTO

The receiver sends a cumulative ACK naming the next in-order byte it expects. Data is retransmitted when the sender infers loss, by one of two mechanisms:

MechanismTriggerNotes
RTO (timeout)No ACK before the retransmission timer firesSlow, conservative; also collapses the congestion window to 1 MSS.
Fast retransmit3 duplicate ACKs for the same byteRetransmit the missing segment immediately, without waiting for the RTO.

Flow control — the receive window

Flow control protects a slow receiver from a fast sender. The receiver advertises a window (rwnd) = free space in its buffer; the sender may have at most that many unacknowledged bytes in flight. This is the sliding window.

Bandwidth-delay product is the number to know: BDP = bandwidth × RTT is the amount of in-flight data needed to fill the pipe; if the window < BDP, throughput is capped regardless of link speed.

Congestion control — the congestion window

Congestion control protects the network. The sender maintains a congestion window (cwnd); the amount it may send is min(cwnd, rwnd). Classic (Reno/NewReno) has four phases:

PhaseBehavior
Slow startcwnd starts at ~1–10 MSS and doubles every RTT (exponential) until it reaches ssthresh or loss occurs.
Congestion avoidanceAbove ssthresh, cwnd grows linearly (+1 MSS/RTT) — additive increase.
Fast retransmit3 dup ACKs → retransmit the lost segment without a timeout.
Fast recoveryOn 3 dup ACKs, halve cwnd/ssthresh and continue (not back to 1) — multiplicative decrease.

Nagle, delayed ACK, and PSH

MSS, MTU, and PMTUD

TCP vs UDP

TCPUDP
ConnectionConnection-oriented (handshake)Connectionless
ReliabilityReliable, retransmitsBest-effort, none
OrderingOrdered byte streamUnordered datagrams
Flow / congestion controlYesNo (app's job)
Header20–60 bytes8 bytes
Head-of-line blockingYes (one loss stalls the stream)No
UseWeb, APIs, file transfer, anything needing reliabilityDNS, DHCP, VoIP, market-data multicast, QUIC/HTTP-3 base

Head-of-line blocking is the key TCP tradeoff to mention: because delivery is an ordered stream, a single lost segment blocks delivery of everything after it until it's retransmitted. This is exactly why QUIC (HTTP/3) runs over UDP with independent streams.

Likely follow-up questions

More company-bank questions (Meta-style)

Related: Life of a Packet · Low-Latency & Trading Networks.