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.
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) | |
|---|---|---|
| Protects | The receiver's buffer | The network in between |
| Set by | The receiver | The sender, by guessing |
| Known how | Window field in every ACK | Worked out from ACKs and losses |
| Seen on the wire? | Yes | No, 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.
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):
| Signal | What it suggests | Reaction |
|---|---|---|
| 3 duplicate ACKs | One packet was dropped, but later ones got through. Mild congestion. | ssthresh = cwnd ÷ 2, cwnd = ssthresh. Carry on in congestion avoidance (fast recovery). |
| Retransmission timeout | Nothing is getting through. Severe. | ssthresh = cwnd ÷ 2, cwnd back to 1 segment, slow start again. |
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:
- 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:
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:
| Step | Where | |
|---|---|---|
| 1 | In the handshake, both sides agree to use ECN. | TCP flags ECE and CWR in SYN / SYN-ACK (chapter 18) |
| 2 | The sender marks its packets "ECN-capable". | 2 ECN bits in the IP header |
| 3 | A router whose queue is building sets those bits to "congestion experienced" instead of dropping. | IP header |
| 4 | The receiver echoes the warning back on its ACKs. | TCP flag ECE |
| 5 | The sender cuts cwnd as if there had been a loss, and confirms it did. | TCP flag CWR |
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:
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):
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.
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:
| # | Algorithm | Signal | How it grows | Where |
|---|---|---|---|---|
| 1 | Tahoe (1988) | loss | Any loss → back to 1 and slow start | Historic |
| 2 | Reno / NewReno | loss | Classic AIMD with fast recovery (the simulator's "Reno") | The textbook model |
| 3 | CUBIC | loss | After 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 |
| 4 | BBR | measured bandwidth and RTT | Estimates the bottleneck bandwidth and the minimum RTT, and sends at that rate instead of filling queues | Google's servers; available on Linux |
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
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1 | Why | Shared links, full queues, drops; senders must self-regulate | queue, congestion collapse |
| 2 | cwnd | The sender's own estimate of network capacity | congestion window |
| 3 | Slow start | Doubles each RTT from 10 segments | slow start, initial window |
| 4 | Avoidance | After ssthresh, +1 segment per RTT | ssthresh, congestion avoidance |
| 5 | Loss | Dup ACKs → halve; timeout → back to 1 | fast recovery, multiplicative decrease |
| 7 | AIMD | The sawtooth; converges to fair shares | AIMD, fairness |
| 8 | ECN | Routers mark instead of drop; sender slows without loss | ECN, ECE, CWR |
| 9 | The rule | In flight ≤ min(cwnd, rwnd) | |
| 10-11 | Costs | Small loss rates cap speed; oversized queues add delay | Mathis formula, bufferbloat, AQM |
| 12 | Algorithms | Reno, CUBIC (default), BBR | CUBIC, BBR |
Try it yourself
The algorithm in use
- What to look for:
cubicby default. - Runs on: Linux, e.g. on kade-api
- From section: 12. Congestion control algorithms
sysctl net.ipv4.tcp_congestion_controlAlgorithms you can switch to
- What to look for:
reno cubic, andbbrif the module is loaded. - From section: 12. Congestion control algorithms
sysctl net.ipv4.tcp_available_congestion_controlLive cwnd, ssthresh, RTT per connection
- What to look for:
cwnd:10on fresh connections, larger on busy ones;ssthresh,retrans,delivery_rate. - Runs on: Linux
- From section: 9. The rule: min(cwnd, rwnd)
ss -tiA throughput test
- What to look for: The Retr and Cwnd columns each second.
- Runs on: run iperf3 -s on the other end
- From section: 6. Watch it: a congestion window simulator
iperf3 -c SERVER -t 10Switch to BBR
- What to look for: Compare iperf3 results before and after.
- Runs on: Linux, test machines only
- From section: 12. Congestion control algorithms
sudo sysctl -w net.ipv4.tcp_congestion_control=bbrBufferbloat on your own line
- Steps: Run
ping 1.1.1.1while uploading a big file - What to look for: ping times jumping from ~10 ms to hundreds of ms during the upload.
- From section: 11. Bufferbloat: when queues are too big
ping 1.1.1.1