TExcelTAD
Home › Computer Networking › TCP Flow & Congestion Control

TCP Flow & Congestion Control

TCP limits how fast it sends for two different reasons:

  • Flow control protects the receiver: never send more than its buffer can hold. The receiver advertises this limit as the receive window (rwnd) in every segment.
  • Congestion control protects the network: if every sender pushed as fast as it could, router queues would overflow, packets would be dropped, everyone would retransmit, and throughput would collapse. (This really happened on the Internet in 1986, and led to Van Jacobson's congestion control algorithms.) The sender keeps its own estimate, the congestion window (cwnd).

At any moment a sender may have at most min(cwnd, rwnd) unacknowledged bytes in flight.

Flow control: the receive window

python · try it yourself
buffer_size = 8000
buffered = 0                      # bytes received but not yet read by the application

def advertise():
    return buffer_size - buffered

events = [("recv", 3000), ("recv", 3000), ("app reads", 2000), ("recv", 4000),
          ("recv", 1000), ("app reads", 8000)]
for kind, n in events:
    if kind == "recv":
        allowed = advertise()
        accepted = min(n, allowed)
        buffered += accepted
        extra = f" (sender had to hold back {n - accepted})" if accepted < n else ""
        print(f"receive {n:>4} bytes -> buffered {buffered:>4}, advertise window {advertise():>4}{extra}")
    else:
        read = min(n, buffered)
        buffered -= read
        print(f"app reads {read:>4} bytes -> buffered {buffered:>4}, advertise window {advertise():>4}")
receive 3000 bytes -> buffered 3000, advertise window 5000
receive 3000 bytes -> buffered 6000, advertise window 2000
app reads 2000 bytes -> buffered 4000, advertise window 4000
receive 4000 bytes -> buffered 8000, advertise window    0
receive 1000 bytes -> buffered 8000, advertise window    0 (sender had to hold back 1000)
app reads 8000 bytes -> buffered    0, advertise window 8000

When the window reaches 0 the sender stops and sends small window probes until the receiver opens it again. A server whose application is slow to read its sockets shows exactly this in packet captures as "TCP ZeroWindow".

Congestion control: AIMD

Classic TCP congestion control (TCP Reno) has these phases:

  1. Slow start: cwnd starts small (10 segments on modern Linux) and doubles every RTT (each ACK adds one segment). Despite the name, this is exponential growth.
  2. When cwnd reaches the slow-start threshold (ssthresh), switch to congestion avoidance: add just one segment per RTT (additive increase).
  3. On loss detected by 3 duplicate ACKs: halve the window (ssthresh = cwnd / 2, cwnd = ssthresh) and continue in congestion avoidance (fast recovery). This is the multiplicative decrease.
  4. On a timeout (a much worse sign): ssthresh = cwnd / 2 and cwnd = 1, back to slow start.

This Additive Increase, Multiplicative Decrease (AIMD) pattern produces TCP's famous sawtooth.

python · try it yourself
def reno(rounds, losses, timeouts, cwnd=1, ssthresh=16):
    history = []
    for rtt in range(1, rounds + 1):
        if rtt in timeouts:
            ssthresh, cwnd, phase = max(cwnd // 2, 2), 1, "TIMEOUT -> slow start"
        elif rtt in losses:
            ssthresh = max(cwnd // 2, 2)
            cwnd, phase = ssthresh, "3 dup ACKs -> halve"
        elif cwnd < ssthresh:
            cwnd, phase = min(cwnd * 2, ssthresh), "slow start"
        else:
            cwnd, phase = cwnd + 1, "congestion avoidance"
        history.append(cwnd)
        print(f"RTT {rtt:>2}: cwnd {cwnd:>2} ssthresh {ssthresh:>2}  {'#' * cwnd:<24} {phase}")
    return history

history = reno(rounds=22, losses={10, 17}, timeouts={20})
RTT  1: cwnd  2 ssthresh 16  ##                       slow start
RTT  2: cwnd  4 ssthresh 16  ####                     slow start
RTT  3: cwnd  8 ssthresh 16  ########                 slow start
RTT  4: cwnd 16 ssthresh 16  ################         slow start
RTT  5: cwnd 17 ssthresh 16  #################        congestion avoidance
RTT  6: cwnd 18 ssthresh 16  ##################       congestion avoidance
RTT  7: cwnd 19 ssthresh 16  ###################      congestion avoidance
RTT  8: cwnd 20 ssthresh 16  ####################     congestion avoidance
RTT  9: cwnd 21 ssthresh 16  #####################    congestion avoidance
RTT 10: cwnd 10 ssthresh 10  ##########               3 dup ACKs -> halve
RTT 11: cwnd 11 ssthresh 10  ###########              congestion avoidance
RTT 12: cwnd 12 ssthresh 10  ############             congestion avoidance
RTT 13: cwnd 13 ssthresh 10  #############            congestion avoidance
RTT 14: cwnd 14 ssthresh 10  ##############           congestion avoidance
RTT 15: cwnd 15 ssthresh 10  ###############          congestion avoidance
RTT 16: cwnd 16 ssthresh 10  ################         congestion avoidance
RTT 17: cwnd  8 ssthresh  8  ########                 3 dup ACKs -> halve
RTT 18: cwnd  9 ssthresh  8  #########                congestion avoidance
RTT 19: cwnd 10 ssthresh  8  ##########               congestion avoidance
RTT 20: cwnd  1 ssthresh  5  #                        TIMEOUT -> slow start
RTT 21: cwnd  2 ssthresh  5  ##                       slow start
RTT 22: cwnd  4 ssthresh  5  ####                     slow start

You can see all four phases: exponential growth to 16, linear growth, halving at each triple-duplicate-ACK loss, and the drop to 1 after the timeout.

Why AIMD is fair

When two flows share a bottleneck, AIMD makes them converge to an equal share. Additive increase raises both windows by the same amount; multiplicative decrease cuts the bigger one by more. Watch two flows that start very unequal:

python · try it yourself
capacity = 40           # segments per RTT the bottleneck can carry
a, b = 30, 4
for rtt in range(1, 31):
    if a + b > capacity:            # the queue overflows: both flows see a loss
        a, b = a // 2, b // 2
        event = "loss: both halve"
    else:
        a, b = a + 1, b + 1
        event = ""
    if rtt % 3 == 0 or event:
        print(f"RTT {rtt:>2}: A={a:>2} B={b:>2}  difference {abs(a - b):>2}  {event}")
RTT  3: A=33 B= 7  difference 26
RTT  5: A=17 B= 4  difference 13  loss: both halve
RTT  6: A=18 B= 5  difference 13
RTT  9: A=21 B= 8  difference 13
RTT 12: A=24 B=11  difference 13
RTT 15: A=27 B=14  difference 13
RTT 16: A=13 B= 7  difference  6  loss: both halve
RTT 18: A=15 B= 9  difference  6
RTT 21: A=18 B=12  difference  6
RTT 24: A=21 B=15  difference  6
RTT 27: A=24 B=18  difference  6
RTT 28: A=12 B= 9  difference  3  loss: both halve
RTT 30: A=14 B=11  difference  3

The difference shrinks with every halving, so the two flows end up sharing the link almost equally.

Modern algorithms

Algorithm Signal Notes
Reno / NewReno packet loss the classic AIMD sawtooth
CUBIC packet loss default on Linux, Windows, macOS; grows cwnd as a cubic function of time since the last loss, so it fills fast, long links quickly
BBR (Google) measured bandwidth and RTT models the bottleneck instead of waiting for loss; used by Google, YouTube and many CDNs
ECN routers mark packets instead of dropping lets TCP slow down before loss happens
bash
sysctl net.ipv4.tcp_congestion_control
ss -ti dst 93.184.215.14

Sample output:

text
net.ipv4.tcp_congestion_control = cubic
ESTAB 0 0 192.168.1.20:51514 93.184.215.14:443
     cubic wscale:9,7 rto:212 rtt:11.6/2.1 mss:1448 cwnd:42 ssthresh:28 bytes_sent:18234 bytes_acked:18235 bytes_received:982144 send 41.9Mbps

ss -ti exposes the kernel's live view of a connection: its RTT estimate, RTO, congestion window and estimated send rate.

Bufferbloat

If a router has a huge buffer, loss-based TCP keeps filling it before it sees any drops. The link stays busy, but every packet waits in a long queue, so latency explodes: a video call stutters while someone uploads a file. The cure is active queue management (fq_codel, CAKE) on routers, which drops or marks packets early to keep queues short, and delay-aware algorithms like BBR.

python · try it yourself
link_mbps = 20
for queue_packets in [20, 100, 500, 2000]:
    queue_bytes = queue_packets * 1500
    delay_ms = queue_bytes * 8 / (link_mbps * 1e6) * 1000
    print(f"queue of {queue_packets:>4} full-size packets on a {link_mbps} Mbit/s uplink adds {delay_ms:6.0f} ms")
queue of   20 full-size packets on a 20 Mbit/s uplink adds     12 ms
queue of  100 full-size packets on a 20 Mbit/s uplink adds     60 ms
queue of  500 full-size packets on a 20 Mbit/s uplink adds    300 ms
queue of 2000 full-size packets on a 20 Mbit/s uplink adds   1200 ms

Test yourself

Which window does the receiver advertise to the sender?

  1. The congestion window
  2. The receive window (flow control)
  3. The slow-start threshold
  4. The MTU

During slow start, how does cwnd grow?

  1. By one segment per RTT
  2. It doubles every RTT
  3. It stays constant
  4. It halves every RTT

In TCP Reno, what happens to cwnd after a loss detected by three duplicate ACKs?

  1. It drops to 1
  2. It is halved
  3. It doubles
  4. Nothing