Skip to main content

TCP (Intermediate)

TCP Protocol

This is the continuation of previous wiki about network-protocol. here i will record more on how TCP used in more higher business level

TCP Congestion & Flow Control

TCP uses both congestion control and flow control to manage data transmission efficiently and prevent network overload. While they both aim to regulate the flow of data, they operate at different levels and address different problems.

TCP Congestion

A set of algorithms that TCP uses to estimate the available network capacity and adjust the sender's transmission rate accordingly. The goal is to avoid sending data faster than the network can handle.

TCP Congestion Work Flow
  1. Slow Start: Like cautiously merging onto a highway. The sender starts slowly and gradually increases its speed (transmission rate) exponentially, probing for available bandwidth.
  2. Congestion Avoidance: Once a certain speed is reached (slow start threshold), the sender switches to a more conservative linear increase in speed, like maintaining a safe following distance.
  3. Fast Retransmit/Fast Recovery: If a car (packet) gets lost, it's a sign of potential congestion. The sender quickly retransmits the lost packet and slows down.
  4. Multiplicative Decrease: If a serious traffic jam occurs (severe congestion), the sender drastically reduces its speed (transmission rate), like taking the next exit to avoid the jam.

Key Mechanism:

  1. Congestion Window: The congestion window is like the number of cars the sender is allowed to have on the highway at once. It limits the amount of unacknowledged data in transit.
  2. Round Trip Time (RTT): Used to estimate network latency and adjust the transmission rate.
TCP Flow Control

A mechanism to prevent a fast sender from overwhelming a slow receiver. It ensures that the sender doesn't transmit data faster than the receiver can process and store it.

TCP Flow Control Work Flow
  1. Receive Window: The receiver tells the sender how much free space it has in its buffer (the parking garage). This is the "receive window."
  2. Sender Limits Transmission: The sender can only send as much data as the receiver's advertised receive window allows. If the receive window is zero, the sender must stop sending data until the receiver frees up some buffer space.

Key Mechanism:

  1. Receive Window: The receive window is like the number of parking spaces available in the garage. It limits the amount of data the sender can transmit at any given time.

TCP Fast Transmission

TCP implements fast retransmission to quickly recover from lost packets without waiting for a retransmission timeout. It relies on duplicate ACKs as a signal of packet loss. Here's how it works:

  1. Packet Loss: A packet is lost in transit from the sender to the receiver.
  2. Duplicate ACKs: The receiver continues to receive packets after the lost one. For each of these subsequent packets, the receiver sends an ACK acknowledging the last in-sequence byte received. Because a packet was lost, these ACKs will all acknowledge the same byte – the last byte of the packet before the missing one. These are called "duplicate ACKs."
  3. Fast Retransmit Trigger: When the sender receives three duplicate ACKs, it assumes the packet corresponding to the acknowledged byte has been lost.
  4. Retransmission: The sender immediately retransmits the presumed lost packet without waiting for the retransmission timer to expire.
  5. Further ACKs: The receiver eventually receives the retransmitted packet and sends a new ACK acknowledging the now-received data.

Key Advantage:

  1. Faster Recovery: Avoids the delay of waiting for the retransmission timeout, significantly improving performance, especially in lossy networks.
  2. Better Congestion Control: The quick retransmission helps the congestion control algorithms react faster to packet loss, leading to more efficient network utilization.

Fast recovery is an extension of fast retransmit that aims to avoid unnecessarily reducing the congestion window after a fast retransmit. Instead of halving the congestion window (as in the standard TCP Reno algorithm), fast recovery inflates the congestion window to compensate for the assumed lost packet and then enters congestion avoidance. This helps maintain higher throughput in the presence of occasional packet loss.

In summary, fast retransmission and fast recovery are important TCP mechanisms for improving performance and resilience in the face of network packet loss. They allow for much quicker recovery from lost packets compared to relying solely on retransmission timers.

TIME_WAIT in TCP

The TIME_WAIT state occurs in a TCP connection on the client-side after the client has sent the final ACK in the four-way closing handshake. The server closes the connection immediately after sending its final ACK, but the client enters TIME_WAIT.

Why Its Necessary
  1. Reliable Connection Termination: TIME_WAIT ensures that delayed segments from the server (sent before the server received the client's FIN but arriving late) don't interfere with a new connection on the same port. These late packets are discarded during TIME_WAIT.
  2. Preventing Duplicate Packets: TIME_WAIT prevents old duplicate packets from a previous connection from being mistakenly accepted as part of a new connection, especially if the client quickly reuses the same source port.
Duration Of TIME_WAIT

TIME_WAIT typically lasts for twice the Maximum Segment Lifetime (MSL), which is the maximum time a segment can remain on the network. A common MSL is 2 minutes, resulting in a 4-minute TIME_WAIT.