Search

Search papers and lessons

Flow Control

Level 3 · 12 min read

Flow control lets a receiver slow a fast sender down, by telling it how much free buffer space it has, so that arriving data never overflows the receiver's memory.

On this page

  • Colour = topic mix
  • Discrete math
  • Networking
  • Algorithms
  • Programming languages
  • Abstract algebra
  • ReadReady to readNot yet

TL;DR

A fast sender can send data faster than a slow receiver can read it. The data then piles up in the receiver's memory until that memory is full, and the rest is lost. Flow control prevents this: the receiver tells the sender how much free buffer space it has, and the sender never has more data outstanding than that space allows. The rule protects the receiver. It says nothing about the network in between, which is the job of Congestion Control.

Builds on: Automatic Repeat Request · Leads to: Congestion Control

Where it's used

The TCP receive window. Every TCP segment carries a window field that states how many more bytes the receiver can accept. A laptop downloading a large file from a server receives the window in each acknowledgement, and the server never has more unacknowledged bytes in flight than that. The field is 16 bits wide, so without the window-scaling option it can express at most 65,535 bytes.

Ethernet PAUSE frames. A network switch whose output buffer is filling can send a PAUSE frame to the device on the other end of the cable, asking it to stop sending for a set time. The standard IEEE 802.3x, published in 1997, defines this link-level form of flow control, so a busy switch can slow its neighbours before its memory overflows.

Pipes and bounded queues. A program that writes into a Unix pipe blocks when the pipe's buffer is full, and continues only after the reading program has taken data out. The same idea is the core of the Producer-Consumer Problem: a buffer of fixed size makes the fast producer wait for the slow consumer.

How it works

A buffer is a fixed block of memory where arriving data waits until the program reads it. Its size is BB packets. The receiver's application reads packets from the buffer in order. The example uses B=4B = 4, and sends numbered packets. The receiver's acknowledgements are cumulative, as in Automatic Repeat Request: an acknowledgement "next 5" means packets 1 to 4 arrived in order.

Start with an empty buffer, B=4B = 4, and nothing read yet.

  1. Advertise the room. The receiver has 4 free slots, so it tells the sender that it can accept 4 packets. The sender may have at most 4 packets outstanding. Check: the sender's allowance is the buffer size, because nothing is waiting to be read.

  2. The sender fills the room. The sender transmits packets 1, 2, 3 and 4. All four arrive in order and wait in the buffer, because the application has not read any of them yet. The buffer is full. The receiver acknowledges "next 5" with window 0. Check: the sender must now stop, even though the network could carry more.

  3. The application reads two packets. The program takes packets 1 and 2 out of the buffer. Two slots are free. The receiver sends a window update: "next 5, window 2". Check: the window reopened because the program read data, not because more data arrived.

  4. The sender uses the new room. The sender transmits packets 5 and 6. The buffer now holds packets 3, 4, 5 and 6, which is full again. Check: the sender sent exactly the 2 packets the window allowed.

  5. A slow reader closes the window again, then opens it. The program reads nothing for a while, and the receiver acknowledges with window 0. The sender waits holding packets 7 and 8. Then the program reads packets 3 and 4. Two slots are free, and the receiver sends "next 7, window 2". The sender transmits packets 7 and 8. Check: the sender was held back during the stall and resumed as soon as room appeared.

The plot shows the window the receiver advertised after each numbered step.

1234501234stepwindow (packets)
  • advertised window (packets)
The window falls to zero when the buffer fills, reopens when the program reads, and falls to zero again when the sender refills the buffer.

The math

Definition: Flow control

Let BB be the buffer size in packets. Let AA be the highest packet number the application has read, and LL the highest packet number received in order, so the packets waiting in the buffer are L−AL - A. The receiver advertises the window W=B−(L−A)W = B - (L - A). The sender's highest sent packet number SS must satisfy S≤L+WS \le L + W.

Because W=B−(L−A)W = B - (L - A), the rule can also be written as S≤L+B−L+A=A+BS \le L + B - L + A = A + B. The sender may never be more than BB packets ahead of what the application has read. The receiver's memory is therefore never asked to hold more than BB packets.

Step 1 in symbols: the first allowance

