Apache optimization: KeepAlive On or Off?


Apache is the most widely used web server on the Internet. Knowing how to get the most out of Apache is very important for a systems administrator. Optimizing Apache is always a balancing act. It’s a case of sacrificing one resource in order to obtain savings in another.

What is KeepAlive?

HTTP is a session less protocol. A connection is made to transfer a single file and closed once the transfer is complete. This keeps things simple but it’s not very efficient.

To improve efficiency something called KeepAlive was introduced. With KeepAlive the web browser and the web server agree to reuse the same connection to transfer multiple files.

Advantages of KeepAlive

  • Improves website speed: It reduces latency associated with HTTP transfers and delivers a better user experience.
  • Reduces CPU usage: On the server side enabling KeepAlive reduces CPU usage. Consider that a typical web page has dozens of different files such as images, stylesheets, javascript files etc. If KeepAlive is disabled a separate connection must be made for each of those files. Creating and closing connections has an overhead and doing it for every single file wastes CPU time.

Disadvantages of Keepalive

  • Increases memory usage: Enabling KeepAlive¬† increases memory usage on the server. Apache processes have to keep connections open waiting for new requests from established connections. While they are waiting they are occupying RAM that could be used to service other clients. If you turn off KeepAlive fewer apache processes will remain active. This will lower memory usage and allow Apache to serve more users.

When should you enable KeepAlive?

Deciding whether to enable KeepAlive or not depends on a number of different factors:

  • Server resources: How much RAM vs. how much CPU power you have? RAM is often the biggest limiting factor in a webserver. If you have little RAM you should turn off KeepAlive because having Apache processes hanging around while they wait for more requests from persistent connections is a waste of precious memory. If CPU power is limited then you want KeepAlive on because it reduces CPU load.
  • Types of sites: If you have pages with a lot of images or other files linked into them, KeepAlive will improve the user experience significantly. This is because a single connection will be used to transfer multiple files.
  • Traffic patterns: The type of traffic you get. If your web traffic is spread out evenly throughout a day then you should turn on KeepAlive. OTOH, if you have bursty traffic where a lot of concurrent users access your sites during a short time period KeepAlive will cause your RAM usage to skyrocket so you should turn it off.

Configure Apache KeepAlive settings

Open up apache’s configuration file and look for the following settings. On CentOS this file is called httpd.conf and is located in /etc/httpd/conf. The following settings are noteworthy:

  • KeepAlive: Switches KeepAlive on or off. Put in “KeepAlive on” to turn it on and “KeepAlive off” to turn it off.
  • MaxKeepAliveRequests: The maximum number of requests a single persistent connection will service. A number between 50 and 75 would be plenty.
  • KeepAliveTimeout: How long should the server wait for new requests from connected clients. The default is 15 seconds which is way too high. Set it to between 1 and 5 seconds to avoid having processes wasting RAM while waiting for requests.
Other settings

KeepAlive affects other settings in your Apache configuration file even though they are not directly related. Here are the settings in an Apache prefork MPM webserver:

  • MaxClients: MaxClients is the maximum number of child processes launched by Apache to service incoming requests. With KeepAlive enabled you have will have a higher number of child processes active during peak times. So your MaxClients value may have to be increased.
  • MaxRequestsPerChild: The number of requests a child process will serve before it is killed and recreated. This is done to prevent memory leaks. When KeepAlive is turned on each persistent connection will count as one request. That effectively turns MaxRequestsPerChild into a maximum connections per child value. As a result you can set a lower MaxRequestsPerChild value if you allow KeepAlive. If you don’t allow KeepAlive you should increase the MaxRequestsPerChild value to prevent excessive CPU usage.


There is no one universal solution to tuning Apache. It all depends on the resources at your disposal and the type of sites you have. When used properly KeepAlive can improve the user experience at minimal cost in terms of server resources. But it can also be a liability when you are faced with a large number of concurrent users.

