How does TCP ensure reliability in data transmission?

Sachin Tharaka
5 min readJan 2, 2025

--

TCP (Transmission Control Protocol) is a core protocol of the internet that ensures reliable data transmission during web browsing. Here’s how TCP works to deliver data reliably.

1. Connection Establishment (Three-Way Handshake)

Before data is sent, a TCP connection is established between the client (your browser) and the server hosting the website:

  • SYN: The client sends a synchronization (SYN) request to the server to initiate a connection.
  • SYN-ACK: The server responds with a synchronization acknowledgment (SYN-ACK).
  • ACK: The client acknowledges the server’s response by sending an acknowledgment (ACK). This establishes a reliable connection between the client and the server.

2. Data Transmission

TCP ensures reliable data transfer using the following features:

  • Segmentation: The browser sends the data (HTTP requests) in smaller chunks called packets.
  • Sequencing: Each packet is assigned a sequence number to ensure they are received in order.
  • Acknowledgments (ACKs): The receiver sends an acknowledgment for each packet received.
  • Flow Control: TCP uses a sliding window mechanism to ensure the sender doesn’t overwhelm the receiver with too much data at once.
  • Retransmission: If a packet is lost (detected via missing acknowledgments), TCP retransmits the missing packet.

3. Error Checking

TCP includes a checksum in each packet to detect errors during transmission. If a packet is corrupted, it is discarded, and the sender is informed to retransmit it.

4. Congestion Control

TCP monitors network conditions to avoid congestion:

  • Slow Start: It begins by sending a small amount of data and gradually increases the rate as acknowledgments are received.
  • Congestion Avoidance: If congestion is detected (e.g., packet loss), TCP reduces the transmission rate.

5. Connection Termination

After the data transfer is complete, the connection is gracefully terminated using another finalizing handshake:

  • FIN: The client sends a finish (FIN) request to close the connection.
  • ACK-FIN: The server acknowledges the FIN request and sends its own FIN.
  • ACK: The client acknowledges the server’s FIN, and the connection is closed.

Use Cases

  • Web browsing (HTTP/HTTPS)
  • Email (SMTP, IMAP, POP3)
  • File transfers (FTP)

TCP’s Role in Web Browsing

When you type a URL in your browser:

  1. The browser initiates a TCP connection to the web server.
  2. HTTP or HTTPS requests are sent over this reliable TCP connection.
  3. The server responds with data (HTML, CSS, JavaScript, etc.), which is broken into packets and reliably delivered to your browser.
  4. The browser reassembles the packets into the original response and displays the webpage.

By providing error checking, retransmission, and ordered delivery, TCP ensures that web pages are loaded without corruption or missing content.

How Packets Can Lost?

1. Network Congestion

  • Cause: When too many packets are sent through a network at once, routers and switches may become overwhelmed.
  • Effect: To manage the load, network devices may drop packets they can’t process or forward quickly enough.
  • Example: High traffic during peak hours on an ISP network.

2. Transmission Errors

  • Cause: Data corruption during transmission due to electrical interference, signal degradation, or hardware faults.
  • Effect: The packet is discarded because it fails error-checking mechanisms like CRC (Cyclic Redundancy Check).
  • Example: A damaged Ethernet cable or poor Wi-Fi signal.

3. Hardware Limitations

  • Cause: Routers, switches, or network interface cards may have limited processing or buffer capacity.
  • Effect: When overwhelmed, they drop packets to prevent device failure.
  • Example: A low-capacity router handling a high-bandwidth workload.

4. Routing Issues

  • Cause: Problems with network routing tables, incorrect configurations, or hardware failures in intermediary routers.
  • Effect: Packets are sent to the wrong destination or are dropped due to an invalid path.
  • Example: Misconfigured route tables in a network.

5. Wireless Interference

  • Cause: Interference from other wireless devices, obstacles (like walls), or competing networks using the same frequency.
  • Effect: Wi-Fi signals may degrade, leading to packet drops.
  • Example: Microwave ovens or Bluetooth devices interfering with Wi-Fi.

6. TTL (Time-To-Live) Expiry

  • Cause: Each packet has a Time-To-Live (TTL) value indicating the maximum number of hops it can take through routers.
  • Effect: If the TTL reaches zero, the packet is discarded.
  • Example: A routing loop cause the packet to circulate indefinitely.

7. Security Filtering

  • Cause: Firewalls or intrusion detection systems (IDS) may block packets that appear suspicious or unauthorized.
  • Effect: Legitimate packets may be lost if incorrectly flagged as malicious.
  • Example: A firewall blocking packets from an unknown source.

8. Physical Damage

  • Cause: Damage to cables, connectors, or infrastructure like fiber-optic lines.
  • Effect: Packets may be dropped because they cannot reach their destination.
  • Example: A cut undersea cable disrupting data transmission.

9. Software Bugs

  • Cause: Faults in the network protocol stack or device firmware.
  • Effect: Packets are mishandled or dropped due to software errors.
  • Example: A buggy router firmware causing instability.

10. Intentional Dropping

  • Cause: Devices may drop packets intentionally as part of network traffic shaping or rate-limiting policies.
  • Effect: Dropped packets help manage network resources but can lead to retransmissions.
  • Example: ISP throttling bandwidth during high usage.

How to identify and handle packet loss?

1. Packets Received Out of Order

When a packet is lost, TCP:

  • Buffers (temporarily stores) the packets that arrived after the missing one.
  • Waits for the retransmitted packet to fill the gap.

2. Delivery to the Application Layer

TCP guarantees in-order delivery to the application layer (e.g., your web browser). So:

  • Even if packets are received out of order, TCP holds them until all preceding packets are received.
  • Once the missing packet is retransmitted and arrives, TCP reassembles the data in the correct order and delivers it to the application.

3. How Missing Packets Are Detected

TCP detects missing packets through:

  • Acknowledgments (ACKs): The receiver acknowledges the highest sequence number received in order. If a packet is missing, the receiver repeatedly acknowledges the last correctly received packet, triggering the sender to retransmit.
  • Timeouts: If an acknowledgment for a sent packet isn’t received within a certain time, the sender retransmits it.

4. Impact on Other Packets

While the missing packet is being retransmitted:

  • Packets that have arrived are stored in the buffer.
  • These buffered packets are not delivered to the application until the missing packet arrives to maintain in-order delivery.

Illustrative Example

Suppose a TCP connection transmits packets with sequence numbers: 1, 2, 3, 4, 5. If 3 is lost:

  1. The receiver gets packets 1, 2, and then 4, 5.
  2. It buffers 4 and 5 and sends duplicate ACKs for 2 to indicate a gap.
  3. The sender retransmits the packet 3.
  4. Once 3 arrives, the receiver delivers the packets to the application in order: 1, 2, 3, 4, 5.

Trade-Offs

This behavior ensures reliability and ordered data delivery, but it introduces a slight delay in processing when packets are lost. However, modern networks are optimized to minimize packet loss and latency, making this a rare and manageable occurrence in web browsing.

Thank you for reading. Clap, comment, and follow for more stories!

--

--

Sachin Tharaka
Sachin Tharaka

Written by Sachin Tharaka

Software Engineering, University of Kelaniya, Sri Lanka

No responses yet