Sub topic 10
Flow control and the receive window
This chapter assumes you know what a TCP segment is, how sequence numbers and acknowledgments work, and that TCP delivers a reliable byte stream. It covers how a receiver keeps a fast sender from overwhelming it.
The problem flow control solves
A receiving application reads data from TCP at its own pace. Until the application reads it, incoming data sits in a buffer in the operating system. This buffer is the receive buffer. If a sender transmits faster than the application reads, the buffer fills. Data that arrives after that point has to be dropped, and the sender must retransmit it.
Flow control prevents this. It is a feedback loop between one sender and one receiver. The receiver tells the sender how much more data it can accept, and the sender never sends more than that.
The receive window
Every TCP segment carries a 16-bit Window field in its header. It holds the receive window (rwnd): the number of bytes the sender of that segment is currently willing to accept, counted from the acknowledgment number onward.
The receiver computes it as free buffer space:
rwnd = receive buffer size - bytes received but not yet read by the applicationThe sender tracks how many bytes it has sent but not yet had acknowledged. These are the bytes in flight. It must keep them within the advertised window:
bytes in flight <= rwndEach acknowledgment carries a fresh window value, so the limit changes continuously. This is called a sliding window, because the range of sequence numbers the sender may use moves forward as acknowledgments arrive.
Zero window and window probes
When the receiver advertises a window of 0, the sender stops sending data. The window update that reopens it is a pure acknowledgment with no data. TCP does not retransmit pure acknowledgments, so if that update is lost, both sides could wait forever.
To avoid this deadlock, the sender runs a persist timer. When it fires, the sender transmits a small window probe. The receiver answers with an acknowledgment that carries its current window. If the window is still 0, the sender backs off and probes again later.
The window scale option
A 16-bit field limits the window to 65,535 bytes. On a path with a high bandwidth-delay product, that is too small to keep the link busy. For example, at 1 Gbit/s with a 100 ms round-trip time, about 12.5 MB must be in flight to fill the path.
The window scale option (RFC 7323) fixes this. Both sides include it in their SYN segments during the handshake. It gives a shift count, and the real window is the header value multiplied by 2 raised to that count. The maximum shift of 14 allows windows of about 1 GiB. The option is only negotiated in the SYN, so it cannot be added later in the connection.
Common errors
- Confusing flow control with congestion control. Flow control protects the receiver and uses the rwnd that the receiver advertises. Congestion control protects the network and uses a window that the sender computes itself. The next chapter covers it. The sender obeys the smaller of the two.
- Assuming a bigger socket buffer is always better. The buffer bounds the window. A buffer smaller than the bandwidth-delay product caps throughput, but very large buffers use memory for every connection.
- Ignoring a slow application. If a program reads from its socket too slowly, the window shrinks to 0 and the remote sender stalls. The network is fine, but throughput collapses.
- Treating the window as per-segment. It limits total unacknowledged bytes, not the size of one segment.
Observing the window in code
You can set the receive buffer size in Python with SO_RCVBUF. The operating system may adjust the value you ask for. Linux, for example, doubles it to allow for bookkeeping overhead.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536)
print(s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF))Set this before connect or listen. The window scale factor is chosen during the handshake, so a change made after the connection opens cannot raise the maximum scale.
Exercise
A receiver has a 8000-byte buffer. The sender has 5000 bytes in flight that are not yet acknowledged, and the receiver has acknowledged them all but the application has read none of them. What window does the receiver advertise, and can the sender send 4000 more bytes?
Show answer
The receiver advertises 8000 - 5000 = 3000 bytes, because the unread data still occupies the buffer. The sender cannot send 4000 more bytes. It may send at most 3000 until the application reads data and the receiver advertises a larger window.
Exercise
Why does a TCP sender need a persist timer when the receiver advertises a window of 0?
Show answer
The window update that reopens a zero window is a pure acknowledgment, and TCP does not retransmit those. If it is lost, the sender would wait forever. The persist timer makes the sender send window probes, which prompt the receiver to report its current window.