Reference notes.

TCP (Transmission Control Protocol) provides reliable, ordered, error-checked delivery of data between applications. It’s the transport layer protocol behind HTTP, SSH, SMTP, and most internet traffic.

TCP vs UDP

FeatureTCPUDP
ConnectionConnection-oriented (handshake)Connectionless
ReliabilityGuaranteed delivery, retransmissionsBest-effort, no retransmissions
OrderingOrdered deliveryNo ordering guarantee
Flow controlYes (sliding window)No
Congestion controlYes (CUBIC, BBR)No
Header size20-60 bytes8 bytes
Use casesHTTP, SSH, email, file transferDNS, gaming, streaming, VoIP, QUIC

UDP wins when speed matters more than reliability, or when the application handles reliability itself (e.g., QUIC implements its own reliable transport over UDP).

Connection Lifecycle

Three-Way Handshake (Connection Setup)

Client              Server
  |--- SYN ----------->|    1. Client sends SYN (seq=x)
  |<-- SYN-ACK --------|    2. Server responds SYN-ACK (seq=y, ack=x+1)
  |--- ACK ----------->|    3. Client sends ACK (ack=y+1)
  |                     |    Connection established

Both sides agree on initial sequence numbers, preventing old packets from being confused with new connections.

Four-Way Teardown (Connection Close)

Client              Server
  |--- FIN ----------->|    1. Client done sending
  |<-- ACK ------------|    2. Server acknowledges
  |<-- FIN ------------|    3. Server done sending
  |--- ACK ----------->|    4. Client acknowledges
  |                     |    Connection closed

Either side can initiate. The side that sends the first FIN enters TIME_WAIT state (RFC 9293 defines 2× MSL = 240 seconds, though Linux, macOS, and BSD typically use 60 seconds and Windows defaults to 120 seconds) to handle delayed packets.

TCP Flags and Key States

Common header flags:

  • SYN — Synchronise sequence numbers to initiate a connection
  • ACK — Acknowledge received bytes (set on every segment after the initial SYN)
  • FIN — Graceful close (sender has finished transmitting data)
  • RST — Immediate abnormal reset/abort of a connection
  • PSH — Prompt the receiver to deliver buffered data to the application immediately

Key connection states (visible in ss / netstat):

  • SYN_RECV — Server received SYN, sent SYN-ACK, waiting for the final ACK (high counts indicate a SYN flood or dropped ACKs)
  • ESTABLISHED — Active bidirectional data transfer
  • TIME_WAIT — Active closer waiting after sending the final ACK before reusing the 4-tuple
  • CLOSE_WAIT — Remote peer sent FIN and OS sent ACK, waiting for the local application to call close() (high counts usually indicate a socket leak)

TCP Fast Open (TFO)

Eliminates one round trip (1 RTT) on repeat connections. The server issues a cookie on the first connection; the client includes it with data in the SYN of subsequent connections, allowing data transfer before the handshake completes. Supported in Linux 3.7+, though public internet adoption has been poor due to middlebox interference and it has largely been superseded by QUIC 0-RTT.

Flow Control

Sliding Window

TCP uses a receive window to prevent the sender from overwhelming the receiver. The receiver advertises how many bytes it can accept; the sender must not have more than that amount of unacknowledged data in flight.

Window scaling (RFC 7323) — The original 16-bit window field limits the window to 64KB, far too small for modern high-bandwidth links. Window scaling extends this up to ~1GB using a multiplier negotiated during the handshake. Essential for high-bandwidth, high-latency links (e.g., 10Gbps with 50ms RTT needs ~62MB window).

Nagle’s Algorithm

Buffers small writes and sends them together to reduce the number of tiny packets. Waits until either enough data accumulates or the previous packet is acknowledged. Can cause latency issues in interactive applications — disabled with TCP_NODELAY socket option. Interacts poorly with delayed ACKs (40ms minimum up to 200ms maximum on Linux; historically 100–200ms on Windows/BSD), causing the “Nagle-delayed ACK deadlock” where each write stalls for a full delayed-ACK interval. Most modern applications set TCP_NODELAY.

Congestion Control

Congestion control prevents TCP from overwhelming the network (distinct from flow control, which protects the receiver). The sender maintains a congestion window (cwnd) limiting data in flight.

