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.
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
| TCP | UDP | |
|---|---|---|
| Model | reliable, ordered byte stream | unreliable datagrams |
| Setup | 3-way handshake | none |
| Reliability | ACKs, retransmissions, checksums | checksum only |
| Ordering | yes | no |
| Flow & congestion control | yes | no |
| Message boundaries | no (stream: app must frame messages) | yes (one send = one datagram) |
| Broadcast/multicast | no | yes |
| Header | 20 B + options | 8 B |
| Uses | HTTP/1–2, TLS, SSH, databases, SMTP | DNS, VoIP/video, games, DHCP, QUIC, WebRTC |
Connection lifecycle
TCP states worth knowing
| State | Meaning |
|---|---|
| LISTEN | server waiting for SYNs |
| SYN_SENT / SYN_RECEIVED | handshake in progress |
| ESTABLISHED | data flowing |
| FIN_WAIT_1/2 | we closed; waiting for the peer's FIN |
| CLOSE_WAIT | peer closed; our app hasn't called close() — a pile of these = a bug (leaked sockets) |
| LAST_ACK | we sent FIN after the peer's |
| TIME_WAIT | active 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:
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
| Algorithm | Signal | Notes |
|---|---|---|
| Reno / NewReno | packet loss | classic AIMD |
| CUBIC | loss | Linux/Windows default; cubic growth, good on high-BDP paths |
| BBR (v1/v2/v3) | measured bandwidth + min RTT | paces at estimated bottleneck rate; better on lossy, long paths; used by Google, YouTube |
| ECN | routers mark packets instead of dropping | requires 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_NODELAYand 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
| TCP + TLS 1.3 | QUIC | |
|---|---|---|
| Handshake to first byte | 2 RTT (TCP 1 + TLS 1) | 1 RTT (0-RTT resumption) |
| Streams | one byte stream | many independent streams |
| Loss affects | everything on the connection | only affected streams |
| Encryption | payload only (TCP headers visible) | nearly everything |
| Connection identity | 5-tuple | connection ID (migration) |
| Implementation | kernel | user 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_NODELAYfor 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
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.