Routelearn.net
Course menu

Unit 7: TCP, UDP and PortsLesson 7.1.5 (5 of 20 in this unit)38 of 84 in the Network Fundamentals course

Windowing and flow control

How the receiver tells the sender how much it can take, and how TCP backs off when the network is busy.

Intermediate · 7 min read

TCP windowing is the mechanism in which the receiver advertises a window size (the number of bytes it can still accept), and the sender may have up to that many unacknowledged bytes in flight before it must wait for an acknowledgement. By changing the window, the receiver controls the flow of data, so a fast sender cannot overrun a slow receiver.

In simple terms: Instead of waiting for a reply after every piece, the sender may send several pieces in a row. The receiver says how much room it has left, and the sender never sends more than that.

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.

SenderRouterReceiverwindow 5840 (4 × 1460)
  1. 1. Send the whole window. The receiver offered 5840 bytes, so the sender sends four 1460-byte segments without waiting.
  2. 2. ACK and a new window. The receiver acknowledges all four segments and says it still has 5840 bytes free.
  3. 3. The window slides forward. The sender sends the next four segments. This is why it is called a sliding window.
  4. 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 controlCongestion control
ProtectsThe receiverThe network path
Who sets the limitThe receiver, in the window fieldThe sender, based on what it observes
Signal to slow downA smaller window is advertisedLost 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.

Example output · based on Cisco documentation; exact format varies by platform and software version
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):
What to look for: 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

Predict · scenario 1

A phone downloading a large file is busy, and its TCP stack advertises a window of 0. What does the server do?

Predict · scenario 2

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?

Predict · scenario 3

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?