Classic Phases

  1. Slow start — cwnd starts small (typically 10 segments), doubles each RTT. Grows exponentially until loss occurs or ssthresh is reached.
  2. Congestion avoidance — After reaching ssthresh, cwnd grows linearly (1 segment per RTT). Probes for capacity gradually.
  3. Fast retransmit — After 3 duplicate ACKs, retransmit the lost segment immediately without waiting for a timeout.
  4. Fast recovery — After fast retransmit, reduce cwnd (halved in classic Reno/NewReno; reduced by 30%, , in CUBIC) and continue with congestion avoidance (don’t restart from slow start).

CUBIC

The default congestion control algorithm in Linux, Windows, and macOS. Loss-based — uses packet loss as the signal for congestion.

CUBIC uses a cubic function to grow cwnd, which is:

  • Aggressive far from the last loss point — quickly probes for new capacity
  • Conservative near the last loss point — avoids re-triggering congestion
  • Concave growth approaching the previous maximum, convex growth beyond it

Works well on high-bandwidth, high-latency links. Dominant algorithm on the internet.

BBR (Bottleneck Bandwidth and Round-trip time)

Google’s model-based congestion control. Instead of reacting to loss, BBR builds an explicit model of the network path using:

  • Estimated bottleneck bandwidth — Maximum delivery rate observed
  • Minimum RTT — Baseline round-trip time without queuing

BBR paces packets at the estimated bottleneck rate and limits in-flight data to bandwidth × RTT, targeting minimal queuing delay. Avoids “bufferbloat” — the problem where deep router buffers fill up, adding seconds of latency without dropping packets.

BBRv3 improves on earlier versions (reducing retransmit rates and integrating ECN and packet-loss signals alongside its model-based approach) to address BBRv1’s primary criticism: fairness against CUBIC flows. As of late 2026, BBRv3 has not been merged into mainline Linux — mainline Linux 6.x still ships BBRv1, so running BBRv3 requires out-of-tree patches while standardisation progresses as an active IETF CCWG draft. Fairness with CUBIC under deep-buffer conditions remains an active research area.

ECN (Explicit Congestion Notification)

Routers mark packets (set CE bit) when queues are filling, instead of dropping them. TCP endpoints can react to congestion before loss occurs. Requires support at both endpoints and intervening routers. Increasingly important for data centre networks and BBR’s evolution.

L4S (Low Latency, Low Loss, Scalable Throughput)

Framework standardised as Experimental RFCs 9330 (architecture), 9331 (ECN identifier), and 9332 (DualQ Coupled AQM), combining fine-grained ECN marking with scalable congestion control for ultra-low queuing latency. Seeing production rollouts across Apple platforms, DOCSIS cable networks, and 5G — targeting real-time collaboration, cloud gaming, and VR/AR.

Reliability Mechanisms

Selective Acknowledgements (SACK)

Without SACK, a single lost packet forces retransmission of everything after it. SACK allows the receiver to report exactly which segments it has received, so the sender only retransmits what’s actually missing. Enabled by default in most modern operating systems.

TCP Keepalive

Detects dead connections by sending periodic probes when no data has been exchanged. Default on Linux: probe after 2 hours, then every 75 seconds, give up after 9 probes. Configurable per-socket. Application-level keepalives (e.g., HTTP/2 PING) are generally preferred.

Retransmission Timeout (RTO)

Calculated from smoothed RTT measurements. If no ACK arrives before the RTO expires, the segment is retransmitted. Uses exponential backoff on repeated timeouts. Modern TCP uses timestamps (RFC 7323) for more accurate RTT measurement.

Common Issues

IssueCauseDiagnosis
High retransmissionsCongestion, packet loss, bad linksss -ti, Wireshark retransmission filter
Small window sizeReceiver or sender limited, window scaling offss -ti (check rcv_space, snd_cwnd)
TIME_WAIT accumulationMany short connectionsss -s, consider connection pooling
Connection resets (RST)Firewall, application crash, port not opentcpdump, check firewall rules
High latencyBufferbloat, Nagle + delayed ACKEnable TCP_NODELAY, check buffer sizes
SYN floodDDoS attackSYN cookies (net.ipv4.tcp_syncookies)

Tuning (Linux)

Key sysctl parameters:

  • net.core.rmem_max / net.core.wmem_max — Maximum socket buffer sizes
  • net.ipv4.tcp_congestion_control — Set to bbr or cubic
  • net.ipv4.tcp_window_scaling — Enable window scaling (default: on)
  • net.ipv4.tcp_fastopen — Enable TCP Fast Open (3 = client + server)
  • net.ipv4.tcp_syncookies — SYN flood protection (default: on)
  • net.ipv4.tcp_tw_reuse — Reuse TIME_WAIT sockets (safe for clients)

See Also

References