What is Keep-Alive?


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

  1. A client has to create a new connection to interact and receive a file from a server
  2. The client requests an HTML file using a new connection and this connection terminates after receiving the file
  3. The browser interprets the HTML file and checks whether any other files are required to display the complete web page
  4. After a thorough analysis, it creates a new connection to request each of those files

A single HTML page may contain several resources that make up the entire web page. This includes html code, CSS files, JavaScript files, images and multimedia files. For instance, if the HTML has 4 stylesheets, 2 Javascript files and 3 image files, this indicates an additional 9 requests to get these files. But this mechanism is very inefficient, especially with complex web pages that have a large number of elements.

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

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

  1. KeepAlive
    Use “KeepAlive On” to enable it.
    To disable, just use “KeepAlive Off”.
  2. MaxKeepAliveRequests
    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.
  3. KeepAliveTimeout
    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:

Message Trace:
~$ curl -I https://www.domain.com/file.html
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
Date: Thu, 15 Jan 2015 16:45:29 GMT
Content-Length: 1845
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

  1. 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.
  2. Web page speed: The ability to serve multiple files using the same connection can reduce latency and allow web pages to load faster.
  3. 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.

Request Demo
Have questions? Chat now with the live chat button on this page or request a demo from our edge experts.
Request a Demo
Get an Advantage
Stay informed of the latest edge news, updates and solutions.