WEB clients often open connections to the same site. For example, most inline images on a Web page are usually from the same Web site, and a significant portion of hyperlinks to other objects usually point to the same site. Therefore, an application that initializes an HTTP request to a server is likely to make more requests to that server in the near future. This property is referred to as site locality locality.
Therefore, http/1.1 (and the various enhanced versions of http/1.0) allow the HTTP device to keep the TCP connection open after the transaction has ended in order to reuse the current connection for future HTTP requests. A TCP connection that remains open after the transaction has ended is called a persistent connection. Non-persistent connections are closed after the end of each transaction. Persistent connections remain open between different transactions until the client or server decides to close them.
Reusing an idle persistent connection that has been opened on the target server avoids the slow connection establishment phase.
There are two types of persistent connections: older http/1.0+ "keep-alive" connections, and http/1.1 "persistent" connections.
http/1.0+ keep-alive Connection keep-alive operation
A client that implements a http/1.0 keep-alive connection can keep a connection open by including a connection:keep-alive header request.
If the server is willing to keep the connection open for the next request, it contains the same header in the response. If there is no connection:keep-alive header in the response, the client considers that the server does not support keep-alive and closes the connection after the response message is sent back.
Keep-alive Options
The keep-alive header simply requests that the connection remain active. After a keep-alive request is made, the client and server do not necessarily agree to a keep-alive session. They can then close idle keep-alive connections at any time, and can arbitrarily limit the number of transactions processed by the keep-alive connection.
You can adjust the behavior of keep-alive with the comma-delimited option specified in the keep-alive generic header.
The parameter timeout is sent in the keep-alive response header. It estimates how long the server wants the connection to remain active. This is not a commitment value.
The parameter max is sent in the keep-alive response header. It estimates how many transactions the server also wants to maintain the active state of this connection. This is not a commitment value.
The keep-alive header also supports arbitrary unprocessed properties, which are primarily used for diagnostics and debugging. The syntax is name [=value].
The keep-alive header is completely optional, but it can only be used when the connection:keep-alive is provided. Here's an example of the keep-alive response header, which shows that the server will also keep the connection open for a maximum of 5 other transactions, or leave the open state to 2 minutes after the connection has been idle.
Connection:keep-alivekeep-alive:max=5, timeout=120
http/1.1 Persistent Connection
Unlike the http/1.0+ keep-alive connection, the http/1.1 persistent connection is activated by default. Unless otherwise specified, http/1.1 assumes that all connections are persistent. To close the connection after the end of the transaction, the http/1.1 application must add a Connection:close header to the message to display. This is a significant difference from the previous version of the HTTP protocol, in which the keep-alive connection is either optional or unsupported.
The http/1.1 client assumes that the http/1.1 connection remains open until the response is received, unless the Connection:close header is included in the response. However, the client and server are still free to close idle connections. Not sending Connection:close does not mean that the server will always keep the connection open.
HTTP-Persistent connection