A situation
Suppose you send a long letter one page at a time and wait for a “got it” reply after every page before you post the next. It would take far too long. Now suppose you post all 500 pages at once to a friend with a tiny letterbox. Most of them would end up in the rain. TCP needs a middle way: send several pieces at once, but never more than the other side can accept.
What it is
The window is the number of bytes a sender may send before it must stop and wait for an acknowledgement. The receiver sets it: every segment it sends back carries a window size field that says “I have this much free buffer space right now”. A buffer is memory where received data waits until the application reads it.
- 1. Send the whole window. The receiver offered 5840 bytes, so the sender sends four 1460-byte segments without waiting.
- 2. ACK and a new window. The receiver acknowledges all four segments and says it still has 5840 bytes free.
- 3. The window slides forward. The sender sends the next four segments. This is why it is called a sliding window.
- 4. Receiver full. The application is slow to read the data, so the window drops to zero. The sender pauses until a larger window is advertised.
Flow control
Using the window to protect the receiver is called flow control. A fast server cannot flood a slow phone, because the phone keeps shrinking the window as its buffer fills. A window of 0 means “stop sending”. The sender then sends a tiny window probe from time to time to check whether space has become free.
The window field is only 16 bits, so on its own it can describe at most 65,535 bytes. On fast, long-distance links that is far too small. The window scale option, agreed in the handshake, multiplies the value by a power of two, allowing windows of up to about 1 GB.
Congestion control
Flow control protects the receiver. But the network in between can also be overloaded, for example when a router's queue is full. So the sender keeps a second limit of its own, the congestion window. It may only have as much data in flight as the smaller of the two windows allows.
| Flow control | Congestion control | |
|---|---|---|
| Protects | The receiver | The network path |
| Who sets the limit | The receiver, in the window field | The sender, based on what it observes |
| Signal to slow down | A smaller window is advertised | Lost segments (or ECN marks) |
The sender changes its congestion window in phases:
- Slow start: a new connection starts small and roughly doubles its window every round trip, to find the available speed quickly.
- Congestion avoidance: after a threshold (the slow start threshold), the window grows slowly, by about one segment per round trip.
- Back off: when a segment is lost, the sender takes it as a sign of congestion and cuts its window, often in half, then starts growing it again. After a retransmission timeout, it usually drops back to slow start.
Why it works this way
Waiting for an ACK after every segment wastes the time the data spends in flight; a window keeps the link busy. Letting the receiver set the window prevents its buffer from overflowing. Backing off on loss means that when many connections share a busy link, they all slow down a little and share it fairly, instead of all pushing until nothing gets through. This is also why UDP floods can hurt TCP users: UDP has no congestion control, so it does not back off.
How to verify it
On a Cisco router, show tcp shows the windows for each TCP session that ends on the router, here an SSH session. The output below is based on Cisco documentation, not captured from a lab device, and is shortened.
R1#show tcp Stand-alone TCP connection to host 10.1.1.50 Connection state is ESTAB, I/O status: 1, unread input bytes: 0 Local host: 10.1.1.1, Local port: 22 Foreign host: 10.1.1.50, Foreign port: 51544 iss: 2310155321 snduna: 2310158007 sndnxt: 2310158007 irs: 1754312908 rcvnxt: 1754313934 sndwnd: 64128 scale: 7 maxrcvwnd: 4128 rcvwnd: 4128 scale: 0 delrcvwnd: 0 SRTT: 98 ms, RTTO: 300 ms, RTV: 202 ms, KRTT: 0 ms Datagrams (max data segment is 1460 bytes):
sndwnd is the window the PC (10.1.1.50) advertised, so the router may send up to 64,128 bytes before it must wait for an ACK. rcvwnd is the window the router advertises back. iss and irs are the two initial sequence numbers from the handshake (sent and received). The last line shows the MSS: 1460 bytes.Check yourself
A phone downloading a large file is busy, and its TCP stack advertises a window of 0. What does the server do?
A receiver advertises a 64 KB window, but the sender's congestion window is only 16 KB because of recent loss. How much unacknowledged data can the sender have in flight?
Many users share a busy WAN link, and TCP senders start slowing down. What does TCP mainly treat as the sign that the network is congested?