How does TCP guarantee reliable, in-order delivery?
HardConceptScenario Questions
Answer
IP may lose, duplicate, corrupt or reorder packets, so TCP adds its own mechanisms on top to give applications a reliable, ordered byte stream.
- Sequence numbers: every byte is numbered, so the receiver can put segments back in order, detect gaps and discard duplicates.
- Acknowledgements (ACKs): the receiver reports the next byte it expects (a cumulative ACK), so the sender knows exactly what has arrived.
- Retransmission: if no ACK arrives before the retransmission timeout, which adapts to the measured round-trip time, or if three duplicate ACKs arrive, the sender resends the missing segment.
- Checksums: each segment carries a checksum over its header and data; a corrupted segment is discarded and later retransmitted. Flow and congestion control also prevent losses caused by overflowing the receiver or the network.
Example: The sender sends four 100-byte segments starting at byte 1, and the second is lost:
seq=1 (bytes 1-100) arrives -> ACK 101
seq=101 (bytes 101-200) LOST
seq=201 (bytes 201-300) arrives, buffered -> ACK 101 (duplicate)
seq=301 (bytes 301-400) arrives, buffered -> ACK 101 (duplicate)
timeout, resend seq=101 arrives, gap filled -> ACK 401
The application receives bytes 1 to 400 exactly once and in order, without ever seeing the loss.