1. Relationship between HTTP and TCP/IP

HTTP long and short connections are essentially TCP long and short connections. HTTP is an application-layer protocol. It uses TCP at the transport layer and IP at the network layer. The IP protocol mainly solves the problem of network routing and addressing, while the TCP protocol mainly solves the problem of how to transmit packets reliably on the IP layer, so that the receiver on the network receives all packets sent by the sender, and the order is consistent with the order of sending. TCP is reliable and connection-oriented.

2. How to understand that HTTP is stateless

The HTTP protocol is stateless, which means that the protocol has no memory for transaction processing and the server does not know the state of the client. That is, there is no connection between opening a web page on a server and opening a web page on that server last time. HTTP is a stateless connection oriented protocol. Stateless does not mean that HTTP cannot maintain TCP connections, nor does it mean that HTTP uses UDP (connectionless).

3. What are long connection and short connection?

Short connections are used by default in HTTP/1.0. That is, each time the client and server perform an HTTP operation, a connection is established and broken at the end of the task. When the client browser accesses an HTML or other type of Web page that contains other Web resources (such as JavaScript files, image files, CSS files, etc.), the browser re-establishes an HTTP session each time it encounters such a Web resource.

From HTTP/1.1 onwards, long connections are used by default to preserve the connection feature. HTTP with long connections adds this line to the response header:

Connection:keep-alive
Copy the code

In the case of a long connection, when a web page is opened, the TCP connection between the client and the server for the transmission of HTTP data is not closed. When the client accesses the server again, it continues to use the established connection. Keep-alive does not hold a connection forever, it has a hold time that can be set in different server software such as Apache. To implement persistent connections, both clients and servers must support persistent connections.

HTTP long connection and short connection are essentially TCP long connection and short connection.

3.1. A TCP connection

When the TCP protocol is used for network communication, a connection must be established between the client and server before the read/write operation. When the read/write operation is complete and the connection is no longer needed, the connection can be released. The establishment of a connection depends on a three-way handshake and the release of a connection requires a four-way handshake, so the establishment of each connection requires both resource consumption and time consumption.

The classic three-way handshake establishes a connection:

The classic four-way handshake closes the connection:

3.2. TCP short connection

The following describes how to simulate a TCP short connection: THE client sends a connection request to the server, the server receives the request, and the two parties establish a connection. The client sends a message to the server, the server responds to the client, and the request is completed in one go. Either side can initiate a close, but the client usually initiates the close first. As you can see from the above, the short connection usually only passes one request operation between the client/server.

The advantage of short connection is that it is relatively easy to manage, and all existing connections are useful connections, without additional control means.

3.3. TCP Long Connection

The client initiates a connection to the server, the server accepts the connection, and the two parties establish a connection. After completing a request, the connection between the client and the server will not be actively closed. Subsequent read and write operations will continue to use the connection.

The TCP keepalive function is mainly provided for server applications. If the client has disappeared and the connection is not disconnected, the server is left with a semi-open connection while the server is waiting for data from the client, and the server waits for data from the client forever. The keepalive function attempts to detect this semi-open connection on the server side.

If there is no action on a given connection for two hours, the server sends a probe packet segment to the client, probing four client states based on the client host response:

  • The client host is still up and running and the server is reachable. The client’s TCP response is normal, and the server resets the keepalive timer.
  • The client host has crashed and is down or restarting. In these cases, the client cannot respond to TCP. The server will not receive the client’s response to the probe. The server sends a total of 10 such probes, each 75 seconds apart. If the server receives no response, it assumes that the client is closed and terminates the connection.
  • The client crashed and has been restarted. The server will receive a response to its keepalive probe, which is a reset that causes the server to terminate the connection.
  • The client is running, but the server is unreachable. This is similar to the second state.

4. Advantages and disadvantages of long connection and short connection

As can be seen from the above, the long connection can save many TCP establishment and closure operations, reducing waste and saving time. Long connections are suitable for clients that frequently request resources. In the long connection scenario, the client does not actively close the connection. If the connection between the client and server is kept open, the server will keep too many connections as the number of client connections increases. In this case, the server needs to take some policies, for example, close some connections that do not receive requests for a long time to prevent some malicious connections from damaging the server. If possible, you can limit the maximum number of long connections per client, which completely prevents malicious clients from dragging down the entire backend service.

Short connections are easy to manage for the server. All existing connections are useful connections, and no additional control is required. However, if the client requests frequently, it will waste a lot of time and bandwidth on TCP setup and shutdown operations.

The generation of long and short connections depends on the shutdown policy adopted by the client and server. Different policies are applicable to different application scenarios.

As can be seen from the above, the long connection can save many TCP establishment and closure operations, reducing waste and saving time. Long connections are preferred for customers who frequently request resources. However, there is a problem, the survival function is too long, and it only detects the survival of TCP connections, which is a relatively polite practice, in the case of malicious connections, the survival function is not enough. In the long connection scenario, the client will not actively close the connection between the client and server. If the connection between the client and server is not closed, there will be a problem. With the increasing number of client connections, the server will fail sooner or later. For example, close some connections that do not have read or write events for a long time to avoid damage to server services caused by malicious connections. If the condition is allowed, it can limit the maximum number of long connections to each client based on the granularity of the client machine, so that a painful client can completely avoid the backend service.

Short connections are easy to manage for the server. All existing connections are useful connections, and no additional control is required. However, if the client requests frequently, time and bandwidth will be wasted on TCP setup and shutdown operations.

The generation of long and short connections depends on the shutdown policies adopted by the client and server. Specific policies are adopted in specific application scenarios. There is no perfect choice, but only appropriate choice.

Long connection Short connection process

The procedure for short connection is as follows:Copy the code
Establishing a connection -- Transferring data -- closing a connection... Establish a connection -- transfer data -- close a connectionCopy the code
The procedure for long connection is as follows:Copy the code
Establishing a connection -- data transfer... (Keep connected)... Data transfer -- Closes the connectionCopy the code

When to use long connection, short connection?

Long connections are used for frequent, point-to-point communication, and the number of connections should not be too many. Each TCP connection requires a three-step handshake, which takes time. If each operation is connected first and then operated, the processing speed will be much lower. Therefore, after each operation is finished, it is OK to send packets directly, and there is no need to establish TCP connection. For example, if the database is connected to a long connection, frequent communication with a short connection will cause socket errors, and frequent socket creation is also a waste of resources. And like the WEB site of the HTTP service with short links, as long it takes a certain connection to the server resources, and as frequently as the WEB site of thousands or even hundreds of millions of client connection with short connection will be more save some resources, if with long connection, and hundreds of thousands of users at the same time, if each user to take up a connection, Imagine that. So the concurrency is large, but each user does not need to operate frequently under the condition of short connection.

HTTP and socket long connection and short connection difference

http://www.jianshu.com/p/b68d2b26f5f4

HTTP long and short connections

http://blog.jobbole.com/104108/

HTTP persistent connection

https://zh.wikipedia.org/wiki/HTTP%E6%8C%81%E4%B9%85%E8%BF%9E%E6%8E%A5

A long connection

http://baike.baidu.com/view/2831907.htm