Nothing has been read and nothing has arrived, so A=L=0A = L = 0 and W=B=4W = B = 4. The sender may send up to S≤0+4=4S \le 0 + 4 = 4.

Step 2 in symbols: the buffer fills

After packets 1 to 4 arrive, L=4L = 4 and A=0A = 0. The window is W=4−(4−0)=0W = 4 - (4 - 0) = 0. The sender's limit is S≤L+W=4S \le L + W = 4, which it has reached.

Step 3 in symbols: the reader moves the edge

The program reads two packets, so A=2A = 2. The window is W=4−(4−2)=2W = 4 - (4 - 2) = 2. The limit is S≤L+W=4+2=6S \le L + W = 4 + 2 = 6.

Step 4 in symbols: the check

The sender sends packets 5 and 6, so S=6S = 6 and L=6L = 6. The window is W=4−(6−2)=0W = 4 - (6 - 2) = 0, and the rule S≤A+B=2+4=6S \le A + B = 2 + 4 = 6 holds with equality.

Step 5 in symbols: the long run

The sender can only move its limit forward when the reader moves AA forward, because the limit is A+BA + B. Over a long transfer, the sender's position SS therefore cannot run ahead of the reader's position AA by more than BB. If the reader consumes rr packets per second, the sender's average rate also converges to rr once the buffer is full, however fast the link is. Ignoring the round-trip delay, which only shifts the timing, the rate is limited by the reader.

Worked examples

Example
1. A buffer of three packets (tiny)

The buffer holds B=3B = 3 packets. So far the receiver has received packets 1 to 3, L=3L = 3, and the program has read only packet 1, A=1A = 1.

  1. The packets waiting in the buffer are L−A=3−1=2L - A = 3 - 1 = 2.
  2. The window is W=B−2=3−2=1W = B - 2 = 3 - 2 = 1.
  3. The sender may send up to S≤L+W=3+1=4S \le L + W = 3 + 1 = 4.
  4. Interpretation: the sender may send one more packet, packet 4, and then must wait. The window is the free space, and it is one slot.
Example
2. A reader that drains ten packets per second (standard)

The buffer holds B=20B = 20 packets. The sender has a large file to send over a fast link, and the program reads 10 packets per second, so r=10r = 10. Ignore the round-trip delay, which only shifts the timing.

  1. At the start the sender may send A+B=0+20=20A + B = 0 + 20 = 20 packets, so it sends a burst of 20.
  2. After 1 second the program has read 10 packets, so A=10A = 10. The sender may now reach A+B=10+20=30A + B = 10 + 20 = 30. It sends 10 more.
  3. After 10 seconds the program has read A=100A = 100 packets, so the sender may reach A+B=120A + B = 120. In those 10 seconds it sent 120 packets, in total.
  4. Compute the average rate over those 10 seconds: 120/10=12120 / 10 = 12 packets per second. The initial burst of 20 inflates this figure. Each later second adds only the packets the program reads, which is 10, so the rate settles at r=10r = 10 packets per second once the burst is spent.
  5. Interpretation: the link can carry much more, but the sender's speed is set by how fast the program reads. The 20-packet buffer only lets the sender run ahead by a fixed amount.
Example
3. A 65,535-byte window on a 50 ms path (realistic)

A TCP receiver advertises a window of W=65,535W = 65{,}535 bytes, and the round-trip time is RTT=50\mathrm{RTT} = 50 ms. The application reads data as fast as it arrives, so the window is never the limit on reading.

  1. The window allows WW bytes per round trip, so the window-limited rate is W/RTT=65,535/0.05=1,310,700W / \mathrm{RTT} = 65{,}535 / 0.05 = 1{,}310{,}700 bytes per second.
  2. Convert to bits: 1,310,700×8=10,485,6001{,}310{,}700 \times 8 = 10{,}485{,}600 bit/s, which is about 10.5 Mbit/s.
  3. Time a 10,000,000-byte download: 10,000,000/1,310,700≈7.610{,}000{,}000 / 1{,}310{,}700 \approx 7.6 seconds.
  4. Interpretation: on this path a plain 65,535-byte window caps the speed at about 10.5 Mbit/s, even on a much faster link. A larger window, enabled by window scaling, raises the cap.

