How HTTP persistent connection between browser and server work ?

The Keep-alive is deprecated in Http 1.1 specification, but it is still commonly used by browsers and servers. Clients implementing HTTP/1.0 keep-alive connection can request that a connection be kept open by including the Connection: Keep-Alive request header. If the server is willing to keep the connection open for the next request, it will respond with the same header in the response. If there is no Connection: Keep-Alive header in the response, the client assumes that the server does not support keep-alive and that the server will close the connection when the response message is sent back.

Keep-Alive header must be sent with all messages that want to continue the persistent If the client does not send a Connection: Keep-Alive header the server will close the connection after that request. Same thing if the response does not have Connection: Keep-Alive header browser will assume that server has closed the connection.

The browsers make use of connection close signal even to figure out end of message body. So if you want the connection to remain open, you will have to set correct value for Content-Length, also set correct Content Type header set or the message should be encoded with the chuncked transfer encoding.

The keep-alive behavior can be tuned by following comma-separated options specified in the Keep-Alive general header

  1. The timeout parameter is sent in a Keep-Alive response header. It estimates how long the server is likely to keep the connection alive for.

  2. The max parameter is sent in a Keep-Alive response header. It estimates how many more HTTP transactions the server is likely to keep the connection alive for.

  3. The Keep-Alive header also supports arbitrary unprocessed attributes, primarily for diagnostic and debugging purpose.


The Keep-Alive header is completely option but is permitted only when Connection:Keep-Alive header is present.

Connection: Keep-Alive
Keep-Alive: max=100, timeout=300


These headers in response, will tell browser that server will keep this connection open for next 100 resource request and if you don't make request it will timeout in 300 seconds

No comments: