Apache Prefork vs Worker MPM
Looking at the Apache config file, I see Prefork and Worker MPM defined. What is the difference and which one is Apache using?
Solution 1:
Prefork and worker are two type of MPM apache provides. Both have their merits and demerits.
By default mpm is prefork which is thread safe.
Prefork MPM uses multiple child processes with one thread each and each process handles one connection at a time.
Worker MPM uses multiple child processes with many threads each. Each thread handles one connection at a time.
For more details you can visit https://httpd.apache.org/docs/2.4/mpm.html and https://httpd.apache.org/docs/2.4/mod/prefork.html
Solution 2:
Apache's Multi-Processing Modules (MPMs) are responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests (http://httpd.apache.org/docs/2.2/mpm.html).
They're like any other Apache module, except that just one and only one MPM must be loaded into the server at any time. MPMs are chosen during configuration and compiled into the server by using the argument --with-mpm=NAME
with the configure script where NAME
is the name of the desired MPM.
Apache will use a default MPM for each operating system unless a different one is choosen at compile-time (for instance on Windows mpm_winnt
is used by default). Here's the list of operating systems and their default MPMs:
- BeOS
beos
- Netware
mpm_netware
- OS/2
mpmt_os2
- Unix/Linux
prefork
(update for Apache version ≥ 2.4:prefork
,worker
, orevent
, depending on platform capabilities) - Windows
mpm_winnt
To check what modules are compiled into the server use the command-line option -l
(here is the documentation). For instance on a Windows installation you might get something like:
> httpd -l
Compiled in modules:
core.c
mod_win32.c
mpm_winnt.c
http_core.c
mod_so.c
As of version 2.2 this is the list of available core features and MPM modules:
-
core
- Core Apache HTTP Server features that are always available -
mpm_common
- A collection of directives that are implemented by more than one multi-processing module (MPM) -
beos
- This Multi-Processing Module is optimized for BeOS. -
event
- An experimental variant of the standard worker MPM -
mpm_netware
Multi-Processing Module implementing an exclusively threaded web server optimized for Novell NetWare -
mpmt_os2
Hybrid multi-process, multi-threaded MPM for OS/2 -
prefork
Implements a non-threaded, pre-forking web server -
mpm_winnt
- This Multi-Processing Module is optimized for Windows NT. -
worker
- Multi-Processing Module implementing a hybrid multi-threaded multi-process web server
Now, to the difference between prefork
and worker
.
The prefork
MPM
implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.
The worker
MPM implements a hybrid multi-process multi-threaded server and gives better performance, hence it should be preferred unless one is using other modules that contain non-thread-safe libraries (see also this discussion or this on Serverfault).
Solution 3:
Take a look at this for more detail. It refers to how Apache handles multiple requests. Preforking, which is the default, starts a number of Apache processes (2 by default here, though I believe one can configure this through httpd.conf). Worker MPM will start a new thread per request, which I would guess, is more memory efficient. Historically, Apache has used prefork, so it's a better-tested model. Threading was only added in 2.0.