Networking & Cloud Computing

How TCP Works: Handshakes, Sequence Numbers and Congestion Control

How the internet's workhorse protocol turns lossy, jumbled packets into a clean, reliable stream of bytes

Zarton EditorsNov 13, 2026 · 8 min read
Listen to this article

The internet's packet delivery is a bit like a postal service run by a tired intern: letters arrive late, out of order, twice, or not at all. Yet when you load a web page or download a file, every byte shows up exactly once and in the right order. The protocol doing that quiet cleanup is TCP.

This guide explains how TCP works from the first packet to the last: the three-way handshake, sequence numbers and acknowledgments, how lost data gets resent, and the two separate brakes, flow control and congestion control, that keep a fast sender from drowning a slow receiver or a busy network.

What TCP promises on top of IP

The Internet Protocol (IP) only tries to move individual packets from one address to another. It makes no promises. The Transmission Control Protocol sits one layer above and turns that best-effort service into something applications can trust. The current specification, RFC 9293 (August 2022), describes it as a reliable, in-order byte stream, with each chunk of the stream, called a segment, carried inside an IP packet.

TCP was first written down in RFC 793 back in 1981. Four decades of fixes were scattered across many later documents until RFC 9293 gathered them into one standard. Its core jobs are:

  • Connections. Two endpoints agree to talk before any data flows, and agree to stop when they are done.
  • Reliability. Sequence numbers detect loss, a mandatory checksum detects corruption, and retransmission repairs both.
  • Ordering. The receiver reassembles bytes in the right order no matter how packets arrived.
  • Multiplexing. Port numbers let many conversations share one machine, which is why a browser can hold dozens of connections at once.
  • Rate control. Flow control and congestion control decide how fast the sender may go.

How TCP works, step one: the three-way handshake

Before data moves, each side must tell the other where its byte numbering starts. That starting point is the initial sequence number (ISN), and the exchange that shares it is the famous three-way handshake:

  1. SYN. The client sends a segment with the SYN flag set and its ISN, say 100.
  2. SYN-ACK. The server replies with its own ISN, say 300, and acknowledges the client's with ACK 101. The SYN itself uses up one sequence number, so the server now expects byte 101.
  3. ACK. The client acknowledges with ACK 301. Both sides are now in the ESTABLISHED state and data can flow.

Why three messages instead of two? Sequence numbers are not tied to any global clock, so a server receiving a SYN cannot tell whether it is fresh or a stale duplicate from an old connection that wandered around the network. The third message lets the client confirm the conversation is real. The SYN segments also carry options, most importantly the Maximum Segment Size (MSS), the largest chunk each side can receive. If no MSS is sent, the spec says to assume a conservative 536 bytes for IPv4 or 1,220 for IPv6.

On secure sites, this handshake is only the first round trip. The TLS handshake that encrypts HTTPS traffic runs on top of the TCP connection once it is established.

Sequence numbers, ACKs and retransmission

Here is the clever part: TCP numbers every byte, not every packet. The 32-bit sequence number in each segment names its first byte, and the acknowledgment number tells the other side which byte is expected next. ACKs are cumulative, so ACK 5001 means everything before byte 5001 arrived safely.

Detecting and repairing loss

The sender keeps unacknowledged data in a retransmission queue and starts a timer. If no ACK arrives before the retransmission timeout (RTO), it resends. The RTO is not fixed; it is calculated from measured round-trip times using the algorithm in RFC 6298, and it backs off exponentially when retries keep failing.

Waiting for a timer is slow, so TCP has a shortcut. When a segment goes missing, the receiver keeps acknowledging the last in-order byte it has. Three duplicate ACKs in a row signal that something specific was lost, and the sender performs a fast retransmit without waiting for the timeout. The optional Selective Acknowledgment (SACK) option goes further, letting the receiver describe exactly which ranges it holds so the sender only resends the gaps.

Receivers also try not to flood the network with ACKs. RFC 9293 recommends a delayed ACK that covers at least every second full-sized segment, while never holding an ACK back longer than half a second.

Flow control: don't overwhelm the receiver

Every segment carries a 16-bit window field: the number of bytes the receiver is currently willing to accept beyond what it has acknowledged. This advertised window, often called rwnd, shrinks as the receiving application falls behind and grows again as it reads data. Since 16 bits caps the window at about 64 KB, the widely supported Window Scale option multiplies it so high-speed, long-distance links can keep many megabytes in flight.

If the receiver's buffer fills completely, it advertises a zero window and the sender pauses. To avoid a deadlock if the "window open" update gets lost, the sender periodically sends tiny zero-window probes until space reappears.

Congestion control: don't overwhelm the network

Flow control protects the receiver. Congestion control protects every router between the two endpoints. The sender keeps its own private estimate called the congestion window (cwnd), and at any moment it may only have the smaller of cwnd and rwnd unacknowledged.

