In depth understanding of apahce's working mode perfork worker

  • 2020-05-06 12:10:34
  • OfStack

prefork mode USES multiple child processes with only one thread per child. Each process can only maintain one connection at a certain time. On most platforms, Prefork MPM is more efficient than Worker MPM, but it USES much more memory. prefork's threadless design will have advantages over worker in some cases: it can use third-party modules that don't handle thread safety properly, and it's easier to debug on platforms where threading is difficult.

worker mode USES multiple child processes, each with multiple threads. Each thread can only maintain one connection at a certain time. Generally speaking, Worker MPM is a better choice on a high-traffic HTTP server because Worker MPM has much lower memory usage than Prefork MPM. But worker MPM is also imperfect in that if a thread crashes, the whole process "dies" along with all its threads.

prefork is slightly faster than worker, but it also requires slightly more cpu and memory resources than woker.

See working mode in use:
apachectl -l

Two modes profile + description:
perfork mode:

 
<IfModule mpm_prefork_module> 
ServerLimit 2000 
# Number of connections greater than 256 This item needs to be set. 20000 is ServerLimit The maximum value of this parameter  
StartServers 5 
# Number of child processes created at initialization. prefork The default is 5 
MinSpareServers 10 
# Minimum number of idle processes  
MaxSpareServers 15 
# Maximum number of idle processes  
MaxClients 1000 
# Maximum number of concurrent client connections  
MaxRequestsPerChild 10000 
# Controls how often the server kills old processes to produce new ones  
# will MaxRequestsPerChild Setting to a non-zero value has two benefits:  
#1. Can prevent ( accidental ) Memory leaks proceed indefinitely, thus running out of memory.  
#2. Giving a process a limited lifetime helps reduce the number of active processes as the server load is lightened.  
</IfModule> 


worker mode:

 
<IfModule mpm_worker_module> 
StartServers 3 
# Number of child processes created at initialization  
MaxClients 2000 
# Maximum number of parallel processes. Maximum number of child processes =MaxClients/ThreadsPerChild 
ServerLimit 25 
# Only if you need to MaxClients and ThreadsPerChild Set to exceed the default value 16 This instruction is only needed for child processes. Do not set the ratio of the value of the instruction MaxClients  and ThreadsPerChild High number of child processes required.  
MinSpareThreads 50 
# Minimum number of empty threads  
MaxSpareThreads 200 
# Maximum number of idle threads  
ThreadLimit 200 
# This directive sets the number of configurable threads per child process ThreadsPerChild The value of the instruction should be and ThreadsPerChild The maximum possible value remains the same.  
ThreadsPerChild 100 
# The number of service threads established by the child process  
MaxRequestsPerChild 0 
# Sets the maximum number of requests that each child process can allow for its lifetime. arrive MaxRequestsPerChild The child process will end after the limit of. if MaxRequestsPerChild for "0" , the child process will never end.  
</IfModule> 


Working mode modification method:
Recompile the specified working mode (prefork by default)

 
./configure --with-mpm=worker 


Note:
After making changes to the working mode parameters, the apache service must be turned off and restarted.
Directly starting with restart will be invalid.

Related articles: