In the last post, we saw that UDP uses its checksum only to detect corruption and does not solve loss or reordering.
So how can data that must not lose a single piece, such as a file transfer or a payment request, become reliable?
This post explains, step by step, the principles for building
reliable data transfer
(rdt) on top of a lower layer that can lose or corrupt data.
TCP implements these principles in practice, so reading the structure built here alongside TCP makes it easier to understand.
The sender alternates the sequence numbers 0 and 1 on each packet.
If the receiver gets a number it has already received, it treats the packet as a duplicate, discards it, and sends only the ACK again.
This way, a packet resent because its ACK was lost is filtered out as a duplicate.
If the ACK carries the sequence number of the last correctly received packet, the NAK is no longer needed.
When a damaged packet arrives, the receiver resends the ACK for the previous number, and the sender sees that ACK and resends the same packet.
If a packet disappears entirely, the receiver sends nothing, and the sender would wait forever.
So the sender starts a
timer
when it sends a packet and resends it if no ACK arrives in time.
If the timer is too short, packets still in transit get resent, but the sequence numbers let the receiver filter out those duplicates.
rdt 3.0 is correct, but the sender waits for each ACK before sending the next packet.
This is called
stop-and-wait
.
For example, suppose a 1 Gbps link carries 1,000-byte packets and the round-trip time (RTT) is 30 ms.
The time to push one packet onto the link, L/R, is 8,000 bits รท 1 Gbps = 8 ยตs.
The sender's utilization is:
U = (L/R) / (RTT + L/R) = 0.008 ms / 30.008 ms โ 0.027%
Even on a 1 Gbps link, the sender uses only 0.027% of its capacity.
The rest of the time is spent waiting for ACKs.
, which sends several packets without waiting for each ACK.
The sender can have up to N unacknowledged packets in flight, and this range is called the
window
.
U = Nยท(L/R) / (RTT + L/R)
Under the same conditions, N = 1000 raises utilization to about 26.7%.
This is a theoretical value that assumes no losses.
Pipelining requires two decisions: how to handle losses, and how the receiver treats packets that arrive out of order.
Aspect
Go-Back-N
Selective Repeat
Acknowledgment
Cumulative ACK (everything up to n received)
Individual ACK for each packet
Timer
One, on the oldest unacknowledged packet
One per packet
Retransmission scope
The whole window on timeout
Only the lost packet
Receiver buffer
Not needed; accepts packets in order only
Buffers out-of-order packets
Go-Back-N
keeps the receiver simple, but if one packet is lost, the correctly received packets after it are discarded and the whole window must be resent.
Selective repeat
resends only the lost packets and saves bandwidth, but the receiver must store out-of-order packets.
A pipelined sender sends several packets in succession within the window, and the receiver returns a cumulative acknowledgment
๐จโ๐ป
TCP implements this principle by combining sequence numbers, cumulative ACKs, and timers.
So if retransmissions seem frequent on a TCP connection, check whether losses are occurring and how long the timer is set.
Checking retransmissions under loss with Node.js ๐
The example below implements stop-and-wait over UDP.
The receiving side deliberately drops 30% of the datagrams to simulate loss.
Each run loses packets in different places, so the number of retransmissions changes from run to run.
Still, the received line at the end always contains m0 through m4 in order.
This shows that sequence numbers, ACKs, and timers alone can tolerate loss while preserving order.
This example simulates only data loss and does not handle ACK loss.