The difference between Worker and Prefork in apache
There are differences in the two working modes of Worker and Prefork in Apache in terms of memory usage, stability and compatibility.
- Memory usage
- Worker: Due to the use of threads, the memory usage is less.
- Prefork: Each process runs independently and consumes a lot of memory.
- stability
- Worker: A child process crash will not cause the entire server to stop, but a thread crash will affect all requests within the process.
- Prefork: Processes are independent of each other, and problems with a certain process do not affect other processes, making them more stable.
- compatibility
- Worker: You need to ensure that all third-party modules support thread safety, otherwise problems may arise.
- Prefork: naturally avoids thread safety issues, because each process runs independently.
- Applicable scenarios
- Worker: Suitable for scenarios with high concurrency and high dynamic requests, such as large news sites or social media platforms.
- Prefork: Suitable for scenarios where there is a lot of static content and does not require a lot of concurrent processing, such as internal corporate websites or small projects.
Apache working mode: prefork and worker
As the most widely used and stable open source server software for web servers today, apache has many working modes, currently there are two main modes: prefork mode and worker mode
1. Two modes
Prefork mode:
prefork is the default (default) MPM on the Unix platform, using multiple child processes, each child process has only one thread. Each process can only maintain one connection at a certain time, which is efficient, but has a relatively large memory usage.
This multi-processing module (MPM) implements a non-threaded, pre-derived web server that works similar to Apache 1.3. It is suitable for systems that do not have thread-safe libraries and need to avoid thread compatibility issues. It is the best MPM when each request is independent of each other, so that if a problem occurs with one request, it will not affect other requests.
Worker mode:
The worker uses multiple subprocesses, each subprocess has multiple threads, and each thread can only maintain one connection at a certain time, and the memory usage is relatively small, which is suitable for high-traffic http servers. The disadvantage is that if a thread crashes, the entire process will "dead" with any of its threads, so it is necessary to ensure that a program must be recognized by the system as "every thread is safe" when it runs.
This multi-processing module (MPM) enables the network server to support mixed multi-threaded multi-processes. Since threads are used to process requests, massive requests can be processed, and the overhead of system resources is less than that of process-based MPM. But it also uses multiple processes, each process has multiple threads to obtain the stability of process-based MPM.
2. Viewing and installing apache mode
1. Check the current mode frequently
If apache is already installed, we can use the "httpd -l" command to view the current mode. If found, it means that the current working is in prefork mode, and the same is true that it will work in worker mode.
If apache has not been installed yet, we can add the --with-pem=(prefork|worker) option when compiling to determine which mode to enable.
2. Switch mode
a. Rename the current prefork mode startup file
mv httpd
b. Rename the startup file in worker mode
mv httpd
c. Modify the Apache configuration file
vi /usr/local/apache2/conf/extra/
Find the following paragraph inside and modify the load and other parameters appropriately:
<IfModule mpm_worker_module> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0 </IfModule>
d. Restart the service
/usr/local/apache2/bin/apachectl restart
For stability and security considerations, it is not recommended to change the operation mode of apache2, just use the default prefork of the system. In addition, many php modules cannot work in worker mode, for example, the php that comes with redhat Linux cannot support thread safety. So it is best not to switch the working mode.
3. Comparison of prefork and worker modes
The prefork mode uses multiple child processes, each child process has only one thread. Each process can only maintain one connection at a certain time. On most platforms, Prefork MPM is more efficient than Worker MPM, but has much more memory usage. Prefork's wireless design will have an advantage over worker in some cases: it can use third-party modules that don't handle thread safety well, and it's also easier to debug for platforms that have difficulty debugging.
The worker mode uses multiple child processes, each child process has 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 the memory usage of Worker MPM is much lower than that of Prefork MPM. But the worker MPM is also imperfect. If a thread crashes, the entire process will "dead" along with all its threads. Since threads share memory space, a program must be recognized by the system as "every thread is safe" when it runs.
In general, the speed of the prefork method is slightly higher than that of the worker, but it requires slightly more CPU and memory resources than the worker.
4. Detailed explanation of prefork mode configuration
<IfModule mpm_prefork_module> ServerLimit 256 StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 256 MaxRequestsPerChild 0 </IfModule>
ServerLimit
The default MaxClient has a maximum of 256 threads. If you want to set a larger value, add the ServerLimit parameter. 20000 is the maximum value of the ServerLimit parameter. If you need to be bigger, you must compile apache, and there was no need to recompile Apache before.
Prerequisite for effective: must be placed in front of other instructions
StartServers
Specifies the number of child processes established when the server starts, prefork defaults to 5.
MinSpareServers
Specifies the minimum number of idle child processes, default is 5. If the current number of idle child processes is less than MinSpareServers, Apache will generate new child processes at a maximum rate of one per second. This parameter should not be set too large.
MaxSpareServers
Sets the maximum number of idle child processes, default is 10. If there are currently more than the number of free child processes in MaxSpareServers, the parent process will kill the excess child processes. This parameter should not be set too large. If you set the value of this directive to be smaller than MinSpareServers, Apache will automatically modify it to "MinSpareServers+1".
MaxClients
It limits the maximum number of access requests for clients at the same time (number of concurrent threads for a single process), and the default is 256. Any requests exceeding the MaxClients limit will enter the waiting queue, and once a link 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 allowed by each child process during its lifetime is 10000. After the MaxRequestsPerChild limit is reached, the child process will end. If MaxRequestsPerChild is "0", the child process will never end. There are two benefits to setting MaxRequestsPerChild to a non-zero value:
1. It can prevent (accidental) memory leaks from happening infinitely, thus exhausting memory.
2. Give the process a limited lifespan, which helps reduce the number of active processes when the server load is reduced.
5. Detailed explanation of worker mode configuration
<IfModule mpm_worker_module> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0 </IfModule>
StartServers
The number of child processes created when the server starts, the default value is "3".
MaxClients
The maximum number of access requests (maximum number of threads) allowed to simultaneous servos. Any requests exceeding the MaxClients limit will enter the waiting queue. The default value is "400", 16 (ServerLimit) multiplied by 25 (ThreadsPerChild). Therefore, to increase MaxClients, you must also increase the value of ServerLimit.
MinSpareThreads
The minimum number of idle threads, the default value is "75". This MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too small, the child process will generate new idle threads.
MaxSpareThreads
Sets the maximum number of free threads. The default value is "250". This MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too large, the child process will kill the excess idle threads. The value range of MaxSpareThreads is limited. Apache will automatically correct the value you set according to the following restrictions: the worker requires it to be greater than or equal to MinSpareThreads plus ThreadsPerChild's sum.
ThreadsPerChild
The number of resident execution threads established by each child process. The default value is 25. After the child process establishes these threads at startup, no new threads are created.
MaxRequestsPerChild
Sets the maximum number of requests allowed for each child process to servo during its lifetime. After the MaxRequestsPerChild limit is reached, the child process will end. If MaxRequestsPerChild is "0", the child process will never end. There are two benefits to setting MaxRequestsPerChild to a non-zero value:
1. It can prevent (accidental) memory leaks from happening infinitely, thus exhausting memory.
2. Give the process a limited lifespan, which helps reduce the number of active processes when the server load is reduced.
Note that for KeepAlive links, only the first request will be counted. In fact, it changes the behavior of limiting the maximum number of links per child process.
6. Summary
In the past, the mainstream apache mode was prefork, but now there are more worker modes. To be different, the worker mode can cope with high traffic, but the security is not good; the prefork mode is relatively safe, but the performance will be a little worse. You can choose different modes according to the category of your server to better use apache.
This is the end of this article about the difference between Worker and Prefork in apache. For more related apache Worker and Prefork content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!