62 thoughts on “Apache optimization: KeepAlive On or Off?

  1. Nice article. Its rare to find a post that explains the use of KeepAlive from a practical yet conversational perspective. Most sites just regurgitate whats already said on the apache site. Thanks.

    • Depends on where you installed apache. Probably somewhere in the program files folder. An easy way to find it is to go via the shortcut. use the start menu -> programs -> apache. There even be an option to edit the config file there. If not you can right click on the short cut icon and then choose find target. It’s likely in the conf sub folder of the apache folder.

      • hi
        I have one dought. Hope you can solve my issue.

        I am using apace http 2.2
        I configure 5more virtual hosts in apache.
        but particular 2apps want increase their keep alive timeout.
        but I don’t have any idea where I configure that.
        my question was if I configure it will take changes on only 2apps only. remaining same was normal. I increase only that particular 2apps only.

        can you please explain


        • Sorry for the late reply. I bet you’ve probably figured this out yourself by now. But for others who might be interested in this I have this bit from the docs to share:

          In a name-based virtual host context, the value of the first defined virtual host (the default host) in a set of NameVirtualHost will be used. The other values will be ignored.


          I think what this means is that you need to set the domains of the apps to point to a different IP addresses, add 2 different namevirtualhost statements and then you can specify different keep alive timeouts in the default virtualhost blocks of those different IPs.

  2. Like this article. I have one question though:

    If you have little RAM you should turn off KeepAlive

    How much is little?

    • I guess that depends on your traffic. Are you regularly hitting the limits of memory on your server? If yes then try turning off keep alive. If not then keep it on. You can also try tuning it by limiting the keepalivetimeout to 1-5 seconds and reducing the maxkeepaliverequests to something like 75.

  3. If i have 16GB of RAM, is that enough to turn on Keep-Alive with a lot of active users?? how many is a lot of users with this size of RAM?

    • Yeah. It doesn’t just depend on the amount of RAM you have but also the amount of traffic you have and whether that is causing you to run out of RAM. For instance this site runs on a VPS with just 256MB of RAM and I have keepalive on. Just don’t get a lot of bursty traffic so I never run out of RAM.

  4. By the way, what is First-Byte time and how to configure for best optimizations for my site??

    How do i configure server/DB to compress texts?

    In this link:

    What is Combine 0% and Cookies 8%……how do i configure the server/DB to pass all those tests (Combine, Cookies, Keep-Alive, etc.)???

    • You shouldn’t put too much stock in tests like the one you’ve linked to. Nevertheless since you asked, here are the answers to your questions:

      1. Compressing files can be done with mod_deflate. Look up mod_deflate and how to configure it. Just keep in mind that compressing web pages increases CPU load and IMO it’s better to not compress pages.

      2. Combining multiple CSS and javascript files into one large file requires support from your Content Management System. You will need to ask your web developer for help with this.

  5. I turned on a KeepAlive directive and set KeepAliveTimeout to 600 seconds (sometimes it takes a long time to my module to prepare a response to client).
    Although these configurations I see that Apache server doesn’t sends TCP keepalive messages and the connection is lost.
    So when my module is ready to send response back to client a TCP connection is already down.
    Do yo have any ideas what can it be?

  6. Hello, thanks for posting this, can you tell me what setting to use for a dedi server with 8gb ram on a video sharing website where users online can get to 100 at one time? thanks

  7. That is well written article. Nice balance of explanation on a technical topic. It was very helpful and the changes were made on my server. Speed improvements are very evident.

  8. Hi,

    How does one ‘Enable’ Keep Alive? Do I have to paste some script on .htaccess file?

    May be elementary question for you. Nonetheless, I will appreciate your guidance.

    Best wishes,
    Ashok Koparday

  9. Que se puede hacer si un servidor de hosting compartido no soporte KeepAlive ? Realizar una configuración en el .htaccess del tipo KeepAlive On produciría un error web. Alguna idea?

  10. This very helpful, 1 question I am running on SLES 2 GB memory, traffic quit high with dynamic http (Java Servlet with DB2), what is the recommended value for Keep Alive ? Thank you.

    • Depends on how much free RAM you have. If you are currently well within your memory limits then enable keepalive using the values specified in the article above. But if you are running out of RAM then try turning keepalive off.

  11. I am on a shared hosting service of hostgator? How can i do this for my website [removed] so that it would load in less time. Waiting for your response

  12. I learned a lot from your article, thanks!

    But not sure what my optimal setting should be. I have iphone app users who send requests to my server, which runs code and returns a small text file, just a few KB back to the app. So it is a single very small file, but over slow cell networks. Since it is a single file, should I turn keep alive off? Or, reduce the time? If I reduced it to 5 seconds, but the cell phone was slow, would I be in danger of losing their connection? Thank you.

    • Also, sometimes it might take 10 seconds for the server to finish calculations, and send the packet back to the iphone user. Is this relevant as far as keep alive goes? I assume it begins counting the instant the server receives the request? And if the server itself takes a long time like 15 seconds (more than the keepalive allotment of say 5 seconds), the connection will still be alive?

      • No, a new connection will be made. If this application is all your server runs then set keep alive to higher than 10 seconds. You’ll have to experiment and try different configurations to see what works best for you.

    • That is an interesting question. You would have to conduct benchmarks to see what works best in that scenario.

      If there is a load balancer in front of apache then apache is only serving requests from that one client. So there is no point in creating and tearing down TCP connections. Better to have one long running connection. That seems to be the idea behind cloudflares railgun technology:


      So see if setting keepalivetimeout to a very high value helps.

  13. 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!

      • Thank you for the good article,
        Still need more clarification regarding the “KeepAliveTimeout”;

        For an example:
        if KeepAliveTimeout is set to 10 seconds;
        1. Is the 10 seconds represents the maximum length of the connection; so if a connection (request then response) needs 15 seconds so this connection will timeout and will be interrupted/trminated and not completed? and in that case we can conclude that the 10 seconds is counted from the beginning of the connection;


        2. Is the 10 seconds represents the maximum number of seconds that Apache child is allowed to be idle (doing nothing) waiting for the user/client to request another file/content in the current (same) connection? and in that case we can conclude that this 10 seconds are counted and calculated starting from the point in time of successfully responding to/finishing the lat request received from the user/client?

Leave a Reply

Your email address will not be published. Required fields are marked *