Loading
0x70Lesson 8 of 14

Keep data flowing with TCP

Play with sliding windows, retransmission and congestion control.

25 min 7-question quiz 2 code exercises
By the end of this lesson you can
  • 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?

Window size
  • 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
012.52537.55014710131619222528Round tripSegments
Show the numbers as a table
Round tripCongestion windowSlow-start threshold
1116
2216
3416
4816
51616
61716
71816
81916
92016
102116
112216
122316
131111
141211
151311
161411
171511
181611
191711
201811
211911
222011
232111
242211
252311
262411
272511
282611

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.

congestion_window.py
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)
Output
[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.

Exercise 1

Count the round trips

+25 XP

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
main.py
Loading editor…

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.

Exercise 2

Simulate a congestion window

+25 XP

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
main.py
Loading editor…

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…