1. Home
  2. Computer Networks
  3. Sliding Window Protocol

Sliding Window Protocol

Send many frames before waiting for acknowledgements. Compare Stop-and-Wait, Go-Back-N and Selective Repeat when a frame gets lost — in 3D.

Interactive 3DIntermediate13 min readCNUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · ←→ step

What's happening

Pseudocode

    Try this in the 3D model

    • Run Go-Back-N with frame 2 lost. Which frames get re-sent after the timeout?
    • Run Selective Repeat with the same loss. Compare the number of transmissions.
    • Run Stop-and-Wait on a perfect link and count the ticks. Why is it so slow?

    The problem: waiting wastes time

    Data is sent in frames, and the receiver confirms each with an acknowledgement (ACK). If a frame or ACK is lost, the sender retransmits after a timeout. This idea is called ARQ (Automatic Repeat reQuest).

    The simplest version, Stop-and-Wait, sends one frame and waits for its ACK before sending the next. On a long link (say India → USA, ~200 ms round trip) the line sits idle most of the time.

    The sliding window idea

    Let the sender have up to N unacknowledged frames in flight at once. The window is the range of sequence numbers it may send:

    [ acked | acked | sent | sent | sent | sent | not yet | not yet ]
                     └──────── window (N = 4) ───────┘

    When the oldest frame in the window is acknowledged, the window slides forward and a new frame may be sent. In the 3D model, the translucent box on the sender’s row is the window.

    Go-Back-N (GBN)

    • Sender keeps up to N frames in flight.
    • Receiver accepts frames only in order; anything out of order is discarded.
    • ACKs are cumulative: “ACK k” means everything before k arrived.
    • On timeout, the sender goes back and resends the oldest unacknowledged frame and all frames after it.

    Simple receiver, but one loss causes many good frames to be resent.

    Selective Repeat (SR)

    • Receiver buffers out-of-order frames and acknowledges each frame individually.
    • On timeout, the sender resends only the missing frame.
    • More efficient on lossy links, but the receiver needs buffer space, and the window can be at most half the sequence-number space (to avoid confusing old and new frames).

    Comparison

    Stop-and-Wait Go-Back-N Selective Repeat
    Sender window 1 N N
    Receiver window 1 1 N
    Out-of-order frames — Discarded Buffered
    ACK type Individual Cumulative Individual
    Resent on one loss 1 Up to N 1
    Complexity Lowest Medium Highest

    Efficiency

    With frame transmission time Tf and propagation delay Tp, let a = Tp / Tf. The link utilisation of Stop-and-Wait is

    U = 1 / (1 + 2a)

    A window of size N ≥ 1 + 2a keeps the link fully busy (U ≈ 1). That’s why TCP uses a sliding window (with a variable size) on top of IP.

    Code (Go-Back-N sender, simplified)

    def go_back_n(frames, N, lost=None):
        lost = set(lost or {2})                                       # frames lost the first time
        base, next_seq, log = 0, 0, []
        while base < len(frames):
            while next_seq < base + N and next_seq < len(frames):    # fill the window
                log.append(f"send {next_seq}" + (" (lost)" if next_seq in lost else ""))
                next_seq += 1
            # receiver accepts only in-order frames
            expected = base
            while expected < next_seq and expected not in lost:
                expected += 1
            if expected == base:                                      # nothing new arrived → timeout
                log.append(f"timeout → resend {base}..{next_seq - 1}")
                lost.discard(base)
                next_seq = base
            else:
                log.append(f"ACK {expected}")
                base = expected
        return log
    
    for line in go_back_n(list(range(8)), 4):
        print(line)

    Common mistakes

    • Thinking Go-Back-N resends all frames — only from the lost one onwards.
    • Forgetting that ACKs can be lost too (a later cumulative ACK still covers earlier frames in GBN).
    • Making the Selective Repeat window bigger than half the sequence space.

    Complexity at a glance

    Case / operationTimeWhy
    Max frames in flightN (window size)
    Retransmissions after one loss — Go-Back-Nup to NThe lost frame and everything after it.
    Retransmissions after one loss — Selective Repeat1Only the lost frame.
    Extra spaceReceiver buffer of N frames (Selective Repeat)

    Quick check

    Test yourself — pick an answer to see if you got it.

    1. In Go-Back-N, what does the receiver do with a frame that arrives out of order?

    2. After a timeout, Selective Repeat retransmits…

    3. Stop-and-Wait is equivalent to a sliding window of size…

    4. Why do sliding window protocols improve throughput?

    Saved only in this browser — no account needed.
    Spotted a mistake or a bug in the 3D model?

    Report a mistake

    in Sliding Window Protocol. Thank you — every report makes the lesson better for the next reader.

    We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.