Scott Wiersdorf
Wed Nov 19 01:01:14 MST 2003
$Id: tuning.html,v 1.4 2004/04/08 06:24:10 scott Exp $


If you're serving dynamic content, you'll want to exercise your site to determine optimal settings:
  1. restart_apache
  2. run 'top' in a terminal window
  3. hit your site hard with ab or some other web server exerciser for 5 minutes or so
  4. watch the size of the Apache children in 'top' during the exercise
  5. if the size of the children does not grow, or grows a little but then stops growing, you can safely assume that there are no memory leaks and you can set your MaxRequestsPerChild to zero
  6. if the size of the children continues to grow over the duration of the exercise, you are using a component (frontpage, some ImageMagick stuff, PHP, etc.) that has a memory leak. Start your test again (beginning with restart_apache) and run the exerciser with fewer total hits. Lather, rinse, repeat. Do this until you find a total number of hits that is acceptable (i.e., the children are not yet too bloated) and that is not too small (otherwise you'll be killing off the children and the cost of forking will become the dominant performance factor). This is the 5000-10000 range, usually, but depends completely on how bad your memory leak is.


KeepAlives, from the manual, "provide long-lived HTTP sessions which allow multiple requests to be sent over the same TCP connection. In some cases this has been shown to result in an almost 50% speedup in latency times for HTML documents with many images".

If the typical HTTP "hit profile" on your site does not involve multiple HTTP requests (see example below), you won't benefit from KeepAlives because once the request has been served (and no more are expected from this client for serveral seconds), the socket remains open, unused and consuming server resources that could otherwise be used for other connections. This is what is happening while your site is being very slow: the server is waiting for all those KeepAlive connections to "time out" so it can server other clients.

Example: Your typical request is for a web page and several dozen images. It usually takes your client (i.e., a 56k modem user) about 5 seconds to request the page and all the images. You want to turn KeepAlive to "on" and set the timeout to 5 seconds.

If your average client is a broadband user OR for whatever reason you can see that the average total request per page (including images and other dependencies) is only 2 seconds, then you should reduce your timeout to 2 seconds.

KeepAliveTimeout - How long is an average session? By setting your timeout high, clients benefit from a quicker response from your server and your server uses fewer resources to service it. By setting this too high, Apache children are tied up waiting for clients when they could be servicing new clients.

MaxKeepAliveRequests - How many requests maximum through a keepalive connection? If you have a site with fewer visitors at any given moment than you have available Apache children your clients can benefit from a high MaxKeepAliveRequests setting. Beware of greedy robots! They can suck your server dry in a single session if this is set too high.

You can calculate your optimal setting by determining how many requests are made per session (a "session" is an average length of stay for your visitors). Note that this only works when the number of clients you service at any moment is fewer than the available Apache children. If you have more clients than available children, KeepAlive settings will hurt overall performance and should be turned off.

nokeepalive - Here are some broken clients known to abuse the KeepAlive feature. You'll need to enable mod_setenvif, if it's not already enabled or compiled in your Apache:

    BrowserMatch "Mozilla/2"       nokeepalive
    BrowserMatch "MSIE 4\.0b2;"    nokeepalive downgrade-1.0 force-response-1.0
    BrowserMatch "RealPlayer 4\.0" force-response-1.0
    BrowserMatch "Java/1\.0"       force-response-1.0
    BrowserMatch "JDK/1\.0"        force-response-1.0
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
See the official Apache performance tuning documentation at:

Apache tutorials

Send comments to the author