Try it: the same 65,535-byte window, but with RTT=20\mathrm{RTT} = 20 ms. What is the window-limited rate in Mbit/s?

Answer: 65,535/0.02=3,276,75065{,}535 / 0.02 = 3{,}276{,}750 bytes per second. Multiply by 8 to get 26,214,00026{,}214{,}000 bit/s, which is about 26.2 Mbit/s. The shorter round trip allows 2.5 times the rate, because the same window is sent more often.

Common mistakes and tradeoffs

It is tempting to think that flow control and Congestion Control are the same thing. In fact they protect different resources. Flow control keeps the sender from overflowing the receiver's buffer, and the receiver advertises that space. Congestion control keeps the sender from overflowing the network's queues, and the sender infers that from loss or delay, often by a rule such as AIMD. A real TCP sender obeys both limits and uses the smaller of the two.

It is tempting to think that a large window always makes a transfer faster. In fact the window only helps until either the receiver's reading speed or the link's capacity becomes the limit. Beyond that point, extra window only adds memory use.

It is tempting to think that a window of zero means the connection is dead. In fact it means the receiver is busy, and the sender must wait. TCP uses a persist timer to send occasional probes, so the sender learns when the window reopens even if the receiver's update was lost.

Use flow control when one end is much slower than the other, or when the receiver's memory is limited, as on a phone or an embedded device. It is not needed when the receiver can always consume data as fast as it arrives. Alternatives include credit-based schemes, where the receiver grants permission in units of messages rather than bytes, and pause frames, which act hop by hop instead of end to end, one link at a time as Protocol Layering describes. Each has the same goal: the sender's rate follows the receiver's capacity.

History

Early computer networks, such as the ARPANET in the 1970s, moved data between machines that often ran at very different speeds. A fast host could flood a slow one, and the slow one had no way to say stop except by dropping data and making the sender resend it. That wasted both link capacity and time.

The TCP specification, first published in 1981 as RFC 793, included a window field in each segment, so a receiver could state how much buffer it had. The idea was then carried into RFC 9293, the 2022 revision of TCP. Flow control became a standard part of reliable transport because it solved a problem that retransmission alone could not: resending a packet into a full buffer just loses it again.

Recap

  • A receiver's buffer has a fixed size BB. Flow control makes the sender stay within BB packets of what the program has read.
  • The receiver advertises a window W=B−(packets waiting)W = B - (\text{packets waiting}), and the sender may send up to WW more.
  • The window opens when the program reads and closes when the buffer fills, so the sender's speed follows the reader's speed.
  • Flow control protects the receiver. Congestion Control protects the network, and a real sender obeys both.

What to read next: Congestion Control shows how a sender limits itself to the network's capacity, and Head-of-Line Blocking shows what happens when the in-order buffer holds back data that is already present.

Check yourself

QuestionRecall: what does the receiver advertise in flow control, and what does the sender do with it?Show answer
The receiver advertises its free buffer space, the window. The sender may have at most that many packets outstanding beyond what has been acknowledged, and it must stop when the window is zero.
QuestionComputation: a receiver's buffer holds 8 packets. Its last in-order packet is 10, and the program has read through packet 7. What is the advertised window, and what is the highest packet number the sender may send?Show answer
The packets waiting are 10−7=310 - 7 = 3, so the window is 8−3=58 - 3 = 5. The sender may send up to 10+5=1510 + 5 = 15, which equals A+B=7+8=15A + B = 7 + 8 = 15.
QuestionWhy: a sender still has packets to send and the network has spare capacity, but the sender stops because the window is zero. Why is that the right behaviour?Show answer
Sending into a full buffer would only lose the packets, since the receiver has nowhere to put them. Waiting costs time, but it avoids wasted transmissions and the resends they would cause. The window tells the sender exactly when room has appeared.
QuestionApplied: a video call runs over TCP, and the viewer's phone is slow to process frames. The sender's congestion window is large, but the video stalls. Which limit is probably active, and what would you check?Show answer
The receiver's advertised window is probably the active limit, because the phone reads data slowly and its buffer fills. You would check the window field in the acknowledgements. A window of zero or a small value means flow control is limiting the sender, so the fix lies on the receiver's side.

Sources