Apache Directive - KeepAlive and KeepAliveTimeout

Posted on 2003-03-28
Medium Priority
Last Modified: 2008-02-26
I run a very busy web server, my load averages are high and I'm looking at ways to reduce the load. Reducing the load is my primary aim, I don't care about anything else.

I have several questions about Apache's KeepAlive and KeepAliveTimeout directive. I understand what they do but I'm not clear on the following:

1. To reduce load on a very busy web server, would it be prudent to disable KeepAlive? Please explain why.

2. My MaxClients is set to 200, if KeepAlive was enabled then what KeepAlive setting would be reasonable (e.g. 1-2 sec or much longer?) to reduce load on a very busy web server?

2. My MaxClients is set to 200, if KeepAlive was enabled then what KeepAliveTimeout setting would be reasonable (e.g. 20-30 or much higher?) to reduce load on a very busy web server?

Apache's manual is vague and other websites have no information for tuning Apache's KeepAlive directive for very busy web servers. Please help.

Thank you.
Question by:topwiz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 15

Expert Comment

ID: 8226649
Hi topwiz,

It's pretty much hard to kinda say that, for busy server, you would need to have this settting.

Depending on your version of Apache (againg apache website);
1.3.x : http://httpd.apache.org/docs-2.0/misc/perf-tuning.html

Overall, it was about the machine where Apache is running.  OS might need to be tuned for optimum - look at max file descriptors, any other application is running - DNS, Sendmail, VirusScanner, Firewall, etc.
You might need to tweak some TCP/IP stack configuraion (beyond my knowledge really) - 10 or 100mbps, full/half duplex, etc.

On apache itself, in addition to KeepAlive/KeepAliveTimeout, loading of modules, directives, would make a difference.  General tips, try to use directive only if you need them.  Load modules only if you need them.  Someone told me, mod_rewrite, would tax some server performance. etc.

On apache itself, you could you mod_status (http://httpd.apache.org/docs-2.0/mod/mod_status.html) to monitor your server activity realtime.

I just couldn't find a better way than mentioning this again (even if you had visited it a number of times).
This directive is taking "on"/"off" as parameter.  In simple term - whether you want to use the feature or not.  For example, once you visit a site (www.someting.com), there would be a number of connection from your machine to the remote machine (on port 80).  Once the browse finished fetcing pages, the socket will be closed (if KeepAlive off).  If you click on a link on that page, another connection will be initiated.  Remember that opening/closing socket will require some overhead from OS, and Apache itself (same thing with closing the sockets).

Good thing here is, you might not need to have a big number of StartServers/MinSparesServer/MaxSpareServer, which is good (since each of the Apache child process would be eating up RAM too), but, remember the overhead to open and close the sockets.


KeepAliveTimeout will determin howlog a persistent connection will be kept open.  Having a big figure would cause existing connection remains open, for subsuquent erquest.  REmember that apache would fork a child process to server a request.  If a user (clint) visit a page with 3 graphics -- there would be 4 apache child process to serve this request.  If the KeepAliveTime is kept big, new request would cause a few more child to be spawned (started) -- remember the overhead!

However, if we keep the KeepAliveTimeout to a low value - it will cause the existing connection to be closed fast -- good so that apache could fork a new chile to serve incoming request.  However, if the request is coming from the same remote client, it will not be very good, since we might as weel keep the recent connection open (high keepalivetimeout), rather that starting to initiate another connection -- remember the overhead again.

Overall, you would need to experiment the figures a bit.

Note: Setting KeepAliveTimeout to a high value may cause performance problems in heavily loaded servers. The higher the timeout, the more server processes will be kept occupied waiting on connections with idle clients.


Author Comment

ID: 8226757

Thanks for your reply. I'm running Apache version 1.3.x. I've read all of Apache's manuals and I've optimized everything by the book. I'm well aware of the other factors that you've mentioned.

The bottom line: my specific questions about KeepAlive and KeepAliveTimeout is still NOT answered.
LVL 51

Accepted Solution

ahoffmann earned 1000 total points
ID: 8230899
if you really have a heavy loaded server **and** your hardware is not good enough for this, then you should probably switch KeepAlive off
AFAIK clients will not see a big disadvaantage 'cause the server is already slow (as you said). But with this setting you get a better performing server.
Does this answer your question?
Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.


Author Comment

ID: 8230927

Basically that does answer my question. However, do you have any facts (e.g. reputable websites) to backup your comment?
LVL 51

Expert Comment

ID: 8230958
no reputations, try&error as samri already suggested
AFAIK, most docs will tell you to upgrade your hardware ('cause it might be cheaper and simpler than tweaking software)
LVL 15

Expert Comment

ID: 8233901

what is your machine spec anyway?  hardware (cpu/RAM), OS.

From my experience, the apps, will depend too much on hw/OS performance.

bandwith will be another issue to look at.

if you happens to be running try monitoring you machine using "top".  It would give you some indication of what is happening.  Remember that, is most cases, RAM would play a big role, since when your machine starts to run out of physical RAM, it will start to swap to disk (paging).  When this happens, IO will be the bottlneck -- and will start to cause CPU utilization to jump -- it's all like a chain reaction.

I personally do not have much expertise in performance fine-tuning, but those items would be some basic that you might want to look at.

hope this would give some idea.


Expert Comment

ID: 8251917
1) Make sure that HostNameLookups is set to 'Off' in your httpd.conf. The IPs of your visitors will not be resolved, so each httpd process will process the requests faster. This will result in fewer httpd processes running concurrently and probably a big improvement in performante (if HostNameLookups was set to 'On' previously).

2) Strip your binaries. Go to your Apache bin/ directory and do an 'strip *' there. Strip the loadable modules too (in Apache's libexec/ directory). This will remove unused symbols from the binaries and make them smaller, resulting in a speed improvement when httpd processes are forking.

3) Make sure that you're not running low on memory. As soon as your system runs out of RAM and starts using swap, it will start crawling because disk access is sooo much slower than RAM. If this is your case, just adding more memory will make your system much faster, without changing anything else. If you're on Linux, run 'free' to see how much memory/swap you're using.

4) Remove all the modules that you're not using. Many distributions come by default with lots of modules compiled in the httpd binary. Compile from scratch adding only the modules you need. Or if you're not sure about what modules you're using, compile only with module 'so' (--enable-module=so) and the rest of modules as dynamic (--enable-shared=<modulename>). If you do this, Apache will load only the modules that are used (on a on-demand basis), so memory usage will improve too.

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question