HTTP long connections and short connections

Source: Internet
Author: User
Tags keep alive
HTTP is a text transfer protocol that works at the application level, and its implementation at the bottom is based on the TCP protocol, and today's HTTP long and short connections are essentially TCP's long connections and short connections.
long and short connections for TCP:When a TCP connection transmits data between the client and the server, it first needs to establish a communication link between the clients and the servers, and the connection can be freed if the client and server do not need to transfer the data or the link is no longer needed. TCP short connection simple, short connection is transmitted once the data is complete and then release the connection. When a connection is made between the client and the server, the client transmits the data to the server, and the server responds to the client, which is equivalent to completing a read-write process (i.e., a data transfer process), Then the connection between the client and the server is released (it can be released by either party, but usually the client), and the next time the client has to access the server again, the connection will have to be established again. TCP long connections with short connections, the connection is not immediately disconnected after a data transfer is completed between the client and the server, and if the next time the client has access to the server, the connection is used (provided this connection is still in place).
long and short connections for http:One, what is long connection and short connection short connection when the browser and server each HTTP operation, the connection is established once, when the task is terminated. If an HTML or other type of Web page that is accessed by a client browser contains other Web resources, such as JavaScript files, image files, CSS files, and so on, an HTTP session is established every time the browser encounters such a Web resource. http/1.0 in the default is the short connection long connection and short connection, long connection is when the data transmission is completed, to ensure that the TCP connection will not immediately disconnect, waiting under the same domain name to continue to use this channel transmission data. That is, in the case of a long connection, when a Web page is turned on, the TCP connection that the client can use to transmit data between servers is not closed, and if the client accesses the page on the server again, it will use the established connection. From http/1.1, a long connection is used by default. In the HTTP protocol using a long connection, the following code is added to the corresponding header: connection:keep-alive
Second, long connection time
A long connection to the client cannot be permanently connected, and it will have a hold time.
The following image is an HTTP request to visit the Baidu page when the response:
There may also be a line under the connection: Keep-alive:timeout=20, indicating that the long connection exists for a period of 20 seconds, in addition to the possibility of max=xxx, indicating that this long connection received a maximum of XXX request to disconnect.
Third, distinguish between TCP keepalive and HTTP in the Keep-alive TCP in the KeepAlive TCP in the live function, mainly to serve the server, that is, check whether the current TCP connection is still alive. Performance: When there is no data transfer in a connection for a period of time, the server sends a probe message to detect if the client host is still there. (Refer to the keep-alive timer in four timers in TCP) in http: Keep the TCP connection persistent. The two are different levels of concept.
Four, long connection and short connection advantages and disadvantages relatively short connection: for the server, the management is relatively simple, and there will be no redundant connections, there are useful connections. If client requests are frequent, it will consume too much time and bandwidth for TCP to establish and release connection operations. Long connection: From the above can be seen, long connection can save more TCP to establish and close the operation, reduce waste, save time. For customers who frequently request resources, long connections are more appropriate. However, there is a problem, the survival function of the detection cycle is too long, there is it is only to detect the survival of TCP connections, belong to a more refined approach, encounter malicious connection, the function is not enough to keep alive. In a long connected application scenario, the client side generally does not actively shut down their connection, if the connection between client and server, if not shut down, there will be a problem, as the client connection is more and more, the server sooner or later can not carry the time, At this point, the server side needs to take some policies, such as closing some long time no read and write events occur, so that some malicious connections can prevent the server side of the service damage, if the conditions can be allowed to the client machine for granularity, limit the maximum number of long connections per client, This can completely avoid the pain of some of the client's backend service.

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.