Process Model – Apache

Multi-Processing Modules

‘Multi Processing Modules’ aka MPM are modules that extends apache’s capability to implement a hybrid multi-process multi-threaded server.

MPM’s deal with making sure there are enough processes or threads lying idle to handle incoming web requests. They generally start one parent process and manage a collection of child processes, threads, or a combination of both.

 Apache provides dynamic pool-size implementation. Web server’s parent process initially forks a number of child processes, which is provided as a configuration parameter. Parent process monitors the load and accordingly manage the child processes (either forking new processes or killing the existing). This again depends on the configuration parameter.

You can also regulate pool size in Apache’s configuration file by specifying the maximum number of requests a child can process before it dies. Once a process has reached the parameter-specified value, it exits

 Broadly, there are two types of MPM available. There are inherent advantages to each model and it is up to each administrator to weigh the pros-and-cons of this decision. The Model affects how Apache behaves and thus affects its performance.

Prefork MPM

In the pre-fork model, there is one thread per process and each process handles one request. The advantages of the pre-fork model are:

  • The number of children is always a known quantity, making troubleshooting easier.
  • Each request is served by a unique process on the system, which adds to security.
  • The pre-fork model has been in use for more than 10 years, making it very well-known and stable.
  • Good for non-thread-safe third party modules.

Disadvantages include:

  • Has higher Memory Consumption, since it spawns a process for each request.
  • Has Lower performance compared to thread-based MPMs

 
Worker MPM

Worker MPM is the new model introduced in Apache 2.x, which is based on multi-threading architecture. Each process will spawn threads and the threads are responsible for handling user requests.

Worker MPM facilitates lower memory footprint per request as multiple threads execute in shared memory space and thus provides high scalability.  Since the threads spawn within the same process, hence they share memory, thereby reducing the memory consumption.

 Advantages:

  • It can scale rather quickly since one additional child can handle multiple requests.
  • Lower memory footprint.

Disadvantages:

The disadvantage is that if one thread dies, the entire process dies, potentially taking down worker threads actively processing requests.

 
 
 Wint NT

Windows processes are heavy-weight, meaning they are costly to spawn. Thus the WinNT parent process starts only one child process with many, many threads.

On the Windows side, the only useful directive is ThreadsPerChild, which is usually set to a value of 250 [defaults to 64 without a value]. If you expect more, or less, concurrent connections/requests, set this directive appropriately. Check process size with Task Manager, under different values and server load.

 
Some of the important Configuration parameters

StartServers
The StartServers directive sets the number of child server processes created on startup. As the number of processes is dynamically controlled depending on the load, there is usually little reason to adjust this parameter.
For worker, the default is StartServers 3. For prefork, default is 5.
 

Maxclients
Specifies the maximum number of Apache processes that can run at once.
Limiting the number of Apache processes can prevent overrunning your hardware capabilities.
Set this number too low and resources will go to waste. Set this number too high and an influx of connections will bring the server to a stand still. Set this number just right and your server will fully utilize the available resources.
If the allocated MaxClients is higher than ServerLimit * ThreadLimit Apache will automatically reduce MaxClients to the value of ServerLimit * ThreadLimit
Requests that go beyond the MaxClients limit, will normally be queued, upto a number based on the ListenBacklog directive.
For Prefork, MaxClients translates into the maximum number of child processes that will be launched to serve requests.
For worker, MaxClients restricts the total number of threads that will be available to serve clients.

MinSpare Threads
Minimum number of idle threads to handle requests.
Worker uses a default of 75 MinSpareThreads.
If there aren’t enough idle threads in the server then child processes are created until the number of idle threads is greater than number.

MaxSpareThreads
Maximum number of idle threads.
For worker, the default is MaxSpareThreads 250. If there are too many idle threads in the server then child processes are killed until the number of idle threads is less than this number.
For worker this value must be greater or equal than the sum of MinSpareThreads and ThreadsPerChild. Apache will correct the value if the above rule is not followed.

ThreadsPerChild
Specifies the number of threads that will be created in each child process.
If using an MPM like mpm_winnt, where there is only one child process, this number should be high enough to handle the entire load of the server.
If using an MPM like worker, where there are multiple child processes, the total number of threads should be high enough to handle the common load on the server.
The default value for ThreadsPerChild is 64 when used with mpm_winnt and 25 when used with the others.
If you increase the ThreadsPerChild up to a value that the APACHE Server will consider it cannot allocate the needed memory, APACHE will exit.

ServerLimit
For the prefork MPM, this directive sets the maximum configured value for MaxClients for the lifetime of the Apache process. For the worker MPM, this directive in combination with ThreadLimit sets the maximum configured value for MaxClients for the lifetime of the Apache process.
If ServerLimit is set to a value much higher than necessary, extra, unused shared memory will be allocated. If both ServerLimit and MaxClients are set to values higher than the system can handle, Apache may not start or the system may become unstable.


MinSpareServers
The MinSpareServers directive sets the desired minimum number of idle child server processes. An idle process is one which is not handling a request. If there are fewer than MinSpareServers idle, then the parent process creates new children at a maximum rate of 1 per second.

MaxSpareServers
The MaxSpareServers directive sets the desired maximum number of idle child server processes. An idle process is one which is not handling a request. If there are more than MaxSpareServers idle, then the parent process will kill off the excess processes.
Tuning of this parameter should only be necessary on very busy sites. Setting this parameter to a large number is almost always a bad idea. If you are trying to set the value equal to or lower than MinSpareServers, Apache will automatically adjust it to MinSpareServers + 1.

 
 MaxRequestsPerChild
The 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.
The default value for mpm_winnt is 0.
Setting MaxRequestsPerChild to a non-zero value limits the amount of memory that process can consume by (accidental) memory leakage.

Apache always try to maintain a pool of spare threads, which stand ready to serve incoming requests. The number of processes that will initially launched is set by the StartServers directive. Apache will try to keep the number of spare threads within the boundaries specified by MinSpareThreads and MaxSpareThreads. 

 
 

How to use a particular MPM

Use of MPM is a compile time decision and it cannot be changed without recompiling the Apache.

So, it an important decision to be made before compiling the Apache.

When compiling on a *nix environment, the default value for Apache MPM is prefork, but you can specify it while compiling Apache with the following configuration directive

–with-mpm=prefork or –with-mpm=worker

 
How to find which MPM in use
In order to find out what MPM apache has been configured with, you need to check “apachectl – l“.  This will list either prefork.c or worker.c

Leave a comment