TCP: Reliability, Sequencing, Flow Control, Congestion Control
What This Concept Is
TCP turns IP's unreliable packet delivery into a reliable, ordered byte stream between two processes. It does this with four distinct mechanisms that coexist in every TCP connection:
- Reliability -- every byte is acknowledged; unacked bytes are retransmitted on timeout or on duplicate ACKs.
- Sequencing -- each byte has a sequence number, so the receiver can reassemble bytes in order even if packets arrive out of order.
- Flow control -- the receiver advertises a
window(how much data it has buffer space for). The sender never sends more in-flight than that window. - Congestion control -- the sender also maintains a
cwnd(congestion window) that shrinks when the network is congested and grows cautiously when it is not.
The TCP header carries the machinery for all four, plus flags, on top of IP.
Why It Matters Here
Almost every performance or reliability question about a service traces to one of these four mechanisms.
- "Why does my upload slow down under loss?" -- congestion control.
- "Why does a slow consumer back up a producer?" -- flow control.
- "Why are bytes arriving out of order?" -- they are not; sequencing masks that from you.
- "Why did a connection hang and then suddenly resume?" -- retransmission after a timeout.
Separating the four instead of saying "TCP just works" is what distinguishes someone who can tune a network service from someone who cannot.
Concrete Example
A TCP segment header:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Offset| Res |C|E|U|A|P|R|S|F| Window Size |
| | |W|C|R|C|S|S|Y|I| |
| | |R|E|G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options (variable) + Payload |
+---------------------------------------------------------------+
- Sequence + Acknowledgment together do reliability and ordering.
- Window Size is the flow-control field (how many more bytes the receiver can accept right now).
- Flags include
SYN,ACK,FIN,RST, plusECE/CWRfor Explicit Congestion Notification. - Options carry things like Selective ACK and window scaling.
A typical classic congestion-control algorithm (Reno) does: slow-start doubles cwnd per RTT until the first loss, then switches to additive-increase multiplicative-decrease -- shrink by half on loss, then grow by one MSS per RTT.
Common Confusion / Misconception
"Flow control and congestion control are the same thing." They are not. Flow control protects the receiver from being overwhelmed. Congestion control protects the network from being overwhelmed. TCP uses both simultaneously: it sends at most min(rwnd, cwnd) bytes in flight.
"TCP guarantees delivery." It guarantees delivery or it kills the connection. If enough retransmissions fail, the connection is torn down with a reset or a timeout, and the application sees an error.
How To Use It
When diagnosing a TCP issue, ask which of the four is acting up:
- Are bytes being lost and retransmitted? (Reliability.)
- Are bytes arriving scrambled at the application? (Never -- if so, TCP itself is broken.)
- Is the window collapsing because the receiver is slow? (Flow control.)
- Is the sender deliberately slowing down after loss? (Congestion control.)
Check Yourself
- How do sequence and acknowledgment numbers together enable reordering and retransmission?
- Why is
rwndadvertised by the receiver butcwndcomputed by the sender? - What does "slow start" actually start slow at, and why is the first RTT of a connection important?
Mini Drill or Application
- Download a moderate file with
curl -v -o /dev/null https://example.com/on a wired connection, and again over a flaky Wi-Fi. - Use
ss -tito observecwnd,rwnd, and retransmits while the transfer is happening. - Describe how the values differ between the two networks and attribute each difference to one of the four mechanisms.