Long and short connections in the HTTP protocol

1. Concept

  HTTP persistent connection, also known as persistent connection (HTTP persistent connection / HTTP keep-alive / HTTP connection reuse), uses the same TCP connection to send and receive multiple HTTP requests/responses, rather than for each new request/response Open a new TCP connection. Because the communication connection always exists in this mode, this mode is often used for P2P communication.

  HTTP short connection. Each time the browser and the server perform an HTTP operation, a connection will be established, but the connection will be interrupted after the task ends, and the connection will not be maintained like a long connection. This method is often used for point-to-multipoint communication, C/S communication.

2. The operation process of long connection and short connection

  The operation steps of a long connection are:
establish a connection — data transmission — — keep the connection (heartbeat) — — data transmission — — keep the connection (heartbeat)… — — close the connection

  The operation steps of short connection are:
establish connection — data transmission — close connection... establish connection — data transmission — close connection

3. When to use long and short connections

  Long connections are mostly used for frequent operations, point-to-point communications, and the number of connections cannot be too many. The establishment of each TCP connection requires a three-way handshake, and the disconnection of each TCP connection requires four waves of hands.
  If you have to establish a connection for each operation and then operate, the processing speed will be reduced, so after each operation, the data can be sent directly during the next operation without establishing a TCP connection. For example: long connection is used for database connection. Frequent communication with short connections will cause socket errors, and frequent socket creation is also a waste of resources.

  Web site http services generally use short connections, because long connections consume a certain amount of resources for the server. Short connections save resources for thousands or even hundreds of millions of client connections that are so frequent on a web site. Imagine if long connections are used, and there are thousands of users at the same time, and each user occupies a connection, you can imagine the pressure on the server. Therefore, the amount of concurrency is large, but each user needs a short connection without frequent operations.

  In short: the choice of long connection and short connection depends on the demand. The generation of long connections and short connections lies in the closing strategy adopted by the client and server. Specific strategies are adopted for specific application scenarios. There is no perfect choice, only a suitable choice.

Four, advantages and disadvantages

  Advantages of long connection:

  • Due to the reduction of TCP connections opened at the same time, less CPU and memory will be used.
  • Allow HTTP pipelineization of requests and responses (a technology that submits multiple HTTP requests in batches without first waiting for the server's response during the transmission process).
  • Reduce network congestion by reducing the number of packets caused by TCP opening.
  • No handshake is required, which reduces the delay of subsequent requests.
  • There is no need to close the TCP connection to report errors.

There should not be more than 2 connections between the user client and any server and proxy server. The proxy server should use at most 2 * N persistent connections to other servers or proxy servers, where N is the number of concurrently active users. Designed to improve HTTP response time and avoid blocking.

  Short connection advantage:
  http services of web sites generally use short connections. Because the long connection consumes a certain amount of resources for the server. The connection of thousands or even hundreds of millions of clients such as a web site frequently saves some resources by using short connections. Imagine if long connections are used, and there are thousands of users at the same time, and each user occupies a connection, you can imagine the pressure on the server. Therefore, the amount of concurrency is large, but each user needs a short connection without frequent operations. In short, the choice of long connection and short connection should be determined according to demand.

  Disadvantages of long connections:
  For the current widespread broadband connections, Keep-Alive may not be as useful as before. The web server will remain connected for several seconds (depending on the configuration of the web server such as Apache), which may affect performance compared to the increased performance.
  For services where a single file is continuously requested (such as a picture storage website), Keep-Alive may greatly affect performance because it maintains unnecessary connections for a long time after the file is requested.

  Short connection disadvantage:
  frequent communication with short connection will cause socket errors, and frequent socket creation is a waste of resources.

Five, http long connection settings
5.1 HTTP 1.0 settings

  There is no clear support for keepalive in the HTTP1.0 official protocol. If you want to support keepalive in HTTP1.0, you must explicitly add Connection:keep-alive to the header. The process is as follows.

  1. The client initiates a request containing Connection: keep-alive.
  2. After the server receives the request, if the server supports keepalive, it will reply with a response containing Connection:keep-alive without closing the connection; otherwise, it will reply with a response containing Connection:close and close the connection.
  3. If the client receives a response containing Connection: keep-alive, it sends the next request to the same connection until one party actively closes the connection.
5.2 HTTP 1.1 settings

  Because keepalive can reuse connections in many cases, reduce resource consumption, and shorten response time, keepalive is supported by default in HTTP 1.1. If the responder does not support keepalive and needs to clearly identify Connection:close in the response header, then the Connection:keep-alive set by the client will be invalid.

  Set HTTP short connection:
  Set Connection: close in the response message header, then after a request/response, the connection will be closed.

  Set HTTP long connection with expiration time:
  Set Connection: keep-alive and Keep-Alive: timeout=60 in the response header, indicating that after the connection is established, it will become invalid after the idle time exceeds 60 seconds. If the connection is used again in the 58th second of idle time, the connection is still valid. After using it, it will count again and expire after 60 seconds of idle time.

  Set HTTP long connection without expiration time:
  Only Connection: keep-alive is set in the response header, indicating that the connection is permanently valid.

Six, HTTP long connection realization principle and configuration case

  After you understand how to set up a long connection, you can start using it. However, the problem is that when Connection:keep-alive is set in the request header, why does the connection still disconnect after being idle for a period of time? This is because the Connection field is only valid for server-side settings.
  HTTP operations are request/response pairs, that is, the client sends the request first, and then the server processes the request. Therefore, the end operation of an HTTP request is on the server, and the shutdown is also initiated by the server.

  Here are a few configuration cases.

6.1 The client connection failure time is greater than the server failure time

  The client sets Connection: Keep-Alive and Keep-Alive: timeout=60.
  The server sets Connection: Keep-Alive and Keep-Alive: timeout=5.
  When the validity period set by the client is greater than the validity period set by the server, the server setting takes effect at this time, and the actual effective time of the connection is determined according to the response setting.

6.2 Client Settings Connection: Close

  As above, when the client sets a short connection, the server returns Connection: Close, and the client closes the connection after receiving the response.
  In addition, whether the request or response header contains a Connection with a value of close, it indicates that the currently in use tcp connection will be disconnected after the request is processed. Later, when the client makes a new request, it must create a new tcp connection. The close setting of HTTP Connection allows either the client or the server to close the underlying connection, and both parties will ask to close their TCP connection after processing the request.

6.3 The client sets the expiration time, and the server setting does not expire

  The client sets Connection: keep-alive and Keep-Alive: timeout=30.
  The server sets a permanent connection, namely Connection: keep-alive.
  The connection will always be maintained.

Reference materials:

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/113752381