Mastering TCP: Sliding Window, Flow & Congestion Control Explained
This article continues the previous discussion on TCP handshakes and termination, then thoroughly explains nine essential TCP mechanisms—including sliding window, flow control, congestion control, delayed and piggyback ACKs, sticky packet handling, and keep‑alive—illustrated with diagrams and practical examples.
This article follows the previous piece that illustrated TCP's three‑way handshake and four‑way termination, and now explores the remaining seven key TCP features.
4. Sliding Window Mechanism
The sliding window improves transmission efficiency while preserving reliability by allowing multiple packets to be sent before waiting for acknowledgments.
In a simple stop‑and‑wait scenario, each packet must be acknowledged before the next is sent, resulting in a total transmission time of N data‑packet times plus N acknowledgment times.
By sending several packets in a batch, the waiting times overlap, reducing overall latency.
When the sender receives an ACK, the window slides forward, allowing the next set of data to be transmitted.
The window size defines the maximum amount of data that can be sent without waiting for an ACK. Larger windows increase network throughput.
Initially the first four segments are sent without waiting for ACKs.
After the first ACK arrives, the window moves forward and the next segment is sent.
The OS maintains a send buffer to track unacknowledged data.
A larger window yields higher throughput.
5. Flow Control
Flow control limits the sender’s rate based on the receiver’s processing capacity, using the receiver’s advertised window size.
If the window is too small, the sender transmits few packets, causing high latency for large transfers.
If the window is too large, the receiver cannot keep up, leading to buffer overflow and possible retransmissions.
The receiver places its available buffer size in the TCP header’s window field, which the sender uses for subsequent transmissions. When the buffer is nearly full, the receiver reduces the window; when it becomes empty, the window may be set to zero, prompting the sender to send periodic window‑probe packets.
6. Congestion Control (Safety Mechanism)
Congestion control adapts the sending rate to the level of network congestion. TCP uses a slow‑start phase that begins with a small congestion window and doubles it each RTT until a loss is detected, then reduces the window and repeats the cycle.
The window grows exponentially at first, then linearly after reaching a threshold. Upon packet loss, the window drops to the initial size and the process restarts, with the new threshold set to half of the previous window.
7. Delayed ACK (Efficiency Mechanism)
Delayed ACK postpones the transmission of an ACK to allow the receiver to acknowledge multiple packets with a single ACK, thereby increasing the advertised window and improving throughput.
Typical policies: acknowledge every N packets (commonly N=2) or after a maximum delay (often 200 ms).
8. Piggyback ACK (Efficiency Mechanism)
Piggybacking combines data and acknowledgment in the same segment, reducing the number of packets on the wire.
9. Sticky Packet Problem
Sticky packets arise from the byte‑stream nature of TCP, where the receiver may read partial application‑level messages, making it difficult to determine message boundaries.
Solutions include:
Using a delimiter (e.g., a semicolon) to mark the end of each message.
Prefixing each message with its length so the receiver knows how many bytes to read.
HTTP, for example, uses an empty line to delimit GET requests and a Content‑Length header for POST bodies.
10. Keep‑Alive Mechanism
When a TCP connection is idle for a long period, either side may send periodic heartbeat packets to confirm the peer is still reachable. If no heartbeat is received, the connection is considered dead and closed.
TCP Summary
TCP is complex because it must guarantee reliability while maximizing performance. Reliability is achieved through checksums, sequence numbers, acknowledgments, retransmission timers, and connection management. Performance is enhanced by sliding windows, fast retransmit, delayed ACK, and piggyback ACK. Additional mechanisms such as various timers (retransmission, keep‑alive, TIME_WAIT) support these goals.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.