How does the TCP sliding window work? Give a small example.
The receiver advertises how many bytes it can accept. The sender may keep that many bytes in flight without waiting for ACKs, and as each ACK arrives the window slides forward so new data can be sent. This keeps the link busy instead of stop-and-wait.
Explain it simply
Stop-and-wait is like a cashier who hands over one item and waits for the customer to say "got it" before the next. A sliding window lets the cashier pass four items without waiting, and as each "got it" comes back, send one more.
Example
MSS = 1000 bytes, receiver window = 4000 bytes -> 4 segments in flight
t0 send seg1 seg2 seg3 seg4 (4000 bytes unacknowledged, window full)
t1 ACK 2001 arrives (seg1, seg2 done) -> send seg5 seg6
t2 ACK 4001 arrives (seg3, seg4 done) -> send seg7 seg8Without a window (stop-and-wait) and a round trip time of 50 ms, the sender would manage 1000 bytes per 50 ms, which is 20 KB per second. With a window of 4000 bytes it is four times that, 80 KB per second. A bigger window means better use of a long, fast link, which is why the window scale option exists: the original 16-bit window field tops out at 65,535 bytes.
The ACK number says "I have everything up to this byte, send the next one", so the window is measured in bytes, not packets.
What to say in the interview
"The sliding window lets the sender transmit several segments before an ACK, up to the receiver's advertised window. As ACKs arrive the window moves right. Throughput is roughly window divided by round-trip time, so large windows matter on high-latency links."
Common mistakes
- Counting the window in packets. TCP counts bytes.