The default HTTP connection is usually closed after each request has been completed, meaning that the server closes the TCP connection after delivering the response. In order to keep the connection open for multiple requests, the keep-alive connection header can be used.
How HTTP Works Without Keep-Alive
- A client has to create a new connection to interact and receive a file from a server
- The client requests an HTML file using a new connection and this connection terminates after receiving the file
- The browser interprets the HTML file and checks whether any other files are required to display the complete web page
- After a thorough analysis, it creates a new connection to request each of those files
The Need for Keep-Alive
Creating multiple connections may reduce the loading time. It also utilizes many resources on the server. We can eventually overcome this issue and transfer all those files through a single connection by enabling the Keep-Alive, which avoids the need to repeatedly open and close a new connection. If it is not enabled, the process could take considerably longer to display the web page.
How to Enable Keep-Alive
Keep-Alive (also known as HTTP Keep-Alive or persistent connection) is a bit of communication (message) between the client and server that says: “You may grab many files.” These are extremely small messages that consume very little bandwidth. The following method assumes Apache web server.
Keep-Alive is enabled by explicitly requesting it via HTTP header. If you don’t have access to your web server configuration file, you can add HTTP headers yourself by using .htaccess file.
The .htaccess file is a configuration control file that will take effect when placed in any directory, which is in turn loaded via the Apache server. You may create .htaccess file using any text editor such as a TextPad or gedit (Linux). It’s a method to reconfigure your website without altering the server configuration file. This is done by overriding it’s original configuration without having to restart the web server when changes are made.
Here is the code:
<IfModule mod_headers.c> Header set Connection keep-alive </IfModule>
Adding this code to your .htaccess file will include Keep-Alive headers to your requests, and doing so will override any higher settings. (Note: Test your website for errors after editing .htaccess file.)
If you are able to access your Apache configuration file (httpd.conf), you can turn on keep-alive there. Keep-Alive must be enabled automatically with every fresh Apache server installation. If it isn't enabled (for some unexpected reason), there is a way to enable Keep-Alive by just editing few settings in the Apache configuration file as described below.
3 Properties that Affect Keep-Alive Functionality
Use “KeepAlive On” to enable it.
To disable, just use “KeepAlive Off”.
It sets the maximum number of requests for every Keep-Alive connection.
A value of 100 is normally good enough for almost any scenario. This value, however, should be increased depending on the amount of files within a web page that the server is supposed to deliver.
This setting prevents unused connections from hanging around for too long. It sets how long your server should wait for new requests from clients.
A value between 7 to 10 seconds is usually ideal. With higher traffic this value can go extremely higher to make sure there is no frequent TCP connection re-initiated. If this value goes down too much, Keep-Alive loses it’s purpose!
Response Header Example
Here’s an example of a Keep-Alive response header:
~$ curl -I https://www.domain.com/file.html
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Thu, 15 Jan 2015 16:45:29 GMT
Keep-Alive: timeout=10, max=20
Server: Apache/2.4.9 (Unix) PHP/5.6.2
The server determines to maintain a persistent connection for 20 more transactions or until it has been idle for 10 seconds.
Benefits of Keep-Alive
- Reduced CPU Usage: Creating new TCP connections can take a lot of resources such as CPU and memory usage. Keeping connections alive longer can reduce this usage.
- Web page speed: The ability to serve multiple files using the same connection can reduce latency and allow web pages to load faster.
- HTTPS: HTTPS connections (content over Secure Socket Layer) are very resource intensive. So it is highly recommended to enable Keep-Alive for HTTPS connections and maybe spice it a bit with HTTP/2.
All modern browsers use persistent connections as long as the server is willing to cooperate. Check your application and proxy server configurations to make sure that they support Keep-Alive. You need to pay close attention to the default behavior of HTTP libraries as well.
You may also use HTTP/1.1, where Keep-Alive is implemented differently and the connections are kept open by default. Unlike HTTP/1.0 keep-alive connections, HTTP/1.1 connections are always active. HTTP/1.1 assumes all connections are persistent unless the response contains a "Connection: close" header. But not sending "Connection: close" does not mean that the server maintains the connection forever and it's still possible for the connection to be dropped.