← All posts
TCP/IP

What is TCP and How It Stays Reliable: The Basics Every Engineer Should Know

What is TCP and How It Stays Reliable: The Basics Every Engineer Should Know

IP is a best-effort service. A packet can be dropped by a congested queue, duplicated, delayed, or delivered out of order, and the network will not tell you. TCP sits on top of that unreliable layer and gives applications something much friendlier: an ordered, complete byte stream. This post explains how TCP builds that guarantee, and how to see it happen in a packet capture.

What "reliable" actually means

TCP, defined in RFC 9293 (which obsoletes the original RFC 793), promises that the bytes you write on one side arrive on the other side in order, without gaps, and without duplicates, or else the connection reports an error. It does not promise speed, and it does not promise bounded latency. Reliability is bought with state, acknowledgements and retransmission.

The building blocks are few: sequence numbers, acknowledgements, a retransmission timer, and a checksum. Everything else (windowing, congestion control) tunes how fast these run.

Sequence numbers count bytes, not packets

Every byte in a TCP stream has a sequence number. A segment carries the sequence number of its first byte, so a segment with Seq=1000 and 500 bytes of payload covers bytes 1000 to 1499, and the next segment should start at Seq=1500. The receiver uses these numbers to put segments back in order and to spot duplicates and gaps.

The initial sequence number is chosen when the connection is opened. If you want the field-level detail, see the TCP header, ports, sequence numbers and flags and the three-way handshake. Here we only need the idea that every byte is numbered.

Acknowledgements are cumulative

The receiver answers with an acknowledgement number meaning "I have received everything up to, but not including, this byte; send me this one next." Standard ACKs are cumulative: ACK=1500 confirms every byte below 1500, even if the individual ACKs for earlier segments were lost.

A simplified exchange (illustrative values) looks like this:

A -> B  Seq=1000 Len=500          (bytes 1000-1499)
B -> A  ACK=1500                  (got everything below 1500)
A -> B  Seq=1500 Len=500          (bytes 1500-1999)
A -> B  Seq=2000 Len=500          (bytes 2000-2499)
B -> A  ACK=2500                  (one ACK covers both segments)

Receivers are allowed to delay an ACK briefly so one ACK can cover several segments, which is why you often see roughly one ACK per two data segments in a capture.

Loss detection, way one: the retransmission timeout

If the sender does not see an ACK for a segment in time, it assumes the segment was lost and sends it again. The wait is the retransmission timeout (RTO). TCP does not use a fixed value. It continuously measures round-trip time from ACKs, keeps a smoothed RTT and a variance estimate, and derives the RTO from them. The algorithm is specified in RFC 6298.

Two practical consequences follow:

  • The RTO adapts to the path. A LAN connection recovers from loss much faster than one crossing an intercontinental link.

  • After a timeout the RTO is doubled for each consecutive retry (exponential backoff), so a dead path produces retransmissions spaced further and further apart until the stack gives up.

Timeouts are the slow path. The sender sits idle waiting, and throughput collapses. That is why TCP has a faster way to detect loss.

Loss detection, way two: duplicate ACKs and fast retransmit

Suppose segment 2 is lost but segments 3, 4 and 5 arrive. The receiver cannot advance its cumulative ACK, so each out-of-order arrival triggers another ACK for the same missing byte. The sender sees repeated identical ACKs. After three duplicate ACKs, it retransmits the missing segment immediately, without waiting for the RTO. This is fast retransmit, described in RFC 5681.

A -> B  Seq=1000 Len=500
A -> B  Seq=1500 Len=500          (lost in the network)
A -> B  Seq=2000 Len=500
A -> B  Seq=2500 Len=500
A -> B  Seq=3000 Len=500
B -> A  ACK=1500                  (normal ACK for the first segment)
B -> A  ACK=1500                  (dup ACK #1, triggered by Seq=2000)
B -> A  ACK=1500                  (dup ACK #2, triggered by Seq=2500)
B -> A  ACK=1500                  (dup ACK #3, triggered by Seq=3000)
A -> B  Seq=1500 Len=500          (fast retransmit)
B -> A  ACK=3500                  (hole filled, cumulative ACK jumps)

Notice the last line: once the gap is filled, the receiver already holds the later segments, so its ACK jumps forward past all of them.

SACK: telling the sender exactly what arrived

Cumulative ACKs have a weakness. With several losses in one window, the sender only learns about the first hole and has to guess about the rest. Selective acknowledgement (SACK), RFC 2018, fixes this. During the handshake both ends advertise "SACK permitted". After that, duplicate ACKs carry SACK blocks listing the byte ranges the receiver already has beyond the hole:

ACK=1500  SACK={2000-3500}        (illustrative: I have 2000-3499, still missing 1500-1999)

The sender can then retransmit only what is actually missing rather than everything after the first loss.

The checksum catches corruption

Every segment carries a 16-bit checksum over the TCP header, payload and a pseudo-header containing the IP addresses. A segment that fails the check is discarded, which the sender then experiences as ordinary loss and repairs by retransmission. The checksum is deliberately simple, so applications that need strong integrity (file transfers, for instance) still add their own checks or use TLS.

What reliability does not do

  • It does not stop the receiver from being overwhelmed. That is flow control, handled by the advertised window. See TCP receive window and window scaling.

  • It does not stop the network from being overwhelmed. That is congestion control (slow start, congestion avoidance), which reacts to the same loss signals described above.

  • It does not protect against a dead peer silently. An idle connection with a vanished peer can sit open indefinitely unless keepalives or the application detect it.

See it in a capture

You do not need a lab to watch this. Capture a download in Wireshark, or on a Linux host with a filter such as the one below (interface name is an example):

sudo tcpdump -i eth0 -w tcp-demo.pcap 'tcp port 443'

Open the file in Wireshark and try these display filters:

tcp.analysis.retransmission
tcp.analysis.fast_retransmission
tcp.analysis.duplicate_ack
tcp.analysis.lost_segment
tcp.options.sack

Wireshark's expert analysis flags retransmissions, duplicate ACKs and suspected lost segments for you. Note that "lost segment" is inferred from where the capture was taken: if the packet was dropped before your capture point, you see a gap; if it was dropped after, you only see the retransmission. A clean path will show few or none of these; a lossy Wi-Fi link or a congested uplink will show them quickly. On Linux, ss -ti shows per-connection counters such as retransmissions, RTT and RTO for live sockets (exact fields vary by kernel and iproute2 version).

Why network engineers should care

When an application is "slow", TCP's loss recovery is often the mechanism turning a small amount of packet loss into a large drop in throughput. A single timeout costs far more than a fast retransmit. Knowing which one you are looking at tells you whether you are chasing tail loss, a congested queue, or a path with a bad link. The same TCP machinery also carries routing sessions, so a retransmitting transport can show up as a flapping neighbor; BGP peering over a TCP session is a good example.

Key takeaways

  • TCP numbers bytes, and ACKs say which byte is expected next.

  • Loss is detected by an adaptive RTO (slow) or three duplicate ACKs (fast).

  • SACK lets the receiver report exactly which ranges arrived.

  • Reliability, flow control and congestion control are separate mechanisms that cooperate.

  • Wireshark's tcp.analysis.* filters make all of this visible.

Watch the video

Prefer to see it walked through? Watch the video version on YouTube.

Comments (0)

Please sign in to comment.

  • Be the first to comment.