Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Nagel algorithm?

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the principle of Nagel algorithm". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

TCP is a complex protocol, and each mechanism brings advantages as well as other problems. Nagel algorithm and delay ack mechanism are two mechanisms to reduce the amount of packets at the sender and receiver, which can effectively reduce the amount of network packets and avoid congestion. However, in a specific scenario, the Nagel algorithm requires that there is only one unacknowledged packet in the network, and the delay ack mechanism needs to wait for more packets before sending ACK packets, which causes the sender and receiver to wait for the other side to send data, resulting in deadlock. Only when the delay ack timeout can be unlocked, resulting in high external delay on the application side. Other texts have introduced the relevant mechanisms, and there have been some articles on the scenario of this delay. Combined with the specific tcpdump package, this paper analyzes the scenario of triggering delay ack, the relevant kernel parameters, and the scheme of evasion.

Background

A proxy layer is added to the redis. During the pressure test, it is found that for the write command, when the data length is greater than 2k, the performance degradation is very obvious, only the directly connected redis-server 1max 10. The impact of get requests is not so obvious.

Analysis.

Observing the load of the system and the amount of network packets are relatively low, the amount of network packets is also relatively small, and the time-consuming within the proxy is also relatively short. Scoundrels can only sacrifice tcpdump magic, as expected, there are evil spirits.

Tcp request packet No. 22. The server returned ack only after 42ms. The initial suspicion is that the delay in the network layer has led to an increase in time. Looking for information on Google and km, the general explanation is as follows: because the client opens the Nagel algorithm, the server does not turn off delay ack, which will cause delay ack timeout, and then send ack, resulting in timeout.

Principle

Nagel algorithm

If there is new data to send if the window size > = MSS and available data is > = MSS send complete MSS segment now else if there is unconfirmed data still in the pipe enqueue data in the buffer until an acknowledge is received else send data immediately end ifend ifend if

In a nutshell, the rules of the Nagel algorithm are:

If the content sent is greater than 1 MSS, send it immediately

If no previous packet has not been confirmed, send it immediately

If there is a packet that has not been confirmed before, cache the content sent

If an ack is received, the cached content is sent immediately.

The source code of delay ACK is as follows: net/ipv4/tcp_input.c

The basic principles are:

If the received data content is larger than a MSS, send ACK

If the data is received as expected by the receiving window, send ACK

If you are in quick mode, send ACK

If disordered data is received, send ACK

Other, delay sending ACK

Everything else is relatively clear, how does quick mode judge? Move on to the code:

One factor that affects quick mode is the state of ping pong. Pingpong is a state value, which is used to identify the status of the current tcp interaction to predict whether it is an interactive communication mode like W-R-W-R-W-R. If it is, delayed ack can be used to piggyback the return packet of Write to the sender by using the return packet of Read.

As shown in the figure above, the default pingpong = 0 means that the server returns ACK immediately after receiving the data. When there is a data response from the server, the server will pingpong = 1. In subsequent interactions, the server will not immediately return ack, but wait for data or ACK to respond after timeout.

problem

According to the previous principle analysis, there should be ACK latency every time, why is it that when we test data less than 2K, the performance is not affected?

Continue to analyze the tcpdump package:

According to the Nagel algorithm and delayed ACK mechanism, the above interaction is shown in the following figure. Since the data that occurs each time contains a complete request, when the server returns the command response to the client after processing, it copies the requested ACK to the client to save a network packet.

Then analyze the 2K scenario:

Trigger scene

The data requested by a tcp cannot generate a response on the server, or is less than a MSS

Circumvention scheme

Only when the client opens the Nagel algorithm at the same time, the server opens the tcp_delay_ack will cause the previous deadlock state. The solution can start at both ends of the TCP.

Server:

Turn off tcp_delay_ack so that each tcp request packet will have an ack to respond in a timely manner without delay. Mode of operation:

Echo 1 > / proc/sys/net/ipv4/tcp_no_delay_ack

However, each tcp request returns an ack packet, resulting in an increase in the number of network packets. After disabling tcp delay confirmation, the network packet volume has increased by about 80%, and the impact is still obvious during the peak period.

Set the TCP_QUICKACK property. But you need to set it again after each recv. It is not suitable for our scenario, so you need to modify the server redis source code.

Client:

Turn off the nagel algorithm, that is, set the socket tcp_no_delay property.

Static void _ set_tcp_nodelay (int fd) {int enable = 1setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (void*) & enable, sizeof (enable));}

Avoid writing multiple times and then reading, merging writes into one large packet; avoid sending a request into multiple packets, and the initial packet sent is less than one MSS. For our scenario, the 1424 bytes of packet No. 22 are cached and sent out when it is larger than one MSS. The server immediately returns the response, and the client continues to send subsequent data to complete the interaction to avoid delay.

This is the end of the content of "what is the principle of Nagel algorithm". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report