HTTP transaction latency-TCP impact

Source: Internet
Author: User

HTTP transaction latency-TCP impact

Recently, I have read the HTTP authoritative guide, a big part of which often reminds us of boring usage and technical indicator explanations ...... make people big. However, this book makes me feel "fresh". On the one hand, the author uses easy-to-understand languages and a large number of icons, which makes us easy to understand, on the other hand, I have always been interested in network programming and have a great match with the content of this book, the content that I want to talk about today is also about TCP in the HTTP protocol related to my own interests, which is summarized in chapter 4-"connection management.

What network latency does an HTTP request have?

Domain name resolution: domain name resolution is the first step for network access. It recognizes the domain name as an IP address recognized by TCP. This step will often cause many problems due to the quality of the domain name resolution service, the most common problem I encountered in practical engineering practice is that the quality of the selected domain name service provider is poor or the domain name resolution service address set by the client is incorrect, resulting in slow or failed domain name resolution. However, most HTTP clients now have a small DNS cache to save the IP addresses of recently accessed sites, which can effectively alleviate this problem.

Next, the client sends a TCP connection request to the server and waits for the server to return a response. Each new TCP connection has a connection establishment delay (usually only one or two seconds at most). For a single-threaded browser, if there are hundreds of concurrent HTTP transactions, it can be imagined that the time overlay value will also be very large.

Once the connection is established, the client and server will use the established TCP pipeline to send and receive requests, the TCP network latency depends on the hardware speed, the load on the network and the server, the size of the request and response packets, and the distance between the client and the server. See my article "considerations for building high-performance services".

TCP latency

TCP connection handshake

When a new TCP connection is established, the two ends of TCP exchange a series of IP groups. If the connection is only used to transmit a small amount of data, compared with this connection, HTTP performance is greatly affected.

Generally, HTTP transactions do not exchange too much data. The two handshakes of SYN/SYN + ACK produce a measurable latency, but the third handshake-The ACK groups of TCP connections are usually large enough, it can carry the entire HTTP Request Packet (the modern TCP protocol stack allows the client to send data in this validation group), and many HTTP server response packets can be placed in an IP group.

It can be seen that a small HTTP transaction may spend 50% of the time establishing a connection over TCP. Therefore, in reality, we often need to reuse HTTP connections.

TCP Delay confirmation Mechanism

We all know that the Internet itself cannot guarantee reliable packet transmission. Therefore, TCP implements its own validation mechanism to ensure data transmission is successful. The validation message is used in this validation mechanism. Because the validation message is small, TCP will return the confirmation information and the output data grouping can be combined to send more effective use of the network. To increase the possibility of identifying the same-direction transmission data group for the validation message, Many TCP protocol stacks have implemented a "latency validation" algorithm-in a specific window time (usually-milliseconds) put the output validation in the buffer to find the output data group that can be merged with it. Otherwise, the validation data will be sent separately beyond this period.

However, HTTP has a request with a double peak feature-the response behavior reduces the possibility that the response carries information. There are not so many groups to be returned in the opposite direction. Generally, the latency validation algorithm introduces a large latency, so we should adjust or disable the latency validation algorithm based on the corresponding operating system.

Note: before modifying any parameters of the TCP protocol stack, you must have a clear understanding of what you are doing. The purpose of introducing these algorithms in TCP is to prevent poor applications from damaging the network. Therefore, after modifying these configurations, you should ensure that the application will not cause problems to be avoided by these algorithms.

TCP Slow Start (congestion control)

To better protect the network, TCP Slow Start limits the number of groups that a TCP endpoint can transmit at any time. At first, TCP Data Transmission limits the transmission speed (number of transmission groups). If the data is successfully transmitted, the transmission speed (number of transmission groups) will be increased over time ). If an HTTP transaction has a large amount of data to send, it cannot send all groups at once. The congestion window must be opened gradually depending on the slow start.

Due to this congestion control feature, the transmission speed of new connections is slower than that of connections that have already exchanged a certain amount of data. In this way, we need to re-use HTTP connections (persistent connections) to improve transmission performance.

Nagle algorithm and TCP_NODELAY

If the TCP connection always sends a large number of groups containing a small amount of data, the network performance will be seriously degraded. The Nagle algorithm tries to bind a large amount of TCP data to send together before sending a group (to encourage sending full-size segments, for example, the segment size on the Ethernet is 1500 bytes, otherwise, it will be cached, or when all other groups are confirmed, the Nagle algorithm will be allowed to send non-full-size groups) to improve network efficiency.

The Nagle algorithm causes several HTTP performance problems. For example, a small HTTP packet may not be able to fill a group, so you need to wait for the cache or wait for the arrival of the confirmation group (the delay of the confirmation group is about-milliseconds ).

Therefore, HTTP applications often set the parameter TCP_NODELAY in their own protocol stacks and disable the Nagle algorithm to improve performance.

TIME_WAIT accumulation and port depletion

For an explanation of the TIME_WAIT status, please read my blog article "network programming doubts: the impact of the TCP TIME_WAIT status on server development?", In the past, the TIME_WAIT time was set to 2 sub-categories because the processing speed of earlier routers was relatively slow, but the use of high-speed routers has greatly weakened this problem, therefore, for web servers, you can set the operating system to reduce the duration of the TIME_WAIT status. Otherwise, if the server has a large number of TIME_WAIT statuses, the server performance will be greatly affected. Port depletion may occur when a few client hosts benchmark the web server, which is related to the TCP connection tuples.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.