Difference between Prefork and Worker apache modules.

MPM stands for multiprocessing module. We can see the default mpm using the command ”  httpd -l ”

Apache 2 is mainly comes with the following  MPM’s

prefork
worker
mpm_winnt
This Multi-Processing Module is optimized for Windows NT.
mpm_netware
Multi-Processing Module implementing an exclusively threaded web server optimized for Novell NetWare

 

1) Prefork MPM

prefork mpm  handles requests just like apche 1.3. As the name implies this will pre fork necessary child process while starting apache. It is suitable for websites which avoids threading for compatibility for non-thread-safe libraries .  It is also known as the best mpm for isolating each request.

Working : –
A single control process is responsible for launching child processes which listen for connections and serve them when they arrive. Apache always tries to maintain several spare or idle server processes, which stand ready to serve incoming requests. In this way, clients do not need to wait for a new child processes to be forked before their requests can be served.
We can adjust this spare process through the apche conf. For a normal server which is having 256 simultaneous  connections can use the default prefork settings.

Perfork is the default module given by apache.

# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild directive sets the limit on the number of requests that an individual child server process will handle. After MaxRequestsPerChild requests, the child process will die. If MaxRequestsPerChild is 0, then the process will never expire

2) Worker MPM

This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server.

The most important directives used to control this MPM are ThreadsPerChild, which controls the number of threads deployed by each child process and MaxClients, which controls the maximum total number of threads that may be launched.

Advantage      : Memory usage and performanance wise its better than prefork
Disadvantage : worker will not work properly with languages like php

Working : –

A single control process (the parent) is responsible for launching child processes. Each child process creates a fixed number of server threads as specified in the ThreadsPerChild directive, as well as a listener thread which listens for connections and passes them to a server thread for processing when they arrive.

Apache always tries to maintain a pool of spare or idle server threads, which stand ready to serve incoming requests. In this way, clients do not need to wait for a new threads or processes to be created before their requests can be served. The number of processes that will initially launched is set by the StartServers directive. During operation, Apache assesses the total number of idle threads in all processes, and forks or kills processes to keep this number within the boundaries specified by MinSpareThreads and MaxSpareThreads. Since this process is very self-regulating, it is rarely necessary to modify these directives from their default values. The maximum number of clients that may be served simultaneously (i.e., the maximum total number of threads in all processes) is determined by the MaxClients directive. The maximum number of active child processes is determined by the MaxClients directive divided by the ThreadsPerChild directive

 
You can refer more about these multiprocessing modules here  :Apace Modules

  • Norbert Hoeller

    Thanks for this post! I was getting lost in the Apache documentation.

    I am running a VPS that started to have memory issues after my hosting provider upgraded to Virtuozzo 4. Magically after a reboot on August 3rd, my memory dropped by nearly half. Unfortunately, it bounced back after the weekly Apache restart on August 7th.

    Looking at Apache processes before August 3rd and after August 7, I have 10 free slots, 5 idle servers and one or at most two busy servers. In between those dates, it appears the number of idle servers is one more than the number of busy servers. My guess is that my MinSpareServer value was less between August 3rd and August 7th, although I certainly did not change the values. I did find a swtune.conf that appears to have much more restrictive values. The problem is that it appears some swtune.conf values and some httpd.conf values are currently running. I may only be a little closer to the problem, but at least see a path to follow!
    Thanks again, Norbert

  • admin

    Dear Norbert,

    Thank you for the update.

  • wow!!! good information.. keep posting

  • agu

    Thanks !!!