HTTP persistent connection
HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair. The newer HTTP/2 protocol uses the same idea and takes it further to allow multiple concurrent requests/responses to be multiplexed over a single connection.
Operation
HTTP 1.0
Under HTTP 1.0, connections should always be closed by the server after sending the response.Since at least late 1995, developers of popular products using HTTP/1.0, started to add an unofficial extension named "keep-alive" in order to allow the reuse of a connection for multiple requests/responses.
If the client supports keep-alive, it adds an additional header to the request:
Connection: keep-alive
When the server receives this request and generates a response, if it supports keep-alive then it also adds the same above header to the response. Following this, the connection is not dropped, but is instead kept open. When the client sends another request, it uses the same connection.
This will continue until either the client or the server decides that the conversation is over and in this case they omit the
"Connection:" header from the last message sent or, better, they add the keyword "close" to it:
Connection: close
After that the connection is closed following specified rules.
Since 1997, the various versions of HTTP/1.1 specifications acknowledged the usage of this unofficial extension and included a few caveats regarding the interoperability between HTTP/1.0 and HTTP/1.1 clients / servers.
HTTP 1.1
In HTTP 1.1, all connections are considered persistent unless declared otherwise. The HTTP persistent connections do not use separate keepalive messages, they just allow multiple requests to use a single connection. However, the default connection timeout of Apache httpd 1.3 and 2.0 is as little as 15 seconds and just 5 seconds for Apache httpd 2.2 and above. The advantage of a short timeout is the ability to deliver multiple components of a web page quickly while not consuming resources to run multiple server processes or threads for too long.Keepalive with [chunked transfer encoding]
Keepalive makes it difficult for the client to determine where one response ends and the next response begins, particularly during pipelined HTTP operation. This is a serious problem whenContent-Length cannot be used due to streaming. To solve this problem, HTTP 1.1 introduced a chunked transfer coding that defines a last-chunk bit. The last-chunk bit is set at the end of each response so that the client knows where the next response begins.Advantages
- Reduced latency in subsequent requests.
- Reduced CPU usage and round-trips because of fewer new connections and TLS handshakes.
- Enables HTTP pipelining of requests and responses.
- Reduced network congestion.
- Errors can be reported without the penalty of closing the TCP connection.
Disadvantages
If the client does not close the connection when all of the data it needs has been received, the resources needed to keep the connection open on the server will be unavailable for other clients. This affects both server's availability and the availability of resources on the server, with the degree of impact depending on the server's architecture and configuration.Also a race condition can occur where the client sends a request to the server at the same time that the server closes the TCP connection. A server should send a 408 Request Timeout status code to the client immediately before closing the connection. When a client receives the 408 status code, after having sent the request, it may open a new connection to the server and re-send the request. Not all clients will re-send the request, and many that do will only do so if the request has an idempotent HTTP method.
Use in web browsers
All modern web browsers including Chrome, Edge, Firefox, Opera, and Safari use persistent connections.In Firefox, the number of simultaneous connections can be customized. Persistent connections time out after 115 seconds of inactivity which is changeable via the configuration.
Implementation
Python'srequests library contains requests.Session, which establishes a persistent HTTP connection, thereby allowing the underlying TCP connection to be reused, which can result in a significant performance increase.