In my last article, I mentioned the Nagle algorithm to solve the problem of low network utilization caused by small data in the header, which actually introduces new problems. In addition, let’s take a look at TCP will also have what optimization strategy! This article is purely a learning record, any imperfections or mistakes will be appreciated. If you have friends who are misled, please forgive me.


First we first kangkangnagle algorithm

Nagle algorithm rules

  • (1) If the packet length reaches MSS, it is allowed to send;
  • (2) If the packet contains FIN, it is allowed to send;
  • (3) if the TCP_NODELAY option is set, sending is allowed;
  • (4) If the TCP_CORK option is not set, if all sent small packets (packet length less than MSS) are confirmed, the packet is allowed to be sent;
  • (5) If none of the above conditions is met, but a timeout (generally 200ms) occurs, it will be sent immediately.

Delay ACK

In the ACK mechanism, after receiving a packet, the recipient checks whether it needs to respond to an ACK immediately. Otherwise, the ACK logic is delayed. Reference, the advantages are obvious, improve the utilization rate of network channel

When Nagle encounters a delayed ACK

Imagine a scenario where the MSS is 8 Chinese characters (maximum packet length). User A needs to send two application-layer packets to User B. Hello! “I’m a.” Obviously, the two messages are less than 8, but “Hello” meets the fourth rule of Nagel, so it will be sent out at the first time. Due to the ACK delay, A has not received the confirmation of B. Wait until the timeout is sent. This creates a significant delay in resolution

  • Turn off the Nagle algorithm and use the TCP socket option TCP_NODELAY to turn off the socket option (not recommended, there are more optimization strategies)
  • Using writev instead of calling write twice, a single writev call will result in TCP output once instead of twice, producing only one TCP section, which is the preferred method

Slide window for flow control

The sliding window is designed to balance the rate mismatch between sender and receiver, and the sending window is negotiated by both parties when the connection is established. However, in the process of communication, the receiver feedback its buffer size to dynamically adjust the size of the sending window (buffer)

Congestion control

For convenience, we assume that host A transmits data to host B. As we know, when two hosts transmit data packets, if the sender does not receive ACK feedback from the receiver, the sender will think that the packet it sends is lost, and will re-transmit the lost packet. However, the actual situation may be that too many hosts are using channel resources at this time, resulting in network congestion, and the data packets sent by A are blocked on the way and fail to reach B. At this time, A mistakenly thinks that packet loss occurs and retransmits the packet. As a result, channel resources are wasted and the network becomes more congested. Therefore, we need congestion control

Slow start and congestion avoidance

  • How is the size of the congestion window determined after the connection is established? (Note the difference between congested Windows and sliding Windows)
    • The first strategy is to send a packet the first time, +1 if it is not lost, and so on
    • The second strategy is to send a packet the first time, multiply it by two if it is not lost, and so on

    In fact, the first method grows too slowly to quickly adapt to network congestion, while the second method grows exponentially and easily reaches the congestion threshold.

    So take the length of the two, use exponential growth in the early stage, when a certain number of linear growth

    But either exponential or linear growth eventually reaches a MAX value, at which point it restarts at 1 and sets the threshold to MAX/2

    We call the exponential growth phase in determining the size of the congestion windowSlow startThe linear growth phase is calledCongestion avoidance

Fast retransmission and fast recovery

As mentioned earlier, the slow start process is restarted when network congestion occurs

  • How to determine network congestion?
    • When the network is congested, a large number of packets will be lost
  • Timeout retransmission (packet loss) must be network congestion?
    • Network congestion causes a large number of packets to trigger timeout retransmission events. When a single packet is damaged or lost, timeout retransmission will occur. Therefore, timeout retransmission does not necessarily mean network congestion
  • How to determine the cause of timeout retransmission
    • Network congestion leads to a large number of packet loss
    • For a single packet loss, due to the delayed ACK rule, the receiver responds with the same ACK for each packet that arrives later. Therefore, when the sender receives three identical ACKS, a single packet loss occurs

Single packet loss event. When the sender receives three acks with the same id, the sender does not wait for the timeout of the ACK-1 packet and resends the packet immediately. The current threshold is set to MAX, and the new threshold is MAX/2. The congestion window = MAX/2 is used for the growth and retransmission stage, which is called fast retransmission, and the window parameter adjustment stage is called fast recovery