RFC 9293 makes basic congestion control mandatory, pointing to RFC 5681 for the classic algorithms that grew out of Van Jacobson's work after the internet's congestion collapses in the 1980s:

  • Slow start. Despite the name, growth is exponential: the window roughly doubles every round trip until it hits a threshold or loss occurs. RFC 6928 raised the allowed starting point to about 10 segments.
  • Congestion avoidance. Past the threshold, growth becomes linear, about one segment per round trip.
  • Fast retransmit and fast recovery. On three duplicate ACKs, resend the missing segment and cut the window roughly in half instead of starting over.
  • Timeout. A full retransmission timeout is treated as severe congestion, and the window collapses back to slow start.

Modern algorithms: CUBIC and BBR

The linear climb of classic Reno-style TCP is painfully slow on fast, long-distance paths. Two newer approaches dominate today:

AlgorithmCongestion signalHow it growsWhere you'll find it
Reno / NewRenoPacket lossLinear, halves on lossThe textbook baseline
CUBICPacket lossCubic curve based on time since last lossLinux default; IETF Proposed Standard as RFC 9438
BBRModel of path bandwidth and minimum RTTPaces sending near the estimated bandwidth-delay productDeveloped at Google; used by large content providers

The key difference is philosophy. Loss-based algorithms keep pushing until a router's buffer overflows and drops a packet. BBR instead keeps estimating the bottleneck's bandwidth and the path's minimum delay, which makes it more tolerant of random loss on links like Wi-Fi or satellite.

Closing a connection and the TIME-WAIT state

Each direction of a TCP connection closes independently. One side sends a FIN, meaning it has no more data, and the other side acknowledges it but may keep sending until it sends its own FIN. That allows a half-closed connection, useful when a client finishes uploading a request but still wants the response.

The side that closes first then lingers in TIME-WAIT for twice the Maximum Segment Lifetime. The spec treats that lifetime as two minutes, though many operating systems use shorter values in practice. TIME-WAIT exists so a lost final ACK can be resent and so stray segments from the old connection die off before a new one reuses the same addresses and ports. A RST (reset) segment is the abrupt alternative: it aborts the connection immediately and discards its state.

Inspecting TCP on your own machine

On Linux, the ss tool shows connection states and live congestion details, and sysctl reveals which algorithm is active:

# Count connections by state (ESTAB, TIME-WAIT, CLOSE-WAIT...)
ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c

# Show cwnd, RTT and retransmits for established connections
ss -ti state established

# Which congestion control algorithm is in use?
sysctl net.ipv4.tcp_congestion_control
sysctl net.ipv4.tcp_available_congestion_control
A pile of CLOSE-WAIT sockets usually means an application forgot to close connections; lots of TIME-WAIT is normal on busy clients.

A few practical notes. Latency-sensitive apps often disable Nagle's algorithm with TCP_NODELAY, which stops TCP from batching tiny writes but leaves congestion control untouched. Long-lived connections such as WebSockets rely on TCP underneath, so their behavior through proxies depends on TCP timeouts. And a Layer 4 load balancer makes its routing decisions at exactly this level, on TCP connections rather than HTTP requests.

Key takeaways

  • TCP turns IP's unreliable packet delivery into a reliable, ordered byte stream.
  • The three-way handshake syncs unpredictable starting sequence numbers and filters out stale connection attempts.
  • Every byte is numbered; cumulative ACKs, timeouts, fast retransmit and SACK repair losses.
  • Flow control (rwnd) protects the receiver; congestion control (cwnd) protects the network.
  • CUBIC reacts to loss, while BBR models the path; FIN, TIME-WAIT and RST govern how connections end.

Frequently asked questions

What is the difference between TCP and UDP?

UDP just sends individual datagrams with no handshake, ordering, retransmission or congestion control. It is lighter and lower latency, so applications that can tolerate loss, or that build their own reliability, often prefer it.

Is HTTP/3 still built on TCP?

No. HTTP/1.1 and HTTP/2 run over TCP, but HTTP/3 runs over QUIC, which is built on UDP and reimplements reliability and congestion control in user space, partly to avoid one lost packet stalling every stream.

Why are there so many TIME-WAIT connections on my server?

Whichever side closes a connection first enters TIME-WAIT. Servers that actively close many short connections accumulate them. It is usually harmless; keeping connections alive and reusing them is the cleanest fix.

Should I switch my servers from CUBIC to BBR?

It depends on your traffic. BBR can help on long, lossy paths, while CUBIC is a solid default for most networks. Benchmark both with your real workload before changing anything in production.

Sources

Comments

No comments yet — be the first to share your thoughts.

Join the discussion

Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Keep reading

All posts

Run your whole business from one conversation.

Get startedTalk to us