HTTP long connections and short connections

Source: Internet
Author: User
Tags keep alive

1. The relationship between HTTP protocol and TCP/IP protocol

Long and short connections of HTTP are essentially TCP long connections and short connections. HTTP belongs to the Application layer protocol, using IP protocol at the network layer, mainly solves the network routing and addressing problem, uses the TCP protocol in the transport layer, mainly solves how to transmit the data packet reliably on the IP layer, causes all packets which the originator sends out on the other end of the network, and the order is consistent with the issuing order, has the reliable Connection-oriented features.

2. How to understand the HTTP protocol is stateless

The HTTP protocol is stateless, meaning that the protocol has no memory capability for transaction processing, and the server does not know what state the client is. That is, there is no connection between opening a Web page on a server and the pages you previously opened on the server. HTTP is a stateless, connection-oriented protocol, and stateless does not mean that HTTP does not maintain TCP connections, nor is UDP protocol (connectionless) used on behalf of HTTP.

3. What is long connection, short connection.

  in http/1.0, a short connection is used by default . That is, each time the browser and the server perform an HTTP operation, the connection is established once and 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 whenever such a web resource is encountered.

from http/1.1, a long connection is used by default to maintain the connection characteristics. The HTTP protocol using a long connection will be added in the response header: Connection:keep-alive. In the case of a long connection, when a Web page is turned on, the TCP connection between the client and the server for transmitting HTTP data is not closed, and if the client accesses the Web page on the server again, it will continue to use the established connection. Keep-alive does not permanently connect, it has a hold time that can be set in different server software (such as Apache).

The long connection and short connection of the HTTP protocol are essentially long and short connections of the TCP protocol. Implementing an HTTP long connection must be supported by both the client and the server.


3.1 TCP Connections

When the TCP protocol is used in network communication, before a true read-write operation, a connection must be established between the server and the client, and when the read and write operation is complete, they can release the connection when they no longer need the connection, and the connection is established with a handshake of three times, and the release takes 4 shakes. So each connection is built to require resource consumption and time consumption.

Classic three-time handshake schematic:

Classic four-time handshake close diagram:

3.2 TCP Short connection

We simulate the TCP short connection, the client initiates a connection request to the server, the server receives the request, and the two sides establish the connection. The client sends a message to the server, and the server responds to the client, a read-write complete.

At this point, either side can initiate a close operation, but usually the client initiates the close operation. Why, the general server will not reply to the client immediately after the shutdown of the connection, of course, do not exclude special circumstances. From the above description, short connections typically only pass a read-write operation between Client/server

The advantage of a short connection is that it is simpler to manage, the connections that exist are useful connections, and no additional control is required.

3.3 tcp Long connection

Next we simulate the long connection, the client initiates a connection to the server, the server accepts the client connection, and the two sides establish the connection. Client sends information to server, server responds to client, once read and write. Once read-write is completed, the connection between them is not actively closed and subsequent read and write operations continue to use the connection.

First of all, the TCP/IP explained in detail on the TCP live function, the main function for the server application, server applications want to know whether the customer host crashes, so that customers can use resources on behalf of. If the customer has disappeared, leaving a half-open connection on the server, and the server is waiting for data from the client, the server will always wait for the client's data, which is trying to detect the Half-open connection on the server side.

If a given connection does not have any action within two hours, the server sends a probe message segment to the client, and the client host must be in one of the following 4 states: The client host is still running and can be reached from the server. The client's TCP response is normal, and the server is aware that the other is normal, the server in two hours after the live timer reset. The client host has crashed and is shutting down or restarting. In either case, the client's TCP is not responding. The server will not be able to receive a response to the probe and timeout after 75 seconds. A total of 10 such probes are sent by the server, each interval of 75 seconds. If the server does not receive a response, it believes that the client host has shut down and terminated the connection. The client host crashed and has restarted. The server will receive a response to its live probe, which is a reset that causes the server to terminate the connection. The client is running normally, but the server is unreachable, and this is similar to 2, where TCP can find no response from the probe.

3.4 Long Connection Short connection operation procedure

The operation procedure of short connection is: Establish connection--data transfer--close the connection ... Establish a connection--data transfer--Close the connection

The operation step of long connection is: Establish connection--data transfer ... (Stay Connected) ... Data transfer--close connection

4. Advantages and disadvantages of long connection and short connection

It can be seen from the above, long connection can save more TCP establishment and shutdown 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.

Short connections are simpler to manage for servers, connections that exist are useful connections, and no additional control is required. However, if the client requests frequently , it will waste time and bandwidth on TCP's establishment and shutdown operations .

Long connection and short connection is the result of the client and server to take the shutdown strategy, specific application scenarios using specific strategies, there is no perfect choice, only the right choice.

5. When to use long connection, short connection.

long connections are often used for frequent operation, point-to-point communication, and the number of connections cannot be too much. Each TCP connection requires a three-step handshake, which takes time, if each operation is the first connection, then the processing speed will be reduced a lot, so after each operation is constantly open, the next time processing directly send packets on OK, do not establish a TCP connection. For example: A database connection with a long connection, if the use of short connection frequent communication will cause socket errors, and frequent socket creation is also a waste of resources.

HTTP services like Web sites generally use short links , because long connections consume a certain amount of resources for the server, the connections of thousands or even billions of clients, such as Web sites, are more resource-intensive, if connected with long, and thousands of users, Imagine if each user uses a connection. Therefore, a large number of concurrent, but each user without frequent operation of the need for a short link good.


Article Source: Whywin

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.