DEV Community

Subham
Subham

Posted on

Congestion Control in TCP ๐Ÿšฆ

Congestion is a situation where the network performance degrades due to too many packets being sent or received. Congestion can cause packet loss, delay, and lower throughput. To avoid congestion, TCP uses two mechanisms: congestion avoidance and congestion detection. ๐Ÿ•ต๏ธโ€โ™‚๏ธ

Congestion Avoidance ๐Ÿšซ

Congestion avoidance is a proactive approach that tries to prevent congestion before it happens. TCP uses a parameter called congestion window (cwnd) to control how many packets can be sent at a time. The cwnd is initially set to a small value and is increased gradually as long as the network is not congested. This is called slow start. ๐ŸŒ

However, if the cwnd grows too large, it may exceed the capacity of the network or the receiver, and cause congestion. To avoid this, TCP uses another parameter called threshold (ssthresh) to limit the growth of the cwnd. When the cwnd reaches the ssthresh, TCP switches from slow start to congestion avoidance mode. In this mode, the cwnd is increased by a smaller amount for each successful transmission. This is called additive increase. โž•

Congestion Detection ๐Ÿ”Ž

Congestion detection is a reactive approach that tries to detect congestion after it happens and adjust the cwnd accordingly. TCP uses two indicators to detect congestion: timeout and duplicate acknowledgments (ACKs). โฐ

Timeout occurs when a sender does not receive an ACK for a packet within a certain time interval. This means that either the packet or the ACK was lost due to congestion. When timeout happens, TCP assumes that the network is congested and reduces the cwnd to one segment. This triggers slow start again. ๐ŸŒ

Duplicate ACKs occur when a receiver receives an out-of-order packet. This means that some packets before it were lost due to congestion. When a receiver receives an out-of-order packet, it sends an ACK for the last in-order packet it received. If the sender receives three duplicate ACKs for the same packet, it assumes that the network is congested and performs a fast retransmit of the lost packet. It also reduces the cwnd by half and switches to fast recovery mode. In this mode, the cwnd is increased by one segment for each duplicate ACK received until a new ACK arrives. This is called multiplicative decrease. โœ–๏ธ

The following diagram illustrates how the cwnd changes over time in fast recovery mode.

Summary ๐Ÿ“

  • Congestion control in TCP aims to avoid or detect congestion and adjust the cwnd accordingly.
  • Congestion avoidance is a proactive approach that uses slow start and additive increase to increase the cwnd gradually.
  • Congestion detection is a reactive approach that uses timeout and duplicate ACKs to detect congestion and perform fast retransmit and fast recovery.
  • Congestion control in TCP helps to improve the network performance and reliability.

I hope you learned something new from this article. If you have any questions or feedback, please leave a comment below. Thank you for reading! ๐Ÿ˜Š

Top comments (0)