apache22 and threads

Michael Powell nightrecon at hotmail.com
Mon Sep 20 12:17:32 UTC 2010

Victor Sudakov wrote:

> Colleagues,
> When building apache22 from ports, would you recommend to enable or to
> disable threads support?
> Even more confusing is the fact that for ports/www/apache22 the default
> is: "Enable threads support in APR is off" (WITHOUT_THREADS=true)
> while for ports/devel/apr1 the default is:
> "Enable Threads in apr is on" (WITH_THREADS=true).
> Thank you in advance for any input.
> PS ports/devel/apr1 will also be used for the subversion client.

I wouldn't mind someone with more apache22-fu to elaborate, correcting the 
following if necessary.

My thoughts are this matters depending upon which mpm you choose to build 
into apache. The default is prefork, and it handles incoming requests by 
spawning child processes. The main shortcoming associated with this approach 
is resources such as database connections are not shareable between the 
child processes, e.g. each must have its own. So each incoming request has 
to fork a child, then build up, consume, and tear down the database 
connection. The lifetime will exist during the keepalive period and just be 
sitting there in memory idle most of the time following the task completion.

A threaded mpm such as worker or event, is designed to spawn threads within 
a process to service incoming requests. One is a hybrid, in that it also 
forks additional processes as well when a preset thread count is reached. 
When all threads are contained within the same process each thread is able 
to share and consume resources in a pool amongst other threads. So an idle 
database connection which has finished serving a previous request can be 
immediately reused by a new thread without a build up tear down cycle.

So my idea of the usage of WITH_THREADS is for the default prefork mpm it 
would be "NO", while for the event mpm it would be "YES".

An additional consideration might be what kind of backend is used. For 
example, since not all of PHP is known to be thread safe it is not 
recommended for use with a threaded server and mod_php. The way to get 
around this situation is to separate PHP from Apache with something like 
mod_fcgid which runs PHP as a FastCGI. This way you can safely run a 
threaded Apache with non-thread safe PHP. As far as which is the better 
approach I still am not really sure. Each has its set of pros and cons.


More information about the freebsd-questions mailing list