TCP Flow Control and Congestion Control
Problem Description
TCP's Flow Control and Congestion Control are core mechanisms that ensure network reliability and stability, but they address different problems. Interviews often require explaining their differences, working principles, and specific algorithms.
I. Flow Control: Addressing the Receiver's Insufficient Processing Capacity
Goal: Ensure the sender does not send too much data, causing the receiver's buffer to overflow.
Core Mechanism: Sliding Window Protocol, which dynamically adjusts the sending window based on the receiver's Advertised Window.
Step-by-step Breakdown:
-
Receiver Advertises Window Size
- Each time the receiver sends an ACK segment, it informs the sender of the current remaining buffer size (i.e., the receive window, rwnd) via the
windowfield in the TCP header. - Example: If the receiver's buffer has 100 bytes remaining, it sends rwnd=100.
- Each time the receiver sends an ACK segment, it informs the sender of the current remaining buffer size (i.e., the receive window, rwnd) via the
-
Sender Limits Data Sent Based on rwnd
- The sender must ensure: Amount of data sent but not acknowledged ≤ min(congestion window cwnd, receive window rwnd).
- If rwnd=0, the sender pauses transmission and starts a Persist Timer to periodically probe if the receiver's window has recovered.
-
Avoiding Deadlock (Zero Window Handling)
- When the receiver's buffer is full (rwnd=0), the sender stops sending.
- If the receiver later frees up buffer space and sends an ACK with an updated rwnd, but this ACK is lost, both sides enter a waiting state.
- Solution: After receiving rwnd=0, the sender starts the persist timer. Upon timeout, it sends a 1-byte probe segment to trigger the receiver to reply with the latest rwnd.
Example:
- Sender's initial window is 300 bytes, receiver's rwnd=100.
- Sender can send up to 100 bytes, then waits for ACK; after processing 50 bytes, the receiver replies with an ACK and rwnd=50, allowing the sender to send another 50 bytes.
II. Congestion Control: Addressing Network Overload
Goal: Prevent the sender from overusing network resources, leading to router buffer overflow (congestion collapse).
Core Mechanism: Dynamically adjusts the sending rate via the Congestion Window (cwnd), using various algorithms to explore available bandwidth in phases.
Classic Algorithm: TCP Reno (includes 4 phases)
-
Slow Start
- Initial cwnd = 1 MSS (Maximum Segment Size). For each ACK received, cwnd doubles (exponential growth).
- Purpose: Quickly probe available bandwidth.
- Termination Conditions:
- Reaches the slow start threshold (ssthresh);
- Congestion occurs (timeout or duplicate ACKs).
-
Congestion Avoidance
- When cwnd ≥ ssthresh, enter the linear growth phase: increase by 1 MSS per RTT (Round-Trip Time) (increase 1/cwnd per ACK).
- Purpose: Cautiously approach the network capacity limit.
-
Fast Retransmit
- Upon receiving 3 duplicate ACKs, immediately retransmit the lost segment without waiting for a timeout.
- Implication: Some data reached the receiver, indicating the network still has capacity, so no drastic speed reduction is needed.
-
Fast Recovery
- After triggering Fast Retransmit, set ssthresh to half of the current cwnd, and cwnd = ssthresh + 3 MSS (because 3 duplicate ACKs indicate 3 data packets have left the network).
- Then enter the Congestion Avoidance phase (linear growth).
Complete Process Example:
- Initial cwnd=1 MSS, ssthresh=16 MSS.
- Slow Start: cwnd grows exponentially to 16 MSS.
- Congestion Avoidance: cwnd grows linearly to 20 MSS, then 3 duplicate ACKs are received (indicating packet loss).
- Fast Retransmit & Fast Recovery: Retransmit the lost packet, set ssthresh=10 MSS, cwnd=13 MSS, and enter Congestion Avoidance.
III. Collaboration Between Flow Control and Congestion Control
- Actual sending window size = min(rwnd, cwnd).
- Flow control depends on receiver capacity, congestion control depends on network state; both jointly constrain sending behavior.
Key Differences:
| Aspect | Flow Control | Congestion Control |
|---|---|---|
| Focus | Receiver Buffer | Network Carrying Capacity |
| Control Signal | Receiver's Advertised Window (rwnd) | Congestion Window (cwnd) |
| Trigger | Buffer Full | Packet Loss or Increased Delay |
Through the above mechanisms, TCP ensures reliable transmission while balancing fairness and network efficiency.