TCP/IP Stack & Handshake

TCP 3-way handshake, SYN/ACK/FIN/RST flags, TCP state machine, congestion control algorithms, and Nagle algorithm tradeoffs.

Overview

Overview The TCP/IP stack is the foundational protocol suite that powers virtually all internet communication. Understanding it deeply is essential for any engineer working on distributed systems, backend services, or infrastructure.

TCP (Transmission Control Protocol) operates at Layer 4 of the OSI model and provides reliable, ordered, and error-checked delivery of data between applications. It achieves this through a combination of sequence numbers, acknowledgments, retransmissions, and flow control mechanisms.

The 3-way handshake is the mechanism by which a TCP connection is established. The client sends a SYN (synchronize) segment, the server responds with SYN-ACK (synchronize-acknowledge), and the client completes the handshake with an ACK (acknowledge). This exchange allows both sides to agree on initial sequence numbers and establish connection parameters. Tearing down a connection is a 4-way process: FIN, ACK, FIN, ACK — because each direction of data flow is closed independently.

TCP flags carry semantic meaning: SYN initiates connections, ACK acknowledges received data, FIN initiates graceful close, RST abruptly terminates a connection, PSH requests immediate delivery to the application layer, and URG marks urgent data.

Congestion control is one of TCP's most sophisticated features. Algorithms like CUBIC (default on Linux), BBR (Bottleneck Bandwidth and Round-trip propagation time), and Reno use different signals — packet loss, RTT variance, and explicit congestion notification (ECN) — to probe available bandwidth without overwhelming the network.

The Nagle algorithm reduces small-packet overhead by coalescing writes into fewer, larger segments. While this improves throughput on high-latency links, it introduces latency for interactive applications like SSH and database protocols, which often disable it with TCP_NODELAY.

In cloud and SRE contexts, understanding TCP's state machine, TIME_WAIT accumulation, SYN flood protection, and keep-alive settings is critical for diagnosing connection exhaustion, latency spikes, and cascading failures under load.

Key concepts

  • 3-Way Handshake
  • TCP State Machine
  • Congestion Control
  • Nagle Algorithm
  • RST vs FIN
  • TCP Keep-Alive

Interview questions

  • Walk me through the TCP 3-way handshake and explain what information is exchanged at each step.
  • Explain TCP congestion control. How does BBR differ from CUBIC, and when would you choose one over the other?
  • What happens to TCP connections when a server crashes mid-connection, and how do you detect and handle this in production?

Sign up free to read the full topic

More Networking topics

Start free on Cariara · Coding problems · System design studio