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.
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 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 →
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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 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:
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
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1 | Problem | Data waits in a receive buffer until the app reads it; it can fill | receive buffer |
| 2 | rwnd | Free buffer space = most unacked data allowed in flight | receive window |
| 3 | Advertised | Window field in every segment; the sender must obey it | |
| 4 | Sliding | ACKs move the window right: done · in flight · may send · wait | sliding window |
| 5 | Scaling | 16-bit cap of 64 KB; window scale × 2n; window ≥ BDP to fill a link | window scale, BDP, auto-tuning |
| 6 | Zero window | Stop; the sender probes on a persist timer | zero window, window probe, silly window syndrome |
| 7 | In code | write() returning false → wait for 'drain' | backpressure |
| 8 | Not congestion | rwnd 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 -tnBuffers and windows
- What to look for:
skmem:(r…,rb…)buffer use and size,rcv_space,snd_wnd. - Runs on: Linux
- From section: 5. Window scaling and the bandwidth-delay product
ss -tmiAdvertised windows live
- What to look for: The
winvalue on every line. - From section: 3. Advertised in every segment
sudo tcpdump -n -i en0 host example.comFlow 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_fullAuto-tuning limits and scaling
- What to look for: Three numbers: min, default, max buffer; scaling = 1 (on).
- Runs on: Linux
- From section: 5. Window scaling and the bandwidth-delay product
sysctl net.ipv4.tcp_rmem net.ipv4.tcp_window_scaling