Keep data flowing with TCP
Play with sliding windows, retransmission and congestion control.
- Explain why a sliding window is faster than waiting for each acknowledgement.
- Describe what happens to a window when a segment is lost.
- Read a congestion window chart and name its phases.
If TCP sent one segment and waited for its acknowledgement before sending the next, a server 100 ms away could send about ten segments a second, however fast the link was. That would be roughly 120 kilobits per second on a gigabit connection.
So TCP keeps many segments in flight at once. The hard question is how many: too few wastes the link, and too many overflows it.
The sliding window
The window is how much unacknowledged data a sender may have in flight. As acknowledgements arrive, the window slides forward and lets new data out. If a segment goes missing, the receiver keeps what arrived after it in a buffer, but can't hand it to the application until the gap is filled. This stall is called head-of-line blocking.
Try it
Race the window
Run the transfer once with a window of 1, then again with 4 and 8. The chart keeps your results.
Then try losing a segment: click a blue one before sending. What happens to the segments after it, and how many round trips does the loss cost you?
- In flight this round trip
- Received, waiting for a gap
- Delivered to the application
- Will be lost
- Round trips
- 0
- Segments sent
- 0
- Delivered in order
- 0/16
1 segment is ready to go. Click one to lose it on the way.
Two limits on the window
The sender obeys the smaller of two limits, which protect two different things:
- Flow control protects the receiver. It advertises how much buffer space it has left.
- Congestion control protects the network. The sender estimates what the path can carry, because no one tells it directly.
That estimate is the congestion window. TCP's approach is to probe for more bandwidth until loss shows it has gone too far, then back off.
Try it
Draw the sawtooth
Try the three scenarios, then click round trips to place your own loss. Compare a D (duplicate ACKs: the network is dropping some packets but still working) with a T (timeout: nothing is getting through). Hover over the chart to read exact values.
Which one costs more, and why would TCP react differently to each?
- Congestion window
- Slow-start threshold
Show the numbers as a table
| Round trip | Congestion window | Slow-start threshold |
|---|---|---|
| 1 | 1 | 16 |
| 2 | 2 | 16 |
| 3 | 4 | 16 |
| 4 | 8 | 16 |
| 5 | 16 | 16 |
| 6 | 17 | 16 |
| 7 | 18 | 16 |
| 8 | 19 | 16 |
| 9 | 20 | 16 |
| 10 | 21 | 16 |
| 11 | 22 | 16 |
| 12 | 23 | 16 |
| 13 | 11 | 11 |
| 14 | 12 | 11 |
| 15 | 13 | 11 |
| 16 | 14 | 11 |
| 17 | 15 | 11 |
| 18 | 16 | 11 |
| 19 | 17 | 11 |
| 20 | 18 | 11 |
| 21 | 19 | 11 |
| 22 | 20 | 11 |
| 23 | 21 | 11 |
| 24 | 22 | 11 |
| 25 | 23 | 11 |
| 26 | 24 | 11 |
| 27 | 25 | 11 |
| 28 | 26 | 11 |
Add loss to a round trip
Click a round trip to cycle through D (duplicate ACKs), T (timeout) and no loss.
- Total segments sent
- 467
- Final window
- 26
- Final phase
- Congestion avoidance
The phases have names. Slow start doubles the window every round trip, which isn't slow at all; the name refers to starting from a small window. Past the threshold, congestion avoidance adds just one segment per round trip. A loss then cuts the window, and this “additive increase, multiplicative decrease” produces the sawtooth you just drew.
1window = 1
2threshold = 8
3history = []
4
5for round_trip in range(1, 9):
6 history.append(window)
7 if round_trip == 6: # loss seen by duplicate ACKs
8 threshold = window // 2
9 window = threshold
10 elif window < threshold:
11 window *= 2 # slow start
12 else:
13 window += 1 # congestion avoidance
14
15print(history)[1, 2, 4, 8, 9, 10, 5, 6]
Key takeaways
A window keeps many segments in flight, so one round trip delivers much more data.
A lost segment blocks in-order delivery until its retransmission arrives.
Flow control protects the receiver, and congestion control protects the network.
Lesson quiz
7 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: model networking concepts
Use small Python programs to model packet, address, and protocol behavior. These exercises run locally in your browser and do not send network traffic.
Count the round trips
Read a number of segments and a window size. With no loss, each round trip delivers one window. Print how many round trips the transfer needs.
- Stop and wait
- A full window every time
- A partial last window
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Simulate a congestion window
Read the threshold, the number of round trips, and the round trip where a loss occurs (0 for none). Starting from a window of 1, print the window at the start of each round trip, separated by spaces.
Use the rules from the example: on the loss round trip, set the threshold to half the window (rounded down) and set the window to that. Otherwise double below the threshold, and add one at or above it.
- No loss
- One loss
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…