Chapters

TCP flow control

TCP can send data faster than some programs can read it. Flow control is how the receiver says "slow down, I'm full", using a number carried in every segment. The same idea shows up in your own code as backpressure.

Layers, routing and transport · chapter 19 of 34 · 5 min read

The problem: a buffer that can overflow

When data arrives, the OS does not hand it straight to the app. It puts it in the connection's receive buffer, where it waits until the app reads it. If the sender pushes data faster than the app reads, the buffer fills up and overflows, and data is lost. Flow control prevents this.

Laptopuploading 20 photossegmentskade-api (the receiver)receive buffer for this connectionwaiting to be read · 48 KBfree · 16 KBNode appreadsTCP writes in from the left as segments arrive. The app reads fromthe right when it calls read().If the app is busy (for example, a slow image resize), the amber partgrows until nothing is free.

Wide by design - scroll it sideways.

The receive window (rwnd)

The receiver tracks how much free space is left in its buffer and calls it the receive window (rwnd). It is the most unacknowledged data the sender may have in flight at once.

rwnd = buffer size − data not yet read by the app = 64 KB − 48 KB = 16 KB

Advertised in every segment

The receiver puts its current window in the Window field of every segment it sends back (chapter 18). So with each ACK, the sender learns both what arrived and how much more it may send. It must never go beyond the advertised window. In tcpdump it looks like this:

IP 34.87.120.15.443 > 192.168.1.23.52814: Flags [.], ack 48001, win 16384
IP 34.87.120.15.443 > 192.168.1.23.52814: Flags [.], ack 60001, win 4096
IP 34.87.120.15.443 > 192.168.1.23.52814: Flags [.], ack 64001, win 0
The ACKs move forward while the window shrinks: data is arriving faster than kade-api's app reads it. The last line says "stop".

The sliding window

From the sender's side, the byte stream is split into four regions. As ACKs arrive, the window moves right and lets new data go out, which is why it is called a sliding window:

← already done|← the window = rwnd →|later →

sent and ACKedsent, not yet ACKed (in flight)may be sent nowmust wait

Try it. The laptop uploads 16 KB-sized pieces to kade-api, whose receive buffer holds 8 of them. Send, deliver, and let the app read, in any order. Leave the app idle to see the window close:

laptop’s upload: 16 pieces

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  • sent + ACKed
  • in flight
  • may send now
  • must wait

kade-api’s receive buffer (8 pieces) · filled = waiting for the app

in flight: 0advertised window: 8unread in buffer: 0

The laptop may send up to 8 pieces (the advertised window).

Window scaling and the bandwidth-delay product

The Window field is only 16 bits, so it caps at 65,535 bytes (64 KB). At most one window can be in flight per round trip, so speed ≤ window ÷ RTT (chapter 18). To keep a link full, the window must be at least the bandwidth-delay product (BDP): how much data fits "in the pipe" during one round trip.

Bandwidth-delay product
500 KB must be in flight to fill the link
Best speed with a 64 KB window
12.8 Mbit/s of 100
Window scale shift needed
3 (× 8)

The window scale option fixes the 64 KB cap. In the handshake each side announces a shift count (0 to 14); the real window is the Window field × 2shift. The SYN in chapter 18 offered shift 7 (× 128), so a Window field of 16,384 means 2 MB. The largest possible window is about 1 GB.

  • Both sides must offer it, or neither uses it. A firewall that strips the option from SYNs quietly limits every connection to 64 KB windows: long-distance transfers become slow for no visible reason.
  • Buffers grow on their own. Modern OSes auto-tune the receive buffer, starting small and growing it (up to several MB) when a connection is fast and busy.

Zero window and window probes

If the receiver's buffer is full, it advertises Window = 0: stop. The sender then waits, but it cannot just wait forever: if the "window open again" message were lost, both sides would wait for each other. So the sender starts a persist timer and sends tiny window probes to ask whether space has opened up:

Laptop (uploading)kade-api (busy app)data · 16 KBACK · win 0buffer full: the Node app has not read anythingthe laptop stops sending and starts its persist timerwindow probe (1 byte)ACK · win 0still fullthe probe gap doubles; meanwhile the app reads 32 KBwindow update · ACK · win 32768"room again"data resumes

The same idea in your code: backpressure

Flow control does not stop at the OS. If kade-api writes a large CSV export to a slow client, the client's window closes, kade-api's send buffer fills, and the OS stops accepting writes. Node tells you this with the return value of write():

for (const row of rows) {
  const ok = res.write(toCsvLine(row));
  if (!ok) await once(res, 'drain');  // buffer full: wait until TCP has room
}
res.end();

Ignore false and keep writing, and Node queues everything in memory: one slow phone can make kade-api use hundreds of MB. Streams with pipe() or pipeline() handle this for you. This chain, from app to OS buffer to TCP window and back, is called backpressure.

It also works the other way. If kade-api's Node event loop is blocked by heavy work, it stops reading from its sockets, its receive buffers fill, and clients' uploads pause at Window = 0. That is TCP flow control doing its job, and a useful clue when uploads "freeze".

Flow control is not congestion control

Flow control · rwnd

Protects the receiver: its buffer.

The receiver sets the limit and tells the sender in every segment.

Congestion control · cwnd

Protects the network: the links in between.

The sender guesses the limit itself; nobody tells it. Next chapter.

Same goal, smooth delivery, but different bottlenecks. The sender obeys both at once: chapter 20 ends with the rule that joins them.

Summary

#IdeaIn one lineNew terms
1ProblemData waits in a receive buffer until the app reads it; it can fillreceive buffer
2rwndFree buffer space = most unacked data allowed in flightreceive window
3AdvertisedWindow field in every segment; the sender must obey it
4SlidingACKs move the window right: done · in flight · may send · waitsliding window
5Scaling16-bit cap of 64 KB; window scale × 2n; window ≥ BDP to fill a linkwindow scale, BDP, auto-tuning
6Zero windowStop; the sender probes on a persist timerzero window, window probe, silly window syndrome
7In codewrite() returning false → wait for 'drain'backpressure
8Not congestionrwnd protects the receiver; cwnd protects the network

Try it yourself

Per connection: Recv-Q and Send-Q

  • What to look for: Recv-Q = bytes in the buffer the app has not read yet. Growing on kade-api = the app is not keeping up.
  • Runs on: Linux · macOS: netstat -an -p tcp
  • From section: 1. The problem: a buffer that can overflow
ss -tn

Buffers and windows

ss -tmi

Advertised windows live

sudo tcpdump -n -i en0 host example.com

Flow control in action

  • Steps: Wireshark filter tcp.analysis.zero_window || tcp.analysis.window_full
  • What to look for: "TCP ZeroWindow", "TCP Window Full", "ZeroWindowProbe".
  • From section: 6. Zero window and window probes
tcp.analysis.zero_window || tcp.analysis.window_full

Auto-tuning limits and scaling

sysctl net.ipv4.tcp_rmem net.ipv4.tcp_window_scaling

Spotted a mistake, or something unclear? Suggest an edit on GitHub