What are HTTP long connections and short connections?

What are HTTP long connections and short connections?

 

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 transmit data packets on the IP layer, so that the receiving end on the network receives all the packets sent by the sending end, and the order is consistent with the sending order. The TCP protocol is reliable and 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 the last time you opened a web page on this server . 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?

Short connections are used by default in HTTP/1.0 . That is to say, every time the client and the server perform an HTTP operation , a connection is established , and the connection is terminated when the task ends . When 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.), every time such a Web resource is encountered, the browser will re-create an HTTP session.

Since HTTP/ 1.1 , persistent connections are used by default to maintain connection characteristics . Using the long-connected HTTP protocol , 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 . When the client accesses the server again, it will continue to use the 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). Implementing a persistent connection requires both the client and the server to 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 connections

When the TCP protocol is used for network communication, a connection must be established between the client and the server before the real read and write operations. After the read and write operations are completed, the connection can be released when the two parties no longer need the connection. The establishment of the connection relies on the "three-way handshake", and the release requires the "four-way handshake", so the establishment of each connection requires resource consumption and time consumption.

A schematic diagram of a classic three-way handshake to establish a connection:

The classic four-way handshake closes the connection diagram:

3.2. TCP short connection

Simulate the situation of a TCP short 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 request is completed. At this time, both parties can initiate the close operation at will, but generally the client initiates the close operation first. As can be seen from the above, short connections generally only pass a request operation between client/server.

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

3.3. TCP long connection

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 the server complete a request, the connection between them will not be actively closed, and subsequent read and write operations will continue. use this link.

The keep-alive function of TCP is mainly provided for server applications. If the client has disappeared and the connection is not disconnected, it will leave a half-open connection on the server, while the server is waiting for data from the client, and the server will wait forever for the client's data. The keep-alive function tries to detect this half-open connection on the server side.

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

  • The client host is still up and running and the server is reachable. At this time, the client's TCP response is normal, and the server resets the keep-alive timer.
  • The client host has crashed and is shutting down or being restarted. In the above 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 at 75 second intervals. If the server does not receive any response, it considers the client closed and terminates the connection.
  • The client crashed and has restarted. The server will receive a response to its keepalive probe, which is a reset, causing the server to terminate the connection.
  • The client is functioning normally, but the server is unreachable. This situation is similar to the second state.

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. Long connections are suitable for clients that frequently request resources. In the application scenario of a long connection, the client generally does not actively close the connection. When the connection between the client and the server is not closed, as the number of client connections increases, the server will maintain too many connections. At this time, the server side needs to adopt some strategies, such as closing some connections that have not been requested for a long time, so as to avoid some malicious connections causing damage to the server-side service; if conditions permit, you can limit the maximum number of long connections per client, which can Completely avoid malicious clients from dragging down the overall 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. However, if the client requests frequently, it will waste more time and bandwidth on the establishment and shutdown of TCP.

The generation of long connections and short connections lies in the closing strategy adopted by the client and server. Different application scenarios are suitable for different strategies.

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 clientrequests 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.

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

When to use long connection, 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.

 

The difference between long connection and short connection between http and socket

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

 

HTTP long and short connections

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

 

HTTP persistent connections

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

 

Long connection

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

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 transmit data packets on the IP layer, so that the receiving end on the network receives all the packets sent by the sending end, and the order is consistent with the sending order. The TCP protocol is reliable and 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 the last time you opened a web page on this server . 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?

Short connections are used by default in HTTP/1.0 . That is to say, every time the client and the server perform an HTTP operation , a connection is established , and the connection is terminated when the task ends . When 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.), every time such a Web resource is encountered, the browser will re-create an HTTP session.

Since HTTP/ 1.1 , persistent connections are used by default to maintain connection characteristics . Using the long-connected HTTP protocol , 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 . When the client accesses the server again, it will continue to use the 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). Implementing a persistent connection requires both the client and the server to 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 connections

When the TCP protocol is used for network communication, a connection must be established between the client and the server before the real read and write operations. After the read and write operations are completed, the connection can be released when the two parties no longer need the connection. The establishment of the connection relies on the "three-way handshake", and the release requires the "four-way handshake", so the establishment of each connection requires resource consumption and time consumption.

A schematic diagram of a classic three-way handshake to establish a connection:

The classic four-way handshake closes the connection diagram:

3.2. TCP short connection

Simulate the situation of a TCP short 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 request is completed. At this time, both parties can initiate the close operation at will, but generally the client initiates the close operation first. As can be seen from the above, short connections generally only pass a request operation between client/server.

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

3.3. TCP long connection

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 the server complete a request, the connection between them will not be actively closed, and subsequent read and write operations will continue. use this link.

The keep-alive function of TCP is mainly provided for server applications. If the client has disappeared and the connection is not disconnected, it will leave a half-open connection on the server, while the server is waiting for data from the client, and the server will wait forever for the client's data. The keep-alive function tries to detect this half-open connection on the server side.

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

  • The client host is still up and running and the server is reachable. At this time, the client's TCP response is normal, and the server resets the keep-alive timer.
  • The client host has crashed and is shutting down or being restarted. In the above 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 at 75 second intervals. If the server does not receive any response, it considers the client closed and terminates the connection.
  • The client crashed and has restarted. The server will receive a response to its keepalive probe, which is a reset, causing the server to terminate the connection.
  • The client is functioning normally, but the server is unreachable. This situation is similar to the second state.

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. Long connections are suitable for clients that frequently request resources. In the application scenario of a long connection, the client generally does not actively close the connection. When the connection between the client and the server is not closed, as the number of client connections increases, the server will maintain too many connections. At this time, the server side needs to adopt some strategies, such as closing some connections that have not been requested for a long time, so as to avoid some malicious connections causing damage to the server-side service; if conditions permit, you can limit the maximum number of long connections per client, which can Completely avoid malicious clients from dragging down the overall 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. However, if the client requests frequently, it will waste more time and bandwidth on the establishment and shutdown of TCP.

The generation of long connections and short connections lies in the closing strategy adopted by the client and server. Different application scenarios are suitable for different strategies.

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 clientrequests 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.

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

When to use long connection, 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.

 

The difference between long connection and short connection between http and socket

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

 

HTTP long and short connections

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

 

HTTP persistent connections

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

 

Long connection

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326677136&siteId=291194637