Recipe 11.10 Tuning Thread Creation


You're using Apache 2.0 with one of the threaded MPMs, and you want to optimize the settings for the number of threads.


Will vary from server to server.


The various threaded MPMs on Apache 2.0 handle thread creation somewhat differently. In Apache 1.3, the Windows and Netware versions are threaded, while the Unixish version is not. Tuning the thread creation values will vary from one of these versions to another.

Setting the number of threads on single-child MPMs

On MPMs that run Apache with a single threaded child process, such as the Windows MPM (mpm_winnt), and the Windows and Netware versions of Apache 1.3, there are a fixed number of threads in the child process. This number is controlled by the ThreadsPerChild directive and must be large enough to handle the peak traffic of the site on any given day. There really is no performance tuning that can be done here, as this number is fixed throughout the lifetime of the Apache process.

Number of threads when using the worker MPM

The worker MPM has a fixed number of threads per child process but has a variable number of child processes, so that increased server load can be absorbed. A typical configuration might look like the following:

StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 ServerLimit 16

The MinSpareThreads and MaxSpareThreads directives control the size of the idle pool of threads, so that incoming clients will always have an idle thread waiting to serve their request. The ThreadsPerChild directive indicates how many threads are in each child process, so when the number of available idle threads drops below MinSpareThreads, Apache will launch a new child process, populated with ThreadsPerChild threads. Similarly, when server load is reduced and the number of idle threads is greater than MaxSpareThreads, Apache will kill off one or more child processes to reduce the idle pool to that number or less.

The goal, when setting these values, is to ensure that there are always idle threads ready to serve any incoming client's request, without having to create a new one. The previous example will work for most sites, as it will ensure that there is at least one completely unused child process, populated with 25 threads, waiting for incoming requests. As soon as threads within this process start to be used, a new child process will be launched for future requests.

The values of MaxClients and ServerLimit should be set so that you will never run out of RAM when a new child process is launched. Look at your process list, using top, or a similar utility, and ensure that ServerLimit, multiplied by the size of an individual server process, does not exceed your available RAM. MaxClients should be less than, or equal to, ServerLimit multiplied by ThreadsPerChild.

Setting the number of threads when using Netware or the perchild MPM

Whereas with most of the other MPMs the MinSpareThreads and MaxSpareThreads directives are server-wide, in the perchild and netware MPMs, these directives are assessed per child process. Of course, with the netware MPM, there is only one child process, so it amounts to the same thing.

With the netware MPM, threads are created and reaped as needed, to keep the number of spare threads between the limits imposed by MinSpareThreads and MaxSpareThreads. The total number of threads must be kept at all times below the limit imposed by the MaxThreads directive.

On the perchild MPM, these limits are set per child process, with each child process monitoring its number of idle threads, and keeping that number between MinSpareThreads and MaxSpareThreads, while keeping at all times the total number of threads below MaxThreadsPerChild.

Because creating additional threads consumes far fewer resources than creating new child processes, it is seldom necessary to adjust these values from their default settings. With the perchild MPM, however, you may wish to adjust the NumServers directive so that you don't run out of RAM. Note that the number set in NumServers is the number of child processes that run all the time, as the perchild MPM does not create and kill child processes, but rather tunes capacity by creating and killing threads.

See Also


Apache Cookbook
Apache Cookbook: Solutions and Examples for Apache Administrators
ISBN: 0596529945
EAN: 2147483647
Year: 2006
Pages: 215

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: