Keep-Alive is a header that maintains a persistent connection between client and server, preventing a connection from breaking intermittently. Also known as HTTP keep-alive, it can be defined as a method to allow the same TCP connection for HTTP communication instead of opening a new connection for each new request.

Graphic for keep-alive by MaxCDN.


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.

  1. 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.

  2. 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
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 HTTP 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


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.

  • Sally Mills

    Thanks, was a useful read. Just wondering if there are any negatives to doing this and why it isn’t automatically configured if it is so good?

    • Ivan Dabic

      Hey Sally,

      Actually, more and more platforms are having this enabled by default but, as for disadvantages, there is one. Given that this TCP use case (functionally) relies fully on Server configuration and (control wise) on client itself it may happen that client does not close the connection for any reason and then server resources (cpu/mem) allocated stay unusable for other clients until Server itself closes this connection. With this in mind, all logic that handles inactive connections is on Server side.

  • Blowsie

    In your example, `max` is set to 20, only allowing 20 requests, was this deliberate?

    • Robert Gibb

      This value was chosen for the purpose of the example. It’s not referring to any specific real world scenario.

  • Julián Acosta

    What happens when user closes browser when the connection is active? Will the connection close?

    • Robert Gibb

      Yes. If the process assigned to the browser by the operating system exits, the keep-alive connection will terminate.

  • Qoil Farul

    when I adding keep-alive into .htaccess, my web is can’t opened and showing error massage “500 internal server error”. Can you help me? thank’s

  • Pablo Alfaro

    Hello, thanks for a very nice article, it’s very informative.
    Does anybody know if the KeepAliveTimeout is since the last request or since the start of the connection?

    I mean does each new request reset the timer or does the connection last a fixed amount of time?

    Thanks in advance!