Chapters

TCP congestion control

Flow control protects the receiver. This chapter is about protecting everyone else: the shared links in between. TCP cannot see the network, so it has to guess how much it may send, and it does so with a few simple rules that keep the whole internet working.

Layers, routing and transport · chapter 20 of 34 · 7 min read

Why the network needs protecting

Links between hosts are shared. The Kadé office's 100 Mbit/s line to LankaNet carries the staff's browsing, a video support call, a nightly backup and every order sent to kade-api. When more arrives at a router than its outgoing link can carry, packets wait in the router's queue. When the queue is full, new packets are dropped.

Backup jobVideo callStaff browsingtogether: 180 Mbit/sOffice router · queuefull: the next arrivals are dropped✕100 Mbit/sLankaNet

Wide by design - scroll it sideways.

If every sender kept blasting, every drop would cause a resend, which adds more traffic, which causes more drops. In October 1986 this actually happened on the early internet: a link between two university sites fell from 32 kbit/s to about 40 bit/s, nearly all of it resends. TCP congestion control was added in response. It makes every sender regulate itself.

A second window: cwnd

TCP keeps a second limit, the congestion window (cwnd), separate from the receiver's rwnd. rwnd reflects the receiver's capacity and is told to the sender. cwnd is the sender's own estimate of what the network can carry, and nobody tells it. The sender must respect both.

rwnd (chapter 19)cwnd (this chapter)
ProtectsThe receiver's bufferThe network in between
Set byThe receiverThe sender, by guessing
Known howWindow field in every ACKWorked out from ACKs and losses
Seen on the wire?YesNo, it only exists inside the sender

Slow start

A new connection has no idea how much the network can carry, so it starts small and ramps up fast. In slow start, every ACK adds one segment to cwnd, so cwnd doubles every round trip. Despite the name, this is exponential growth: a quick probe upward until the first sign of trouble.

slow start (today)congestion avoidance: +1 per round trip0204060801000123456789101112round tripscwnd (segments)ssthresh = 80start at 10 segments (today)start at 1 (textbook)

Both lines double every round trip until ssthresh, then grow by one segment per round trip. Starting at 10 reaches full speed 3 to 4 round trips sooner, about 150 ms on the 41 ms path to kade-api.

Congestion avoidance

Once cwnd passes a threshold called ssthresh (slow start threshold), TCP switches to congestion avoidance: it grows cwnd slowly, by roughly one segment per round trip. This additive increase gently probes for more bandwidth without causing loss. In the graph above, the curve bends from steep to gentle at ssthresh.

Loss means "too much"

TCP cannot see router queues, so it treats packet loss as a sign of congestion. How it reacts depends on how the loss was noticed (chapter 17):

SignalWhat it suggestsReaction
3 duplicate ACKsOne packet was dropped, but later ones got through. Mild congestion.ssthresh = cwnd ÷ 2, cwnd = ssthresh. Carry on in congestion avoidance (fast recovery).
Retransmission timeoutNothing is getting through. Severe.ssthresh = cwnd ÷ 2, cwnd back to 1 segment, slow start again.
Cutting cwnd by a factor (halving) is the multiplicative decrease. It relieves the queue quickly, then growth resumes.

Watch it: a congestion window simulator

A connection from your laptop downloads a big sales report from kade-api. The path can carry about 40 segments per round trip before the queue overflows. Change the capacity, the algorithm, and whether a timeout happens:

Algorithm
Initial window
slow start010203040506070809001020304050607080round trips (41 ms each)cwnd (segments)✕✕✕✕cwndssthresh
  • dashed red line: path capacity (above it, the queue overflows)
  • amber ✕: loss seen by duplicate ACKs
  • red ✕: timeout
  • losses: 4
  • link used: about 74%

AIMD: the sawtooth, and why it is fair

Together, congestion avoidance and loss reaction give the classic AIMD pattern: Additive Increase, Multiplicative Decrease. Plotted over time, cwnd is a sawtooth: climb slowly, drop sharply on loss, climb again.

The surprising part: AIMD makes flows share fairly without talking to each other. Below, the backup job (already running with a big window) and a new download share the office's link. Each time the link overflows, both halve. Halving takes more from the bigger one, while both grow by the same +1, so their shares move together:

010203040506070010203040506070round tripscwnd (segments)fair share = 30 eachbackup job (started big)new download

The link fits 60 segments per round trip in total. Each overflow halves both flows. The gap between them halves too, while +1 each keeps it the same, so after a few cycles both sawtooth around the same share.

ECN: a warning instead of a drop

Waiting for a drop is wasteful: the data must be resent, and the queue was already full for a while. ECN (Explicit Congestion Notification) lets a router mark a packet as "congestion building" instead of dropping it. It is a mechanism that spans IP and TCP, not an algorithm:

StepWhere
1In the handshake, both sides agree to use ECN.TCP flags ECE and CWR in SYN / SYN-ACK (chapter 18)
2The sender marks its packets "ECN-capable".2 ECN bits in the IP header
3A router whose queue is building sets those bits to "congestion experienced" instead of dropping.IP header
4The receiver echoes the warning back on its ACKs.TCP flag ECE
5The sender cuts cwnd as if there had been a loss, and confirms it did.TCP flag CWR
Same slowdown, but no data lost and no resend needed. It only works if routers on the path support it; many now do.

