MA-NET1 · Module 9
TCP flow control, handshake, and congestion
Topics: Receive window · Three-way handshake · Slow start · Congestion avoidance
Overview
Session 9 — TCP: flow control, handshake, congestion
Last session Mira learned how TCP repairs losses. This session asks two harder questions. First: how fast should the sender go? There are two different answers, because there are two different things that can be overwhelmed — the receiving application's buffer (flow control) and the network in between (congestion control). Second: how does a connection begin? The three-step opening exchange (SYN, SYN-ACK, ACK) establishes agreement on starting sequence numbers before any Skylark Forecast data flows to Northline Relay.
You will follow a congestion window through slow start and congestion avoidance with invented numbers, see what a timeout does to it, and develop an intuition for why many competing flows through a shared BayLink-to-SpineNet bottleneck tend to settle toward fair shares.
Plan: ideas → worked numerical stories → short drills.
Time: about 60–75 minutes if you trace each window walk on paper before reading the interpretation.
Concepts
Session 9 — TCP: flow control, handshake, congestion
The one-paragraph story
A reliable protocol that sends as fast as it possibly can would be a menace. If the sender outruns the receiving application, bytes pile up in a buffer that has nowhere to put them. If the sender outruns the network, router queues overflow and packets are discarded — which TCP then dutifully resends, adding even more load. So TCP has two separate brakes. Flow control protects the receiver's buffer; congestion control protects the shared network. And before either brake matters, the two ends must agree that a connection exists, which is the job of the three-way handshake. This session covers all three, in that order of importance for understanding: the brakes first, the handshake second, and the dynamics of congestion third.
Core ideas — keeping a fast sender polite
1. Flow control: protecting the receiver
Imagine Mira's phone receiving a long stream of map tiles. TCP on the phone stores arriving bytes in a receive buffer; the Skylark app reads from that buffer whenever it gets around to it. If the app is busy rendering and reads slowly, the buffer fills. A sender who keeps transmitting would overflow it.
To prevent that, every segment the receiver sends back includes a field called the receive window, abbreviated rwnd. It means: "This is how many more bytes I have room for right now." The receiver computes it from the buffer state:
[ \text{rwnd} = \text{RcvBuffer} - (\text{LastByteRcvd} - \text{LastByteRead}) ]
In words: total buffer size, minus the bytes currently sitting in it waiting for the application. The sender must obey by keeping the amount of unacknowledged ("in flight") data no larger than rwnd:
[ \text{LastByteSent} - \text{LastByteAcked} ;\le; \text{rwnd}. ]
When the app reads and frees space, the next ACK carries a bigger rwnd and the sender may speed up. When rwnd shrinks to zero, the sender must stop sending data. (It keeps sending tiny one-byte probe segments so it will learn when the window reopens; otherwise a lost "window is open again" message could deadlock the connection.)
Flow control is a conversation between two endpoints only. It has no idea how crowded the network between them is.
2. Congestion control: protecting the network
The network has no loud way to say "slow down." Routers just drop packets (or, in some networks, mark them) when their queues fill. So a TCP sender infers congestion from its own experience: a retransmission timer firing, or several duplicate ACKs, is read as "somebody along the path is overloaded."
To act on that inference, the sender keeps a second limit, the congestion window, cwnd, measured in bytes (or, for convenience in teaching, in multiples of MSS). The sender may have no more than cwnd bytes in flight:
[ \text{LastByteSent} - \text{LastByteAcked} ;\le; \min(\text{cwnd},\ \text{rwnd}). ]
Whichever limit is smaller wins. If the receiver's buffer is tiny, rwnd is the bottleneck; if the network is crowded, cwnd is. A rough picture of speed follows: in each round trip, the sender can push about one window of data, so
[ \text{throughput} ;\approx; \frac{\text{window}}{\text{RTT}}. ]
The sender cannot see the network's true capacity, so it probes: it increases cwnd while things go well, and cuts it sharply when loss appears.
3. Slow start
A new connection does not know whether the path can handle 2 segments or 200. Starting at a large window would be reckless; starting tiny and creeping would be wasteful. Slow start is the compromise: begin with cwnd = 1 MSS and double it every round trip. It is called "slow" only because it starts small; the growth is actually exponential.
The doubling comes from a simple rule: for every ACK that arrives acknowledging new data, add 1 MSS to cwnd. In a round where cwnd segments are sent, cwnd ACKs come back, so cwnd grows by cwnd — that is, it doubles.
4. The threshold and congestion avoidance
Exponential growth cannot continue forever. A variable ssthresh (slow-start threshold) marks where TCP should become cautious. Once cwnd reaches ssthresh, TCP leaves slow start and enters congestion avoidance, where cwnd increases by about 1 MSS per round trip — linear growth. Think of it as moving from "sprinting to find the speed limit" to "creeping up carefully."
Where does ssthresh come from? It starts at some large value (or is configured). After a loss it is reset based on what the connection just experienced, as the next section explains.
5. Reacting to loss
When TCP decides a loss has happened, it does two things: it sets ssthresh to roughly half of the current cwnd (the point at which things were probably still fine), and it shrinks cwnd. How much it shrinks depends on the type of evidence:
- Timeout. The most severe signal, because it means even the follow-up ACKs stopped coming. TCP sets ssthresh ≈ cwnd / 2 and cwnd = 1 MSS, then restarts slow start. It climbs quickly back up to the new, lower threshold and then continues linearly.
- Three duplicate ACKs. A milder signal: segments are still getting through, since ACKs are flowing. Modern TCP sets ssthresh ≈ cwnd / 2 and then continues from about that level (plus a small allowance) in congestion avoidance, skipping the dive to 1 MSS. This is fast recovery.
The overall shape of cwnd over time is a sawtooth: climb, drop, climb, drop. The additive climb followed by a multiplicative cut is called AIMD — additive increase, multiplicative decrease.
6. Fairness on a shared bottleneck
Suppose a BayLink-to-SpineNet link is the narrowest point on several flows' paths. If (N) TCP connections share a bottleneck of capacity (R), the hope is that each gets about (R/N). AIMD helps achieve this by a quiet geometric effect. When the link is full, every flow gets hit by loss at about the same time and halves its window; halving shrinks a big flow by a lot and a small flow by a little, so the gap between them shrinks. Then all flows add the same amount each round trip, which preserves the gap. Repeat: halve (gap shrinks), add (gap stays), halve (gap shrinks again). Over many cycles the flows converge toward equal shares.
Two caveats are worth knowing. First, flows with a shorter RTT adjust faster and tend to grab a bit more. Second, fairness is per connection: an application that opens ten connections gets about ten shares. And programs using UDP are not required to back off at all, which can squeeze TCP flows.
7. The handshake: agreeing to talk
Before data moves, the two ends perform a three-way handshake, so each side knows the other is alive and each tells the other where its sequence numbering starts.
- SYN. The client (Mira's phone) sends a segment with the SYN flag set and its chosen initial sequence number, call it (x). No application data yet.
- SYN-ACK. The server replies with both SYN and ACK flags set. It announces its own initial sequence number (y) and acknowledges the client's with ACK number (x+1). (The SYN counts as using up one sequence number, hence the +1.)
- ACK. The client acknowledges the server's SYN with ACK number (y+1). From here on the connection is established, and this third segment is allowed to carry application data.
Why three steps? Each side has to send its own starting number and hear it confirmed. The two middle actions of the server (announce and acknowledge) fit in one segment, giving three segments in total rather than four. The cost to remember: the handshake takes one round trip before the client's first data can leave, which is why short exchanges over a long-delay path feel slow. Closing a connection is a separate exchange using FIN segments.
If the SYN or its reply is lost, the sender's timer eventually fires and it simply sends the SYN again. The sequence-number agreement also helps the endpoints ignore stray, delayed segments left over from older connections.
Common confusions
"Flow control and congestion control are the same thing." They both limit the sender, but for different reasons and by different signals. rwnd arrives explicitly from the receiver; cwnd is inferred by the sender from loss. The sender obeys the smaller of the two.
"Slow start is slow." It begins small but grows exponentially. Its name describes where it starts, not how it behaves.
"After a timeout cwnd = 1 and ssthresh = 1." No: cwnd becomes 1 MSS, but ssthresh becomes about half of the old cwnd. The old window is not forgotten; it determines where slow start will hand over to linear growth.
"The handshake carries data in the first segment." The first two segments carry none. Data can ride on the third.
"Fairness means every flow is always equal." It is a long-run tendency, not an instantaneous guarantee, and it breaks down with unequal RTTs or multiple connections per application.
Why not simply send everything at once?
It is tempting to ask why TCP bothers with all this caution. Consider what would happen if every connection on a BayLink-to-SpineNet link blasted out its full file the moment it started. The link's queue would overflow almost immediately, most packets would be discarded, and the survivors would be mostly duplicates of retransmissions. The link would be fully busy yet deliver very little useful data — a condition sometimes called congestion collapse. Congestion control is, in a sense, a collective-good agreement: each sender holds itself back a little so that all of them get more done.
Notice, too, that nothing forces a sender to cooperate; the rules live in the endpoints' software. The scheme works because nearly everyone runs compatible implementations.
Putting the numbers in context
Congestion control matters most when the bandwidth-delay product is large — the amount of data that fits "in the pipe" at once. If a path carries 8 megabits each second and the RTT is 80 ms, then roughly 80,000 bytes are in flight when the pipe is full. A window smaller than that leaves capacity unused; a window much larger than that merely fills router queues and adds delay. The slow-start-then-AIMD pattern is TCP's way of homing in on that ideal size without ever being told it.
Quick self-check before the worked examples
- A receiver has a 20,000-byte buffer with 6,000 bytes waiting to be read. What rwnd does it advertise?
- cwnd is 14 MSS when a timeout occurs. What are cwnd and ssthresh afterwards?
- In a SYN-ACK segment, why is the acknowledgment number one more than the client's initial sequence number?
- Why does halving both windows narrow the difference between two flows, but adding equal amounts does not?
Answers to the first two (with full numbers) appear in the examples. The third is explained in section 7: the SYN consumes one number, so the next expected one is (x+1). For the fourth, picture 8 and 2: halving gives 4 and 1, so the gap goes from 6 to 3; adding 1 to each gives 5 and 2, so the gap is still 3.
If this felt dense
Keep one image in mind: a faucet with two valves in series. The receiver controls one valve (rwnd) by telling the sender what the bucket can hold. The network controls the other, indirectly (cwnd), by losing packets when pipes overflow. Water flows at the rate allowed by the tighter valve — and the sender keeps nudging the second valve open until it hears a splash.
Examples
Session 9 — Worked examples
Example A — How many bytes may the sender add right now?
Scene. The Northline Relay server is sending tiles to Mira's phone. The phone's receive buffer is 32,000 bytes. At this instant:
- the highest byte the phone has received is number 58,400 (call it LastByteRcvd),
- the highest byte the Skylark app has actually read out of the buffer is 49,900 (LastByteRead),
- the server has 18,000 bytes in flight (sent but not acknowledged),
- the server's congestion window is 30,000 bytes.
Step 1 — bytes waiting in the buffer.
[ 58{,}400 - 49{,}900 = 8{,}500 \text{ bytes} ]
Step 2 — receive window the phone advertises.
[ \text{rwnd} = 32{,}000 - 8{,}500 = 23{,}500 \text{ bytes} ]
Step 3 — the sender's limit. The server may keep at most (\min(\text{cwnd}, \text{rwnd}) = \min(30{,}000,\ 23{,}500) = 23{,}500) bytes in flight.
Step 4 — room left. It already has 18,000 in flight, so it may add
[ 23{,}500 - 18{,}000 = 5{,}500 \text{ more bytes.} ]
Interpretation. Here the receiver's buffer is the tighter limit. Now vary the story: suppose the cwnd had instead been 15,000 bytes. Then the limit would be (\min(15{,}000, 23{,}500) = 15{,}000), which is already less than the 18,000 in flight, so the server may send nothing new until ACKs reduce the in-flight amount. In that version the network (cwnd), not the buffer, is the brake. One more variation: if the app suddenly reads 6,000 more bytes (LastByteRead rises to 55,900), the buffered amount drops to 2,500, rwnd rises to 29,500, and the next ACK passes that larger number back, allowing the sender to speed up.
Example B — A handshake with invented numbers
Scene. Mira's phone opens a connection to a Northline Relay server. The phone picks initial sequence number 7,300; the server picks 41,900. The one-way delay is 30 ms each direction, so the RTT is 60 ms.
| Time | Direction | Flags | Seq | ACK | Data | |---|---|---|---|---|---| | t = 0 ms | phone → server | SYN | 7,300 | — | none | | t = 30 ms | server → phone | SYN, ACK | 41,900 | 7,301 | none | | t = 60 ms | phone → server | ACK (can carry first data) | 7,301 | 41,901 | optional |
Reading the table.
- The phone's SYN uses up sequence number 7,300 even though it carries no data. So the server acknowledges 7,301 — "the next number I expect from you."
- The server's own SYN uses up 41,900, so the phone's final ACK says 41,901.
- The first real data byte from the phone will be numbered 7,301, and the first real data byte from the server will be numbered 41,901.
When can data arrive at the server? The phone can transmit the third segment (and the first data with it) at t = 60 ms, the instant the SYN-ACK arrives. That segment reaches the server at t = 90 ms. So the earliest the server can see request bytes is 1.5 round trips after the phone first tried — one full RTT for the handshake plus the one-way trip of the data.
A lost SYN-ACK. Suppose the server's reply at t = 30 ms is dropped. The phone hears nothing and, when its timer fires, sends the same SYN (seq 7,300) again. The server, which already created state for the half-open connection, sends the SYN-ACK once more. The numbers repeat exactly; the only difference is the delay added by the timeout.
Interpretation. The handshake costs one RTT. On a short-delay link that is barely visible; on a long path, it is a noticeable fixed fee for every new connection, which is why applications reuse existing connections when they can.
Example C — A congestion-window walk through slow start, avoidance, and a timeout
Scene. Skylark's phone downloads a large tile pack over a TCP connection with MSS = 400 bytes and an initial ssthresh = 10 MSS. We track cwnd at the start of each round trip, in MSS. A timeout occurs during round 8.
Phase 1 — slow start (cwnd doubles each round trip until it would pass ssthresh).
| Round | cwnd (MSS) | Bytes sent that round | Phase | |---|---|---|---| | 1 | 1 | 400 | slow start | | 2 | 2 | 800 | slow start | | 3 | 4 | 1,600 | slow start | | 4 | 8 | 3,200 | slow start | | 5 | 10 | 4,000 | doubling would give 16, but capped at ssthresh = 10 |
Phase 2 — congestion avoidance (+1 MSS per round trip).
| Round | cwnd (MSS) | Bytes sent that round | Phase | |---|---|---|---| | 6 | 11 | 4,400 | avoidance | | 7 | 12 | 4,800 | avoidance | | 8 | 13 | 5,200 | avoidance → timeout |
Phase 3 — the timeout. During round 8 the retransmission timer fires. TCP's response:
- ssthresh = about half of 13 MSS = 6.5, rounded down to 6 MSS
- cwnd = 1 MSS
- restart slow start
Phase 4 — recovery.
| Round | cwnd (MSS) | Phase | |---|---|---| | 9 | 1 | slow start | | 10 | 2 | slow start | | 11 | 4 | slow start | | 12 | 6 | doubling would give 8, capped at new ssthresh = 6 | | 13 | 7 | avoidance | | 14 | 8 | avoidance |
Interpretation. The window climbed to 13, was knocked to 1, and then returned to 6 in just four rounds because exponential growth is quick. After that it creeps up again from a more conservative base; this time the connection will not try to sprint beyond 6 until it has more evidence the network can handle it. If the loss had been signalled by three duplicate ACKs instead of a timeout, the usual modern response would skip the plunge to 1: ssthresh would again be 6, and cwnd would continue from about 6 (plus a small allowance for the duplicate ACKs) in avoidance — a much shallower drop.
Example D — Two flows on one bottleneck converge toward fairness
Scene. Two TCP flows from different phones cross a BayLink access link that can carry 10 units of window in total per round trip (a toy number; one unit could be a handful of segments). Flow P starts greedy at 9, flow Q at 3. Each round with no loss, both add 1. Whenever the sum exceeds 10, both detect loss and halve.
| Step | P | Q | Sum | Event | Difference P − Q | |---|---|---|---|---|---| | start | 9 | 3 | 12 | over capacity → loss, both halve | 6 | | after halving | 4.5 | 1.5 | 6 | — | 3 | | +1 each | 5.5 | 2.5 | 8 | — | 3 | | +1 each | 6.5 | 3.5 | 10 | exactly full, no loss yet | 3 | | +1 each | 7.5 | 4.5 | 12 | over capacity → loss, both halve | 3 | | after halving | 3.75 | 2.25 | 6 | — | 1.5 | | +1 each | 4.75 | 3.25 | 8 | — | 1.5 | | +1 each | 5.75 | 4.25 | 10 | exactly full, no loss yet | 1.5 | | +1 each | 6.75 | 5.25 | 12 | over capacity → loss, both halve | 1.5 | | after halving | 3.375 | 2.625 | 6 | — | 0.75 |
Interpretation. Watch the last column: 6 → 3 → 1.5 → 0.75. Every halving cuts the difference in half; every equal addition leaves it untouched. After only three loss events the flows are nearly equal. This is the quiet reason AIMD tends toward fairness — nobody coordinates, yet the shared pattern of "add the same, cut proportionally" pushes the pair together. (Real flows lose packets at slightly different instants, and RTTs differ, so the convergence is rougher, but the tendency holds.)
Example E — Window-limited speed and sharing a link
Part 1: What does a small window cost?
Scene. A path between Mira's phone and a Northline Relay server has a bottleneck rate of 8 megabits per second and a round-trip time of 80 ms. The server's TCP stack gets an rwnd advertisement of only 16,000 bytes, and cwnd has grown well beyond that.
Bandwidth-delay product (how much data fits in the pipe):
[ 8{,}000{,}000 \text{ bit/s} \times 0.080 \text{ s} = 640{,}000 \text{ bits} = 80{,}000 \text{ bytes.} ]
Throughput limited by the window. Only 16,000 bytes may be in flight per round trip:
[ \frac{16{,}000 \times 8 \text{ bits}}{0.080 \text{ s}} = \frac{128{,}000}{0.080} = 1{,}600{,}000 \text{ bit/s} = 1.6 \text{ Mbit/s.} ]
That uses one fifth of the 8 Mbit/s link (16,000 / 80,000 = 0.2). To fill the pipe the window would need to reach 80,000 bytes, which at MSS = 400 means a window of 200 MSS. The lesson: on a path with a large bandwidth-delay product, a tiny advertised window throttles the connection no matter how fast the links are.
Part 2: Sharing a bottleneck.
Scene. A 12 Mbit/s SpineNet link is the bottleneck for four long-running TCP connections. Skylark's phone has opened three of them (parallel tile downloads); a video app on another device has one.
If TCP shares equally per connection, each gets (12 / 4 = 3) Mbit/s. Skylark's total is (3 \times 3 = 9) Mbit/s and the video app's is 3 Mbit/s.
Interpretation. Per-connection fairness is not per-application fairness. An app that opens many connections claims a larger share, which is why the other app sees 3 of the 12 Mbit/s rather than 6. It is a legitimate technique, but it shows that "fair" in TCP has a narrow meaning.
Practice check — TCP flow control, handshake, congestion
Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.
1.A receive buffer holds 48,000 bytes in total and 15,000 bytes are waiting to be read by the application. What receive window (rwnd) does the receiver advertise?
2.A sender has cwnd = 20,000 bytes and receives rwnd = 12,000 bytes. About how many bytes may it have in flight?
3.A client's SYN carries initial sequence number 2,500. What does the server's SYN-ACK acknowledge, and why?
4.A connection with MSS = 500 bytes is in slow start with cwnd = 4 MSS at the beginning of a round trip and no loss occurs (ssthresh is large). What is cwnd at the beginning of the next round trip?
5.A connection is in congestion avoidance with cwnd = 18 MSS when a timeout occurs. Which pair of values describes the state immediately afterwards?
6.ssthresh is 12 MSS and cwnd is 6 MSS in slow start. Following the simple 'cap at ssthresh' rule, what are the cwnd values at the starts of the next two round trips?
7.Two flows at windows of 12 and 4 both detect a loss and halve their windows, then both add the same amount for several rounds. What happens to the gap between them during the halving step and the adding steps?
8.Which statement best distinguishes flow control from congestion control?