1. The relationship between the HTTP protocol and the TCP/IP protocol
Long connections and short connections to HTTP are essentially TCP long connections and short connections. HTTP belongs to the Application layer protocol, which uses the TCP protocol at the Transport Layer and IP protocol at the network layer. The IP protocol mainly solves the network routing and addressing problems, the TCP protocol mainly solves how to reliably pass the packet over the IP layer, so that all the packets sent by the originator are received on the other end of the network, and the order is consistent with the order of issuing. TCP has a reliable, connection-oriented feature.
2. How to understand that the HTTP protocol is stateless
The HTTP protocol is stateless, meaning that the protocol has no memory capability for transactions, 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 have previously opened on this server. HTTP is a stateless, connection-oriented protocol, and stateless does not mean that HTTP cannot maintain TCP connections, nor does it use the UDP protocol (no connection) on behalf of HTTP.
3. What is long connection, short connection?
In http/1.0, a short connection is used by default. That is, the browser and the server each HTTP operation, the connection is established, but the end of the task to disconnect. If a client browser accesses an HTML or other type of Web page that contains other Web resources, such as JavaScript files, image files, CSS files, and so on, when the browser encounters such a Web resource, an HTTP session is established.
However, from http/1.1, a long connection is used by default to maintain the connection characteristics. Using a long-connected HTTP protocol, this line of code is added to the response header:
In the case of a long connection, when a Web page opens, the TCP connection between the client and the server for transmitting HTTP data does not close, and if the client accesses the Web page on the server again, it will continue to use this established connection. Keep-alive does not permanently keep the connection, it has a hold time that can be set in different server software (such as Apache). Long connections are supported for both the client and the server.
The long and short connections of the HTTP protocol are essentially long connections and short connections to the TCP protocol.
3.1 TCP connections
When the TCP protocol is used in network communication, before the actual read and write operation, the server and the client must establish a connection, when the read and write operation is completed, the two sides no longer need the connection when they can release the connection, the connection is established to need three handshake, and release will require 4 handshake, So the establishment of each connection requires resource consumption and time consumption.
Classic three-time handshake:
Classic four-time handshake close diagram:
3.2 TCP Short Connection
We simulate the case of a 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, the server responds to the client, and then a read-write is completed, and either of the parties 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 closing the connection, of course, do not rule out a special situation. From the above description, short connections generally only pass a read and write operation between Client/server
The advantage of short connections is that they are simpler to manage, that existing connections are useful connections, and that no additional control is required
3.3 TCP Long Connections
Next we simulate the case of a long connection, the client initiates a connection to the server, the server accepts the client connection, and the two sides establish the connection. After the client and server have completed a read and write, 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 feature described in the TCP/IP details, the KeepAlive function is mainly provided for the server application, the server application wants to know whether the customer host crashes, which can be used on behalf of the customer resources. If the customer has disappeared, leaving a semi-open connection on the server while the server is waiting for data from the client, the server should be waiting for the client's data, and the KeepAlive function is trying to detect this semi-open connection on the server side.
If a given connection does not have any action within two hours, the server sends a probe segment to the client and the client host must be in one of the following 4 states:
- The client host is still functioning and can be reached from the server. The client's TCP response is normal, and the server knows the other side is normal, the server after two hours will be keepalive 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 receive a response to the probe and time out after 75 seconds. The server sends a total of 10 such probes at each interval of 75 seconds. If the server does not receive a response, it considers the client host to be closed and terminates the connection.
- The client host crashed and has restarted. The server receives a response to its keepalive probe, which is a reset that causes the server to terminate the connection.
- The client runs normally, but the server is unreachable, which is similar to 2, where TCP can find no response to the probe.
4. Advantages and disadvantages of long connections and short connections
As can be seen from the above, long connections can eliminate more TCP setup and shutdown operations, reduce waste, save time. For customers who frequently request resources, they are more suitable for long connections. 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, is a relatively gentle practice, encounter malicious connection, the keepalive function is not enough. In the case of long-connected applications, the client side generally does not actively shut down their connection between the client and the server if the connection is not closed, there will be a problem, as the client connection more and more, the server sooner or later can not carry the time, At this time, the server side needs to take some policies, such as the closure of some long-term no read and write events occurred connection, so as to avoid some malicious connection caused server-side service damage, if the condition is allowed to the client machine for granularity, limit the maximum number of connections per client, This can completely avoid an egg-hurting client from compromising the backend service.
Short connections are simpler for the server to manage, and the connections that exist are useful connections and do not require additional control. However, if customer requests are frequent, time and bandwidth will be wasted on TCP setup and shutdown operations.
The generation of long connections and short connections is based on client and server shutdown policies, specific application scenarios with specific strategies, no perfect choice, only the right choice.
HTTP long and short connections