The rule: min(cwnd, rwnd)

At any moment, the sender may have at most min(cwnd, rwnd) bytes in flight: the smaller of the network's limit and the receiver's limit. Both controls apply at once, and whichever is smaller is in charge:

kade-db's app busy: rwnd shrinks0102030405060700102030405060round tripssegmentsallowed in flight = mincwnd (network)rwnd (receiver)

Most of the time rwnd is large and cwnd (the network) is the limit. While the receiving app is busy, rwnd drops below cwnd and the receiver becomes the limit, even though the network could carry more. cwnd stays flat meanwhile: TCP does not grow a window it is not using.

This is a useful debugging question: when a transfer is slow, is it the receiver (small rwnd, growing Recv-Q, zero windows) or the network (small cwnd, retransmissions)? ss -ti shows both.

A little loss costs a lot

Because every loss halves cwnd, throughput depends heavily on the loss rate and on RTT. A well-known estimate (the Mathis formula) is about 1.22 × MSS ÷ (RTT × √loss rate). For your laptop to kade-api (MSS 1,460, RTT 41 ms):

0204060801000.001%0.01%0.1%0.5%1%2%5%packet loss ratebest speed (Mbit/s)>10034.811.04.93.52.51.6RTT 41 ms (home → kade-api)RTT 200 ms (a far region)

An estimate for loss-based TCP (Reno-style), capped at 100 Mbit/s for the chart. The farther path (dashed) suffers even more at the same loss rate.

Going from 0.01% to 1% loss cuts the best possible speed by 10× in this estimate. Longer RTTs make it worse, which is why long-distance transfers suffer most from small amounts of loss.

Bufferbloat: when queues are too big

Loss-based TCP keeps growing until a queue overflows. If a router has a very large queue, TCP fills it completely before seeing any loss, and every packet waits in line behind it. Throughput stays fine, but delay balloons. This is bufferbloat: the video support call lags badly the moment someone starts the backup upload.

backup upload running01002003004005006007000102030405060time (seconds)delay (ms)above ~150 ms, calls feel laggybig dumb queuewith fq_codel (AQM)

Measured with ping from the office while the backup uploads. The upload speed is about the same in both cases; only the waiting in the queue differs.

Fixes: smarter queues on routers (active queue management such as CoDel and fq_codel, which drop or mark early), ECN, and delay-aware algorithms like BBR (next section). On an office router, "SQM" settings turn these on.

Congestion control algorithms

The rules above are the classic ones. Real systems use refined algorithms, and you can choose them per machine:

#AlgorithmSignalHow it growsWhere
1Tahoe (1988)lossAny loss → back to 1 and slow startHistoric
2Reno / NewRenolossClassic AIMD with fast recovery (the simulator's "Reno")The textbook model
3CUBIClossAfter a loss, climbs quickly back toward the old peak, flattens near it, then probes past it. Growth depends on time, not RTT.Default on Linux, Windows and macOS
4BBRmeasured bandwidth and RTTEstimates the bottleneck bandwidth and the minimum RTT, and sends at that rate instead of filling queuesGoogle's servers; available on Linux
Only the sender's algorithm matters for a transfer. When kade-api sends a report to your laptop, kade-api's setting decides. BBR is often a good choice for servers talking to users over long, slightly lossy paths.

What's next

This completes TCP's inner workings. Chapter 21 looks at the same TCP from kade-api's code: sockets, queues, Nagle and the timeouts that kill idle connections. Then Section 2 is complete.

Summary

#IdeaIn one lineNew terms
1WhyShared links, full queues, drops; senders must self-regulatequeue, congestion collapse
2cwndThe sender's own estimate of network capacitycongestion window
3Slow startDoubles each RTT from 10 segmentsslow start, initial window
4AvoidanceAfter ssthresh, +1 segment per RTTssthresh, congestion avoidance
5LossDup ACKs → halve; timeout → back to 1fast recovery, multiplicative decrease
7AIMDThe sawtooth; converges to fair sharesAIMD, fairness
8ECNRouters mark instead of drop; sender slows without lossECN, ECE, CWR
9The ruleIn flight ≤ min(cwnd, rwnd)
10-11CostsSmall loss rates cap speed; oversized queues add delayMathis formula, bufferbloat, AQM
12AlgorithmsReno, CUBIC (default), BBRCUBIC, BBR

Try it yourself

The algorithm in use

sysctl net.ipv4.tcp_congestion_control

Algorithms you can switch to

sysctl net.ipv4.tcp_available_congestion_control

Live cwnd, ssthresh, RTT per connection

  • What to look for: cwnd:10 on fresh connections, larger on busy ones; ssthresh, retrans, delivery_rate.
  • Runs on: Linux
  • From section: 9. The rule: min(cwnd, rwnd)
ss -ti

A throughput test

iperf3 -c SERVER -t 10

Switch to BBR

sudo sysctl -w net.ipv4.tcp_congestion_control=bbr

Bufferbloat on your own line

ping 1.1.1.1

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