Apache's prefork mode and worker mode

Source: Internet
Author: User

Prefork mode

Multi-process
This multi-path processing module (MPM) implements a non-threaded, pre-derived Web server that works in a similar way to Apache 1.3. It is suitable for systems that do not have a thread-safe library and need to avoid threading compatibility issues. It is the best mpm to require each request to be independent of each other, so that if a request has a problem it will not affect the other request.

This MPM has a strong self-tuning capability and requires very little adjustment of the configuration instructions. The most important thing is to set the maxclients to a value that is large enough to handle the potential request spikes, and not too large to use more memory than the size of the physical memory.

Worker mode

Multi-process + multithreading
This multi-path processing module (MPM) enables a network server to support mixed multithreaded multi-process. Because a thread is used to process requests, a large amount of requests can be processed, while the overhead of system resources is less than the process-based MPM. However, it also uses a multi-process, with each process having multiple threads to obtain the stability of the process-based MPM.

The most important instructions for controlling this mpm are the Threadsperchild instructions that control the number of threads allowed to be established by each subprocess, and the maxclients instruction that controls the number of buses allowed to be established.

Prefork and worker mode switching
1. Rename the current prefork mode startup file
MV httpd httpd.prefork
2. Rename the startup file for worker mode
MV httpd.worker httpd
3. Modify the Apache configuration file
vi/usr/local/ Apache2/conf/extra/httpd-mpm.conf
Find the following paragraph inside, you can modify the load parameters such as:
<ifmodule mpm_
startservers 2
maxclients worker_module>
Span style= "width:480px" >minsparethreads
maxsparethreads
threadsperchild
maxrequestsperchild 0
</ifmodule>
4. Restart the service
/usr/local/apache2/bin/apachectl restart
You can switch to worker mode to start apache2

In the stability and security considerations, it is not recommended to replace the operating mode of APACHE2, using the system default Prefork can be. In addition, many PHP modules do not work in worker mode, for example, Redhat Linux comes with PHP that does not support thread safety. Therefore, it is best not to switch to working mode.

Comparison of prefork and worker modes
Prefork mode uses multiple child processes, with only one thread per child process. Each process can only maintain one connection at a certain time. On most platforms, the Prefork MPM is more efficient than the worker mpm, but the memory usage is much larger. Prefork's wireless path design in some cases will be more advantageous than a worker: it can use third-party modules that do not handle thread safety, and it is easier to debug for platforms that have difficulty debugging threads.

Worker mode uses multiple child processes, each of which has multiple threads. Each thread can only maintain one connection at a certain time. In general, worker MPM is a good choice on a high-traffic HTTP server because the worker MPM uses much less memory than Prefork MPM. But the worker mpm is also imperfect, and if a thread crashes, the entire process will "die" along with all of its threads. Because threads share memory space, a program must be recognized by the system as "every thread is safe" at run time.

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

Prefork Mode Configuration Detailed
<ifmodule mpm_prefork_module>
Serverlimit 256
Startservers 5
Minspareservers 5
Maxspareservers 10
MaxClients 256
Maxrequestsperchild 0
</IfModule>
Serverlimit
The default maxclient maximum is 256 threads, if you want to set a larger value, add serverlimit this parameter. 20000 is the maximum value of the Serverlimit parameter. If you need to be larger, you must compile Apache before you need to recompile Apache.
Effective premise: Must be placed in front of other directives

Startservers
Specifies the number of child processes that are established when the server starts, and Prefork defaults to 5.

Minspareservers
Specifies the minimum number of idle child processes, which defaults to 5. If the number of currently idle child processes is less than minspareservers, then Apache will produce a new subprocess at a maximum speed of one second. Do not set this parameter too large.

Maxspareservers
Sets the maximum number of idle child processes, which defaults to 10. If there are currently more than maxspareservers of idle child processes, the parent process kills the extra child processes. Do not set this parameter too large. If you set the value of this directive to be smaller than Minspareservers, Apache will automatically modify it to "minspareservers+1".

MaxClients
Limit the number of client maximum access requests at the same time (number of concurrent threads per process), which defaults to 256. Any request exceeding the maxclients limit will enter the waiting queue, and once a link is freed, the request in the queue will be serviced. To increase this value, you must increase the serverlimit at the same time.

Maxrequestsperchild
The maximum number of requests that each child process allows for a servo during its lifetime, by default 10000. When the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0", the child process will never end. Setting Maxrequestsperchild to a value other than 0 has two benefits:
1. You can prevent (accidental) memory leaks from being carried out indefinitely, thus exhausting memory.
2. Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced.

Worker Mode configuration Detailed
<ifmodule mpm_worker_module>
Startservers 2
MaxClients 150
Minsparethreads 25
Maxsparethreads 75
Threadsperchild 25
Maxrequestsperchild 0
</IfModule>

Startservers
The number of child processes that were established when the server started, and the default value is "3".

MaxClients
Maximum number of Access requests (maximum number of threads) that allow simultaneous servos. Any requests exceeding the maxclients limit will enter the waiting queue. The default value is "Serverlimit", multiplied by the result of (Threadsperchild). Therefore, to increase maxclients, you must increase the value of serverlimit at the same time.

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 produce a new idle thread.

Maxsparethreads
Sets the maximum number of idle 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 many, the child process kills the extra idle threads. The value range of the maxsparethreads is limited. Apache will automatically fix the values you set as follows: The worker requires that it is greater than or equal to Minsparethreads plus threadsperchild.

Threadsperchild
The number of resident execution threads established by each child process. The default value is 25. When a child process establishes these threads at startup, no new threads are established.

Maxrequestsperchild
Sets the maximum number of requests per child process to allow the servo during its lifetime. When the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0", the child process will never end. Setting Maxrequestsperchild to a value other than 0 has two benefits:
1. You can prevent (accidental) memory leaks from being carried out indefinitely, thus exhausting memory.
2. Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced.
Note for keepalive links, only the first request is counted. In fact, it changes the behavior of each child process to limit the maximum number of links.

Monitor thread usage Status by page: Http://127.0.0.1/server-status

Apache's prefork mode and worker mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.