(Turn) long connection and short connection

Reprinted from: http://www.cnblogs.com/0201zcr/p/4694945.html

 

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

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

 

2. How to understand that the HTTP protocol is stateless

  The HTTP protocol is stateless, which means 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 opening a web page on that server before. HTTP is a stateless connection-oriented protocol. Statelessness does not mean that HTTP cannot maintain a TCP connection, nor does it mean that HTTP uses the UDP protocol (connectionless).

 

3. What is a long connection and a short connection?

  In HTTP/1.0, short connections are used by default . That is to say, every time the browser and the server perform an HTTP operation, a connection is established, but the connection is terminated when the task ends. If a certain HTML or other type of Web page accessed by the client browser contains other Web resources, such as JavaScript files, image files, CSS files, etc.; when the browser encounters such a Web resource, it will create a HTTP session.

But since  HTTP/1.1, persistent connections are used by default to maintain connection characteristics. Using the HTTP protocol with a long connection, this line of code will be added to the response header:

Connection:keep-alive

  In the case of using a long connection, when a web page is opened, the TCP connection used to transmit HTTP data between the client and the server will not be closed. If the client accesses the web page on the server again, it will continue to use this one established connection. Keep-Alive does not keep the connection forever, it has a keep time, which can be set in different server software (such as Apache). To implement persistent connections, both the client and the server must support persistent connections.

The long connection and short connection of the HTTP protocol are essentially the long connection and short connection of the TCP protocol.

3.1 TCP connection

  When the TCP protocol is used for network communication, a connection must be established between the server and the client before the real read and write operations. When the read and write operations are completed, the two parties can release the connection when they no longer need the connection. It requires three handshakes, and the release requires four handshakes, so the establishment of each connection requires resource consumption and time consumption

Classic three-way handshake diagram:

The classic four-way handshake closing diagram:

 

3.2 TCP short connection

  Let's simulate the situation of a short TCP connection. The client initiates a connection request to the server, the server receives the request, and then the two parties establish a connection. The client sends a message to the server, the server responds to the client, and then a read and write is completed. At this time, either side can initiate a close operation, but generally the client initiates the close operation first. Why, the general server will not close the connection immediately after replying to the client, of course, there are special cases. From the above description, short connections generally only pass one read and write operation between client/server

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

 

3.3 TCP long connection

  Next, let's simulate the situation of a long connection. The client initiates a connection to the server, the server accepts the client connection, and the two parties establish a connection. After the client and server complete a read and write, the connection between them will not be actively closed, and subsequent read and write operations will continue to use this connection.

First of all, let's talk about the TCP keep-alive function mentioned in the TCP/IP detailed explanation. The keep-alive function is mainly provided for server applications. The server application wants to know whether the client host crashes, so that it can use resources on behalf of the client. If the client has disappeared, leaving a half-open connection on the server, and the server is waiting for data from the client, the server should be far away from waiting for the client's data, the keep-alive function is to try to detect this half-open on the server side connect.

If there is no action on a given connection for two hours, the server sends a probe segment to the client, and the client host must be in one of the following four states:

  1. The client host is still running and reachable from the server. The client's TCP response is normal, and the server knows that the other party is normal. The server resets the keep-alive timer two hours later.
  2. The client host has crashed and is shutting down or being restarted. In either case, the client's TCP did not respond. The server will not receive a response to the probe and will time out after 75 seconds. The server sends a total of 10 such probes, each at 75 second intervals. If the server does not receive a response, it considers the client host closed and terminates the connection.
  3. The client host crashed and has been restarted. The server will receive a response to its keepalive probe, which is a reset, causing the server to terminate the connection.
  4. The client is running normally, but the server is unreachable. This situation is similar to 2. All TCP can find is that no response to the probe is received.

 

3.4  Operation process of long connection and short connection

The operation steps for short connection are:
establish connection - data transfer - close connection... establish connection - data transfer - close connection
The operation steps of long connection are:
establish connection - data transfer... (keep connection)...data transfer - close connection

 

4. Advantages and disadvantages of long and short connections

  It can be seen from the above that a long connection can save more TCP establishment and closing operations, reduce waste and save time . For clients that frequently request resources, long connections are more suitable. However , there is a problem here. The detection period of the keepalive function is too long , and it only detects the survival of the TCP connection, which is a relatively gentle approach. When encountering a malicious connection, the keepalive function is not enough. In the application scenario of long connection, the client side generally does not actively close the connection between them. If the connection between the client and the server is not closed, there will be a problem. As the number of client connections increases, the server Sooner or later , the server side needs to adopt some strategies, such as closing some connections that have not read and written events for a long time, so as to avoid some malicious connections causing damage to the server side service; if conditions permit, you can use The client machine is granular, and the maximum number of long connections per client is limited, which can completely avoid a troublesome client from affecting the backend service.

Short connections are simpler to manage for the server, and all existing connections are useful connections that do not require additional control means. But if client requests are frequent , time and bandwidth will be wasted on TCP setup and shutdown operations .

The generation of long connections and short connections lies in the closing strategy adopted by the client and server. Specific application scenarios adopt specific strategies. There is no perfect choice, only suitable choices.

 

5. When to use long connection and short connection?  
   Long connections are mostly used for frequent operations, point-to-point communication, and the number of connections cannot 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 greatly reduced, so each operation will not be disconnected after each operation, and data packets will be sent directly during the next processing. OK, no need to establish a TCP connection. For example, long connections are used for database connections. If short connections are used for frequent communication, socket errors will occur, and frequent socket creation is also a waste of resources. 
  
  And http services like WEB sites generally use short links , because long connections will consume a certain amount of resources for the server, and short links will be more economical for the connection of thousands or even hundreds of millions of clients as frequently as WEB sites. For some resources, if you use a long connection and there are thousands of users at the same time, if each user occupies a connection, then you can imagine it. Therefore, the amount of concurrency is large, but it is better to use a short connection if each user does not need to operate frequently.

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486306&siteId=291194637