Apache Prefork vs Worker MPM Mode

Prefork Mode
This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that works similarly to Apache 1.3. It is suitable for systems that lack thread-safe libraries and need to avoid threading compatibility issues. It is the best MPM when you need to isolate each request from others, so that if one request encounters a problem, it does not affect other requests.

This MPM is highly self-regulating and requires very few configuration directive adjustments. The most important thing is to set MaxClients to a value large enough to handle potential peak request loads, but not so large that the memory required exceeds the physical memory available.

Worker Mode
This Multi-Processing Module (MPM) enables the web server to support a hybrid multi-threaded, multi-process model. Because it uses threads to handle requests, it can process a massive number of requests with lower system resource overhead compared to process-based MPMs. However, it also uses multiple processes, with each process having multiple threads, to gain the stability of a process-based MPM.

The most important directives for controlling this MPM are ThreadsPerChild, which controls the number of threads allowed per child process, and MaxClients, which controls the total number of threads allowed.

Switching Between Prefork and Worker Modes
1. Rename the current prefork mode startup file
mv httpd httpd.prefork
2. Rename the worker mode startup file
mv httpd.worker httpd
3. Modify the Apache configuration file
vi /usr/local/apache2/conf/extra/httpd-mpm.conf
Find the following section and adjust parameters like load limits as needed:

StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0

4. Restart the service
/usr/local/apache2/bin/apachectl restart
This will start Apache2 in worker mode.

For stability and security reasons, it is not recommended to change the operating mode of Apache2; using the system default prefork is fine. Additionally, many PHP modules cannot work in worker mode; for instance, the PHP bundled with Red Hat Linux does not support thread safety. Therefore, it is best not to switch the working mode.

Comparison of Prefork and Worker Modes
Prefork mode uses multiple child processes, with each child process having only one thread. Each process can only maintain one connection at any given time. On most platforms, Prefork MPM is more efficient than Worker MPM, but it uses significantly more memory. Prefork’s thread-less design can be advantageous in certain situations: it can use third-party modules that haven’t handled thread safety properly, and it is easier to debug on platforms where thread debugging is difficult.

Worker mode uses multiple child processes, with each child process having multiple threads. Each thread can only maintain one connection at any given time. Generally speaking, on a high-traffic HTTP server, Worker MPM is a better choice because its memory usage is much lower than Prefork MPM. However, Worker MPM is not without its flaws: if one thread crashes, the entire process, along with all its threads, will “die.” Since threads share memory space, a program must be recognized by the system as “each thread is safe” when running.

Overall, prefork mode is slightly faster than worker mode, but it also requires slightly more CPU and memory resources.

Detailed Prefork Mode Configuration

ServerLimit 256
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 256
MaxRequestsPerChild 0

ServerLimit
The default maximum for MaxClient is 256 threads. If you want to set a larger value, you must add the ServerLimit parameter. 20000 is the maximum value for the ServerLimit parameter. If you need a value larger than that, you must recompile Apache; previously, this did not require recompiling Apache.
Effective precondition: Must be placed before other directives.

StartServers
Specifies the number of child processes created when the server starts. The default for prefork is 5.

MinSpareServers
Specifies the minimum number of idle child processes. The default is 5. If the current number of idle child processes is less than MinSpareServers, Apache will create new child processes at a maximum rate of one per second. Do not set this parameter too high.

MaxSpareServers
Sets the maximum number of idle child processes. The default is 10. If there are more idle child processes than MaxSpareServers, the parent process will kill the excess child processes. Do not set this parameter too high. If you set the value of this directive to be smaller than MinSpareServers, Apache will automatically adjust it to “MinSpareServers+1”.

MaxClients
Limits the maximum number of simultaneous client access requests (the number of concurrent threads per process). The default is 256. Any requests exceeding the MaxClients limit will enter a waiting queue; once a connection is released, the requests in the queue will be served. To increase this value, you must also increase ServerLimit.

MaxRequestsPerChild
The maximum number of requests each child process is allowed to serve during its lifetime. The default is 10000. After reaching the MaxRequestsPerChild limit, the child process will terminate. If MaxRequestsPerChild is “0”, the child process will never terminate. Setting MaxRequestsPerChild to a non-zero value has two benefits:
1. It can prevent (accidental) memory leaks from running indefinitely and exhausting memory.
2. It gives processes a finite lifespan, which helps reduce the number of active processes when the server load decreases.

Detailed Worker Mode Configuration

StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0

StartServers
The number of child processes created when the server starts. The default value is “3”.

MaxClients
The maximum number of simultaneous access requests (maximum number of threads) allowed to be served. Any requests exceeding the MaxClients limit will enter a waiting queue. The default value is “400”, the result of 16 (ServerLimit) multiplied by 25 (ThreadsPerChild). Therefore, to increase MaxClients, you must also increase the ServerLimit value.

MinSpareThreads
The minimum number of idle threads. The default value is “75”. This MPM monitors the number of idle threads across the entire server. If the total number of idle threads in the server is too low, child processes will create new idle threads.

MaxSpareThreads
Sets the maximum number of idle threads. The default value is “250”. This MPM monitors the number of idle threads across the entire server. If the total number of idle threads in the server is too high, child processes will kill the excess idle threads. The value range for MaxSpareThreads is limited. Apache will automatically correct the value you set according to the following restriction: worker requires it to be greater than or equal to the sum of MinSpareThreads plus ThreadsPerChild.

ThreadsPerChild
The number of persistent execution threads created per child process. The default value is 25. After creating these threads at startup, the child process will not create any new threads.

MaxRequestsPerChild
Sets the maximum number of requests each child process is allowed to serve during its lifetime. After reaching the MaxRequestsPerChild limit, the child process will terminate. If MaxRequestsPerChild is “0”, the child process will never terminate. Setting MaxRequestsPerChild to a non-zero value has two benefits:
1. It can prevent (accidental) memory leaks from running indefinitely and exhausting memory.
2. It gives processes a finite lifespan, which helps reduce the number of active processes when the server load decreases.
Note that for KeepAlive connections, only the first request is counted. In effect, this changes the behavior of limiting the maximum number of connections per child process.

Source http://www.ccvita.com/339.html

Leave a Comment

Your email address will not be published.