HTTP protocol keep-alive detailed and problematic

Source: Internet
Author: User
Tags server memory
<span id="Label3"></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">1, the basic knowledge of HTTP</span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">HTTP is a <span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(255,0,0);">typical example of a request-response <span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);">pattern in which a client sends a request to the server and the server responds to that Information. </span></span><br></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;"><span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);">In the old HTTP version:</span></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;"><span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);">Each request creates a TCP connection, and when a request is answered, TCP Four waves and the connection Disconnects.<br></span></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;"><span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);">Advantages of this Model:</span></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;"><span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);">simple, easy to implement, easy to understand, and meet the characteristics of no Connection.</span></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;"><span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);">Disadvantages of this model:</span></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;"><span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);">Low Efficiency.</span></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;"><span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);"><br></span></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;"><span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(0,0,0);">http/1.0</span></span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">In this version of the protocol, if the client browser supports keep-alive, then a connection:keep-alive is added to the HTTP request header, and when the server receives a request with connection:keep-alive, A similar field is added to the response header to use the Keep-alive. In this way, the TCP connection between the client and the server is maintained, is not disconnected (beyond the time specified by keep-alive, accidental power outages, and so on), when the client sends another request to this server will still use this established Connection.</span></strong></p></p><p><p><br></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">http/1.1</span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">In the http/1.1 version, the official keep-alive usage standard is somewhat different from the http/1.0 version, where all connections are maintained by default in HTTP1.1 unless indicated in the request header or response header to be closed: Connection: Close this is why the Connection:keep-alive field is no longer meaningful. In addition, a new field keep-alive is added: because this field does not describe in detail what is used to do, ignore it.</span></strong></p></p><p><p><br></p></p><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">Not reliable (unreliable)</span></strong></p></p><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">HTTP is a stateless protocol, which means that each request is independent and keep-alive failed to change the Result. In addition, keep-alive does not guarantee that the connection between the client and the server must be active, as is the case in the HTTP1.1 Version. The only guarantee is that when the connection is closed you can get a notification, so you should not let the program rely on keep-alive to maintain the connection characteristics, otherwise there will be unintended consequences</span></strong></p></p><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><br></p></p><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">Keep-alive and Post</span></strong></p></p><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">It is stated in the HTTP1.1 rules that there cannot be any characters behind a post message body, and that the standard may not be followed for a particular browser (such as placing a CRLF character behind the post message body). As far as I know, most browsers will automatically follow a CRLF character after the post message body, How to solve this problem? Depending on the instructions above, it is forbidden to use keep-alive in a post request, or the server automatically ignores the crlf, and most of the servers are automatically ignored, but it is not possible to know whether a server will do this without testing.</span></strong></p></p><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><p style="line-height:28px;margin-top:0px;margin-bottom:10px;padding:0px;color:rgb(51,51,51);font-family:‘Hiragino Sans GB W3‘, ‘Hiragino Sans GB‘, Arial, Helvetica, simsun, u5b8bu4f53;white-space:normal;"><br></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">Easy to make Mistakes:</span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">1, http is a <span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(255,0,0);">stateless</span> connection- <span style="font-family:‘arial black‘, ‘avant garde‘;color:rgb(255,0,0);">oriented</span> protocol, stateless does not mean that HTTP can not maintain TCP connections, and can not be used on behalf of HTTP UDP protocol (no Connection)</span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">2, from http/1.1 onwards, The default is to open the keep-alive, to maintain the connection characteristics, simply said, when a Web page opens, the client and server for the transmission of HTTP data between the TCP connection will not be closed, if the client again access to the Web page on this server, will continue to use this established connection</span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">3, Keep-alive will not remain permanently connected, it has a hold time, you can set the time in different server software (such as Apache)</span></strong></p></p><p><p><br></p></p><p><p><br></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">Problem: a JSP page that takes more than 40 seconds. CSS with a large number of images in the Web page</span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">Problem Solving: The reason also looked for half a day, originally Apache configuration inside, put keep-alive switch off. This is a big problem, why the engineer to close it, the original he considered too simple, we know that Apache is suitable for short-connection requests, processing time is shorter, concurrency can go up, The original he is so considered, but there is no way, can only do so, or open the keep-alive switch bar.<br>of course, Not all cases are set KeepAlive to On.</span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">The following text summarizes the Better:</span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">In the process of using apache, the KeepAlive property I kept as the default value on, in fact, the property is set to on or off or to specific problems specific analysis, in the production environment, the impact is quite large.<br>What is the use of the KeepAlive option? If you've ever used mysql, you should know that there is a persistent Connection similar to keepalive in the connection properties of Mysql: long connection (pconnect). If this property is turned on, a TCP connection can be made more than once for the same user, increasing the response Speed.</span></strong></p></p><p><p> <strong> <span style="font-family: ' Arial black ', ' avant garde ';"> for example, Many web pages have pictures, CSS, JS, and HTML on one server, When the user accesses the HTML page, the images, Css, JS in the webpage make up the access request, and the KeepAlive property can effectively reduce the number of TCP handshake (of course, The browser has limited the number of pictures requested in the same domain, reduced the cost of the domain name interpretation), and reduced the number of httpd Processes. , thereby reducing the use of memory (assuming prefork mode). The Maxkeepaliverequests and KeepAliveTimeout two properties work at KeepAlive =on, which controls the lifetime and maximum number of service requests for a persistent connection. </span></strong></p></p><p><p><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">however, the above is only a situation, that is, static Web pages are mostly in the case, and the Web pages of other requests and Web pages on the same server. When your application dynamic program (for example: php) is mostly, the user access by the dynamic program to generate HTML content, HTML content in the image material and css, js, etc. less or hashed on other servers, KeepAlive =on will degrade Apache Performance. Why is it?<br>As mentioned earlier, KeepAlive =on, each time a user accesses a TCP connection, Apache will keep the connection for a period of time so that the connection can serve the same client continuously. Before KeepAliveTimeout expires and maxkeepaliverequests is not yet up to the threshold, Apache must have a httpd process to maintain the connection, and the httpd process is not cheap and consumes memory and CPU time Slices. If the current Apache response 100 users per second access, keepalivetimeout=5, at this time httpd process is 100*5=500 (prefork mode), a httpd process consumes 5 m of memory, is 500*5m=2500m= 2.5G, Is it exaggerated? of course, Apache has only 100 TCP connections to the Client. If your memory is large enough, the system load will not be too high, if your memory is less than 2.5G, you will use swap, frequent swap switching will increase the load on the CPU.<br>Now we turn off keepalive, Apache still responds to 100 users per second, because we separate the picture, js, css, etc., each access only 1 request, at this time the number of httpd process is 100*1=100, using memory 100*5m= 500M, this time the Apache and the client also made 100 TCP Connections. Performance has increased too much.</span></strong></p></p><p><p><br></p></p><p><p> <strong> <span style="font-family: ' Arial black ', ' avant garde ';"> summary <br> 1, when your server memory is sufficient, KeepAlive = On or off has little effect on system performance. <br> 2, when the static pages on your server (Html, pictures, Css, Js) are mostly, it is recommended to open Keepalive. <br> 3, When your server is more dynamic requests (because connected to the database, more access to the file system), KeepAlive off, will save a certain amount of memory, save memory just as the file system cache (vmstat command in the cache column), reduce I/O Pressure. <br> PS: when the keepalive =on, the KeepAliveTimeout setting is actually a problem, set too short, will lead to Apache frequent connection, to the CPU pressure, set too long, the system will accumulate useless HTTP connection, Consume a lot of memory, how much to set, can be constantly adjusted, depending on your site browsing and server Configuration. </span></strong></p></p><p><p><br><strong><span style="font-family:‘arial black‘, ‘avant garde‘;">Reduce the cost of domain name interpretation<br>For http/1.0, can take full advantage of the browser default maximum number of concurrent connections more than http/1.1 benefits, to achieve the cost of not adding new domain name and higher parallel download, reduce the cost of domain name interpretation (note: IE 6,7 in http/1.0 the default maximum number of concurrent connections is 4, in http/ 1.1 The default maximum number of concurrent connections for 2,IE8 is 6,firefox2 in http/1.0, the default maximum number of concurrent connections is 2 in http/1.1, the default maximum number of concurrent connections is 8,firefox 3 default is 6), According to the July 10 Google index 4.2 billion pages of the statistical report, each page contains 29.39 images, 7.09 external scripts, 3.22 external CSS style sheets, if the keep-alive and reasonable control keep-alive Timeout This parameter can save a lot of connection cost, improve the corresponding Speed. If the settings are not good, in the case of large concurrency, because of the maintenance of a large number of connections to the server resources exhausted, and for most of the current domestic users are still ie6,7 in the case of the shutdown keep-alive can take full advantage of the browser default maximum number of concurrent connections to achieve the benefits of no additional overhead page display Quickly.</span></strong></p></p><p><p>This article from "momo is spicy moe" blog, Please be sure to keep this source http://momo462.blog.51cto.com/10138434/1834127</p></p><p><p>HTTP protocol keep-alive detailed and problematic</p></p></span>

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.