Apache MPM
Prefork
A single control (master) process is responsible for launching multiple child processes which serves incoming http requests, it spins of a number of child processes for serving requests, and the child processes only serve one request at a time.
In this way, clients do not need to wait for a new child processes to be forked before their requests can be served because the master process creates several spare processes, which stand ready to serve incoming requests.
We can adjust this spare process through the Apache configuration.
IfModule prefork.c>
StartServers 8 # number of server processes to start
MinSpareServers 5 # minimum number of server processes which are kept spare
MaxSpareServers 20 # maximum number of server processes which are kept spare
ServerLimit 256 # maximum value for MaxClients for the lifetime of the server
MaxClients 256 #maximum number of server processes allowed to start
/IfModule>
Number of processes or PIDs you can have = MaxClients
It’s very easy to install Apache Prefork MPM style, however it’s memory consumer as it will fork a process for each connection!!! We have to increase your RAM in case of high traffic.
Worker
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 that controls the number of threads deployed by each child process and “MaxClients”, which controls the maximum total number of threads that may be launched.
, as well as a listener thread which listens for connections and passes them to a server thread for processing when they arrive.
In simple words, it uses multiple child processes with many threads each that handles one connection at a time and these threats are set free back up immediately after the request is completed.
That means Worker MPM style is memory usage and performance wise its better than prefork.
IfModule worker.c>
StartServers 5 # Intial number of server processes to start
MaxClients 500 # maximum number of simultaneous client connections
MinSpareThreads 25 # minimum number of worker threads which are kept spare
MaxSpareThreads 100 # maximum number of worker threads which are kept spare
ThreadsPerChild 25 # constant number of worker threads in each server process
MaxRequestsPerChild 0 # maximum number of requests a server process serves
/IfModule>
Number of processes or PIDs you can have = MaxClients / ThreadsPerChild
Finally, we can say Prefork MPM is preferred for better compatibility with older softwares or for simple Apache installation but it uses high memories; while worker MPM is better for high-traffic, smaller memory footprint.
Note that we can have one and only MPM module loaded in apache at any one time.
How to check which MPM is compiled?
# httpd -l