What are HTTP persistent links?

Speaking of HTTP, the impression in everyone’s mind is to establish a TCP link, initiate a request, the server to complete the response to disconnect the link, if there are other requests to establish a TCP link again, the so-called short link. We know that each TCP connection requires three handshakes, which will cost a lot. Therefore, HTTP performance will be poor. Is there any room for optimization? Of course, there is the persistent connection of HTTP that we will talk about today. After the connection is established between the server and the client, it will not be immediately disconnected. On this connection, the client can request multiple times.

Persistent links to HTTP1.0

Persistent links have been supported since HTTP1.0. When a client requests a persistent link, it simply declares the following in the header

Connection: keep-alive
Copy the code

Client support is not enough, only the server is also the need to support, when at the same time satisfy the conditions, will keep persistent link, although the persistent link in 1.0 times has come true, but it is not without faults, we are in the same TCP link, make multiple requests, the request will be handled in accordance with the time sequence, When the first request is not completed, it does not send a second request. This reduces the overhead of the server, but it is too slow for the client. If the first request times out, it will surely affect the subsequent request.

Http1.1 optimization for persistent links

Http1.1 has better support for persistent links

  • Persistent links are enabled by default
  • Pipeline Technology (HTTP Pipelining)

Because in HTTP1.0 persistent links, the next request must wait for the previous request to complete before it can be executed, pipeline technology was introduced in HTTP1.1, which can send multiple requests at once without waiting for the previous request, but the response order is the same as when the request was made.

Http1.0 has no pipes

SequenceDiagram Client ->> Server: request1 server ->> Client: respone1 Client ->> Server: Request2 server ->> Client: respone2

HTTP Pipelining (Pipelining)

SequenceDiagram Client ->> Server: request1 client ->> Server: request2 server ->> Client: respone1 server ->> Client: Respone2 server ->> client: respone3

While pipelining improves performance by not waiting for a response, it still does not solve the problem of dropped head blocking.

http2

Multiplexing in order to achieve Multiplexing, http2 introduced the concept of binary data frame and stream, it will encapsulate data into frames, each frame has sequence identification, send data in parallel, get the data will be merged into the original data according to the identification, In this way, parallel transmission is realized and the problem of wire head blocking is solved.

Http2 Multiplexing

SequenceDiagram Client ->> Server: Request1 client ->> Server: Request2 client ->> Server: Request3 client ->> Server: Request4 server ->> Client: Respone4 server ->> client: respone1 server ->> client: respone3 server ->> client: respone2

Keepalive configuration

Http1.1 has enabled persistent links by default, but it does not necessarily play a role in the use of the client and server, in the case of the client and server are enabled persistent links, if useful to the web server such as nginx, or to configure, otherwise there is no effect. The default configuration of nginx.conf is as follows

keepalive_timeout  65;
Copy the code

If this function is disabled, keepalive_timeout can be set to 0

Connection: keep-alive indicates that the server has enabled persistent links. If the client wants to disable persistent links, set the header as follows in the last request

Connection: close
Copy the code

conclusion

  1. Keep-alive is enabled by default in HTTP 1.0
  2. Keep-alive is enabled by default in HTTP 1.1. Persistent links are closed only when “Connection: close” is added
  3. Http2 implements multiplexing and eliminates header blocking

Enable persistent link benefits

  • The server reduces the overhead of TCP creation
  • The client saves time by not creating TCP frequently

Disadvantages of enabling persistent links

  • TCP cannot be released in a timely manner, wasting server resources
  • Http1.0 and 1.1 introduce header blocking issues