{}DSA Atlas

Transport — TCP, UDP & QUIC

TCP vs UDP, the three-way handshake and four-way teardown, TCP states and TIME_WAIT, sequence numbers and retransmission, flow control and the bandwidth-delay product, congestion control (slow start, AIMD, CUBIC, BBR), Nagle and delayed ACKs, head-of-line blocking, and why QUIC exists.

Intermediate9 interview questions

The transport layer's job

IP delivers packets host to host, best effort: packets may be lost, duplicated, reordered or corrupted. The transport layer delivers data process to process (via ports) and optionally makes it reliable.

TCP vs UDP at a glance

TCPUDP
Modelreliable, ordered byte streamunreliable datagrams
Setup3-way handshakenone
ReliabilityACKs, retransmissions, checksumschecksum only
Orderingyesno
Flow & congestion controlyesno
Message boundariesno (stream: app must frame messages)yes (one send = one datagram)
Broadcast/multicastnoyes
Header20 B + options8 B
UsesHTTP/1–2, TLS, SSH, databases, SMTPDNS, VoIP/video, games, DHCP, QUIC, WebRTC

Connection lifecycle

Client Server SYN seq=x SYN-ACK seq=y ack=x+1 ACK ack=y+1 (+ data) 1 RTT ESTABLISHED data (seq …) data + ACK FIN ACK FIN ACK TIME_WAIT(2×MSL)
Handshake (1 RTT), data, and the four-way close. The side that closes first waits in TIME_WAIT.

TCP states worth knowing

StateMeaning
LISTENserver waiting for SYNs
SYN_SENT / SYN_RECEIVEDhandshake in progress
ESTABLISHEDdata flowing
FIN_WAIT_1/2we closed; waiting for the peer's FIN
CLOSE_WAITpeer closed; our app hasn't called close() — a pile of these = a bug (leaked sockets)
LAST_ACKwe sent FIN after the peer's
TIME_WAITactive closer waits 2×MSL

ss -tan state time-wait | wc -l and ss -s are the go-to commands.

SYN floods

Attackers send many SYNs without completing the handshake, filling the server's backlog of half-open connections. Defences: SYN cookies (encode the state in the server's ISN so no memory is used until the final ACK), larger backlogs, rate limiting, DDoS scrubbing upstream.

Reliability

  • Sequence numbers number every byte; ACKs are cumulative ("I have everything up to byte N").
  • Retransmission timeout (RTO) computed from smoothed RTT and variance (with exponential backoff).
  • Fast retransmit: three duplicate ACKs signal a lost segment → resend immediately without waiting for the timeout.
  • SACK (selective ACK) tells the sender exactly which blocks arrived, so only the gaps are resent.
  • Checksums detect corruption; duplicates are discarded by sequence number.

Flow control

The receiver advertises a receive window; the sender keeps bytes_in_flight ≤ rwnd.

Bandwidth-delay product (BDP) — the bytes that must be "in flight" to fill a pipe:

Text
BDP = bandwidth × RTT
1 Gbit/s × 80 ms = 10^9 / 8 × 0.08 = 10 MB in flight

Max throughput = window / RTT
64 KB / 100 ms ≈ 5.2 Mbit/s   (why window scaling exists)

Congestion control

time (RTTs)cwnd ssthresh slow start(doubles per RTT) additive increase loss (3 dup ACKs) → halve loss → halve
Classic AIMD sawtooth (Reno/CUBIC-style). A retransmission timeout would drop cwnd back to its initial value.
AlgorithmSignalNotes
Reno / NewRenopacket lossclassic AIMD
CUBIClossLinux/Windows default; cubic growth, good on high-BDP paths
BBR (v1/v2/v3)measured bandwidth + min RTTpaces at estimated bottleneck rate; better on lossy, long paths; used by Google, YouTube
ECNrouters mark packets instead of droppingrequires support end to end

Bufferbloat: oversized router buffers fill up before loss occurs, adding hundreds of ms of latency. Delay-based algorithms (BBR) and AQM (CoDel, FQ-CoDel) help.

Small writes: Nagle and delayed ACK

  • Nagle: buffer small writes while data is unacknowledged.
  • Delayed ACK: wait briefly to piggyback ACKs.
  • Combination → ~40 ms stalls for chatty request/response protocols → set TCP_NODELAY and write complete messages at once.

Keepalives and idle connections

TCP keepalive probes (default after 2 hours on Linux!) detect dead peers. NATs, firewalls and load balancers drop idle mappings much sooner (often 60–350 s), so long-lived connections (WebSockets, database pools, gRPC) need application-level heartbeats or tuned keepalives, plus reconnection logic.

Head-of-line blocking and QUIC

HTTP/2 over TCP — one ordered byte stream A1 B1 A2 lost B2 ⏸ C1 ⏸ B and C wait for A2's retransmission HTTP/3 over QUIC — independent streams A1 B1 A2 lost B2 ✓ C1 ✓ only stream A waits QUIC also merges the transport and TLS handshakes (1-RTT, 0-RTT on resumption) and survives IP changes via connection IDs.
Why HTTP/3 moved off TCP.
TCP + TLS 1.3QUIC
Handshake to first byte2 RTT (TCP 1 + TLS 1)1 RTT (0-RTT resumption)
Streamsone byte streammany independent streams
Loss affectseverything on the connectiononly affected streams
Encryptionpayload only (TCP headers visible)nearly everything
Connection identity5-tupleconnection ID (migration)
Implementationkerneluser space (fast evolution)

Practical rules for backend engineers

  • Reuse connections (keep-alive, pools): avoid handshake RTTs, slow start and TIME_WAIT churn.
  • Set timeouts on connect, read and total request time — TCP itself will wait for minutes.
  • Use TCP_NODELAY for latency-sensitive request/response protocols.
  • Size socket buffers for high-BDP links; let autotuning work.
  • Monitor retransmission rates (nstat, ss -ti) when latency spikes.

Interview questions

0 / 9 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.