Sub topic 7
Flow control with the receiver window
This chapter assumes you know the TCP segment header, sequence and acknowledgement numbers, and the idea of a sliding window from the previous chapters. It also assumes you can compute with powers of two and with bandwidth and round-trip time (RTT).
What flow control solves
Flow control stops a fast sender from overrunning a slow receiver. It is a different problem from congestion control. Flow control protects the receiving host's buffer. Congestion control protects the network between the hosts. The receiver window described here handles only the first problem.
Each TCP receiver has a receive buffer. Incoming bytes wait there until the application calls recv. If the application reads slowly and the sender keeps sending, the buffer fills and new data would be dropped. TCP avoids this by letting the receiver tell the sender how much free space remains.
The receiver window field
Every TCP segment carries a 16-bit window field. It is the number of bytes the sender of that segment is willing to accept, counted from the acknowledgement number onward. This value is called the receiver window, written rwnd.
The receiver computes it as:
rwnd = RcvBuffer - (LastByteRcvd - LastByteRead)RcvBufferis the total size of the receive buffer.LastByteRcvdis the number of the last byte that arrived and was placed in the buffer.LastByteReadis the number of the last byte the application has read.
The sender keeps the amount of unacknowledged data within rwnd:
LastByteSent - LastByteAcked <= rwndBecause the window field is 16 bits wide, the largest value is 65535 bytes. Modern links need more. The window scale option is negotiated in the SYN segments and gives a shift count s from 0 to 14. The effective window is the header value multiplied by 2^s, so the maximum becomes about 1 GiB.
How the window moves
The window is a range of sequence numbers the sender may use. Three events change it:
- Closing: the left edge moves right when new data is acknowledged.
- Opening: the right edge moves right when the receiver's application reads data and free buffer space grows.
- Shrinking: the right edge moves left. This is discouraged, and a receiver should not do it.
The right edge is ack number + window. The sender may send up to that byte number but not past it.
A worked trace
Suppose the receive buffer is 4000 bytes and the application has not yet read anything.
| Event | Bytes in buffer | rwnd advertised |
|---|---|---|
| Connection starts | 0 | 4000 |
| 1000 bytes arrive | 1000 | 3000 |
| 2000 bytes arrive | 3000 | 1000 |
| 1000 bytes arrive | 4000 | 0 |
| Application reads 3000 bytes | 1000 | 3000 |
At rwnd = 0 the sender must stop sending data. The row after the application reads shows why a window update is needed. Without it, the sender would wait forever.
The zero window problem
A window update is a pure ACK, and pure ACKs are not retransmitted if lost. If the update is lost, the receiver believes it told the sender to resume, and the sender believes the window is still zero. Both wait, which is a deadlock.
TCP breaks the deadlock with the persist timer. When the sender sees a zero window, it starts this timer. When it expires, the sender sends a zero-window probe, a segment with one byte of data (or sometimes none). The receiver answers with an ACK that carries the current window. If the window is still zero, the sender restarts the timer with a longer interval. Probes continue as long as needed.
Silly window syndrome
Another failure mode comes from tiny windows. If the receiver advertises a window as soon as one byte is free, and the sender sends one byte at a time, each segment has 20 bytes of TCP header and 20 bytes of IP header for 1 byte of data. The link carries mostly header.
Two fixes exist, one on each side.
- Clark's solution (receiver side): do not advertise a larger window until it has grown by at least min(MSS, RcvBuffer / 2). Until then, keep advertising 0.
- Nagle's algorithm (sender side): if there is unacknowledged data in flight, hold small writes in a buffer. Send them together when the ACK returns or when a full MSS has accumulated.
| Technique | Applies at | Behavior |
|---|---|---|
| Clark's algorithm | Receiver | Delays window advertisements until they are large enough |
| Delayed ACK | Receiver | Waits a short time to combine ACKs and window updates |
| Nagle's algorithm | Sender | Batches small writes while data is unacknowledged |
Sender window: flow control and congestion control together
The sender is limited by two windows at once. The effective window is:
W = min(rwnd, cwnd)Here cwnd is the congestion window, covered in the next chapter. The sender may have at most W bytes outstanding. When rwnd is the smaller value, the receiver is the bottleneck. When cwnd is smaller, the network is the bottleneck.
Throughput limit from the window
At most one window of data can be sent per RTT. Therefore:
Throughput <= W / RTTThis is the main numerical tool for this topic. The link rate also bounds throughput, so the achievable rate is min(W / RTT, link bandwidth).
The following program computes this bound and the window needed to fill a link, called the bandwidth-delay product.
def max_throughput_bps(window_bytes, rtt_s, link_bps):
"""Upper bound on TCP throughput in bits per second."""
window_limited = window_bytes * 8 / rtt_s
return min(window_limited, link_bps)
def bandwidth_delay_product_bytes(link_bps, rtt_s):
"""Window in bytes needed to keep the link busy."""
return link_bps * rtt_s / 8
# 64 KB window, 100 ms RTT, 100 Mbps link
w = 65535
rtt = 0.1
link = 100e6
print("Window-limited throughput (Mbps):",
max_throughput_bps(w, rtt, link) / 1e6)
print("Window needed to fill link (bytes):",
bandwidth_delay_product_bytes(link, rtt))The output shows about 5.24 Mbps against a 100 Mbps link. The window needed is 1,250,000 bytes. That is why the window scale option exists. With a 16-bit window and a 100 ms RTT, the connection uses about 5% of the link.
Worked GATE-style example
A TCP connection uses a receiver window of 20 KB. The RTT is 40 ms and the link rate is 10 Mbps. Find the maximum throughput. Take 1 KB as 1000 bytes.
- Window in bits: 20 × 1000 × 8 = 160,000 bits.
- Window-limited rate: 160,000 / 0.040 = 4,000,000 bits per second, or 4 Mbps.
- Compare with the link: 4 Mbps is less than 10 Mbps, so the window is the bottleneck.
- Link utilization: 4 / 10 = 40%.
If the question asks for the window that gives 100% utilization, compute the bandwidth-delay product: 10,000,000 × 0.040 / 8 = 50,000 bytes.
Common errors
- Mixing up rwnd and cwnd.
rwndcomes from the receiver in the header.cwndis kept privately by the sender and never sent. - Forgetting to take the minimum. The sending limit is
min(rwnd, cwnd), not either one alone. - Unit mistakes. Windows are in bytes and link rates are in bits. Convert before dividing.
- Counting the window as packets. The TCP window is in bytes. Divide by MSS only when the question asks for segments.
- Ignoring utilization limits. If
W / RTTexceeds the link rate, throughput is the link rate, not the window formula. - Believing the receiver can ask for more than its buffer. The advertised window never exceeds free buffer space.
- Confusing flow control with ACKs. The ACK number says what has arrived. The window field says how much more may be sent.
Exercise
A receiver has a 16 KB buffer (1 KB = 1024 bytes). It has received 10,240 bytes and the application has read 4,096 of them. What window does it advertise? Then the sender's cwnd is 3 KB. How many bytes can the sender have outstanding, and what happens if the receiver then advertises 0?
Show answer
Data held in the buffer is 10,240 - 4,096 = 6,144 bytes. Free space is 16,384 - 6,144 = 10,240 bytes, so rwnd = 10,240 bytes (10 KB). The sender may have min(10 KB, 3 KB) = 3 KB outstanding, so congestion control is the limit. If the receiver advertises 0, the sender stops sending new data and starts the persist timer. It sends one-byte zero-window probes until the receiver advertises a nonzero window.
Exercise
A TCP connection has a 32 KB window (1 KB = 1000 bytes), an RTT of 80 ms, and runs on a 20 Mbps link. What is the maximum throughput, and what window size would fully use the link?
Show answer
Window-limited rate = 32,000 × 8 / 0.080 = 3,200,000 bps = 3.2 Mbps, which is below 20 Mbps, so throughput is 3.2 Mbps. Window for full use = 20,000,000 × 0.080 / 8 = 200,000 bytes, or 200 KB.
Exercise
Why can a lost window update cause a deadlock, and which TCP mechanism prevents it?
Show answer
A window update is a pure ACK, which TCP does not retransmit. If it is lost, the receiver waits for data while the sender waits for a nonzero window. The sender's persist timer fires and sends a zero-window probe. The receiver's reply carries the current window and ends the deadlock.