Prefork, worker, and event modes in Apache

Source: Internet
Author: User
Tags http authentication memory usage

To put it simply:

1. There is no thread in prefork. It is a multi-process model. A process processes a connection. It is stable and responds quickly. The disadvantage is that memory consumption is very high when the number of connections is large.

2. worker is a multi-process and multi-threaded model. A process has multiple threads and each thread processes a connection. Compared with prefork, worker mode saves more system memory resources. However, you must note the compatibility between Apache and php program modules in worker mode.

3. event is a variant of the worker mode. It separates the service process from the connection. When KeepAlive is enabled, it can withstand higher concurrent loads than the worker mode. The event mode does not support https access (HTTP authentication problems ).

Switching between prefork and worker modes
1. Rename the current prefork startup file
Mv httpd. prefork
2. Rename the startup file in worker mode
Mv httpd. worker httpd
3. Modify the Apache configuration file
Vi/usr/local/apache2/conf/extra/httpd-mpm.conf
Find the following section, and modify the parameters such as load:

The code is as follows: Copy code
<IfModule mpm_worker_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

4. Restart the service
/Usr/local/apache2/bin/apachectl restart
Start apache2 in worker mode.

For stability and security considerations, we do not recommend you change the apache2 running mode. Use the system default prefork. In addition, many php modules cannot work in worker mode. For example, php in redhat linux does not support thread security. So it is best not to switch the working mode.

Comparison of prefork and worker modes
In prefork mode, multiple sub-processes are used. Each sub-process has only one thread. Each process can maintain only one connection at a specified time. On most platforms, the Prefork MPM is more efficient than the Worker MPM, but the memory usage is much larger. Prefork's wireless program design will be more advantageous in some cases than worker: it can use third-party modules that do not handle thread security well, and for platforms that have difficulty in thread debugging, it is easier to debug.

In worker mode, multiple sub-processes are used. Each sub-process has multiple threads. Each thread can maintain only one connection at a specified time. Generally, on a high-traffic HTTP server, Worker MPM is a good choice, because the memory usage of Worker MPM is much lower than that of Prefork MPM. However, the worker MPM is also imperfect. If a thread crashes, the whole process will "die" together with all its threads ". because threads share memory space, a program must be recognized by the system as "every thread is safe" during runtime ".

In general, the speed of the prefork method is slightly higher than that of the worker method. However, it requires more cpu and memory resources than woker.

Prefork mode configuration

The code is as follows: Copy code
<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. To set a larger value, add the ServerLimit parameter. 20000 is the maximum value of ServerLimit. If you need more, you must compile apache without re-compiling Apache.
Prerequisite: it must be placed before other instructions.

StartServers
Specifies the number of sub-processes created when the server starts. The default value of prefork is 5.

MinSpareServers
Minimum number of idle sub-processes. The default value is 5. If the number of idle sub-processes is less than MinSpareServers, Apache will generate a new sub-process at a maximum speed per second. Do not set this parameter too large.

MaxSpareServers
Set the maximum number of idle sub-processes. The default value is 10. If there are idle sub-processes that exceed the number of MaxSpareServers, the parent process will kill the redundant sub-processes. Do not set this parameter too large. If you set the value of this command to a smaller value than MinSpareServers, Apache will automatically change it to "MinSpareServers + 1 ".

MaxClients
Limit the maximum number of client access requests (the number of concurrent threads of a single process) at the same time. The default value is 256. Any request that exceeds the limit of MaxClients will enter the waiting queue. Once a link is released, the request in the queue will be served. To increase this value, you must increase ServerLimit at the same time.

MaxRequestsPerChild
Each sub-process allows the maximum number of requests of the servo within its lifetime. The default value is 10000. When the limit of MaxRequestsPerChild is reached, the sub-process will end. If MaxRequestsPerChild is "0", the child process will never end. Setting MaxRequestsPerChild to a non-zero value has two advantages:
1. It can prevent (accidental) infinite memory leakage and thus exhaust the memory.
2. A limited life cycle is provided for processes, which helps reduce the number of active processes when server load is reduced.

Worker mode configuration

The code is as follows: Copy code
<IfModule mpm_worker_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

StartServers
Number of sub-processes created when the server starts. The default value is "3 ".

MaxClients
Maximum number of access requests (maximum number of threads) that can be simultaneously servo ). Any request that exceeds the MaxClients limit will enter the waiting queue. The default value is "400", 16 (ServerLimit) multiplied by 25 (ThreadsPerChild. Therefore, to add MaxClients, you must add the ServerLimit value at the same time.

MinSpareThreads
The minimum number of idle threads. The default value is "75 ". This MPM monitors the number of idle threads based on the entire server. If the total number of idle threads on the server is too small, the child process will generate a new idle thread.

MaxSpareThreads
Set the maximum number of idle threads. The default value is 250 ". This MPM monitors the number of idle threads based on the entire server. If the total number of idle threads on the server is too large, the child process will kill the redundant idle threads. The value range of MaxSpareThreads is limited. Apache will automatically correct the value you set according to the following restrictions: worker requires that the value is greater than or equal to MinSpareThreads and the sum of ThreadsPerChild.

ThreadsPerChild
The number of resident execution threads created by each sub-process. The default value is 25. After these threads are created at startup, the child process will no longer create new threads.

MaxRequestsPerChild
Sets the maximum number of requests that each sub-process allows for the servo during its lifetime. When the limit of MaxRequestsPerChild is reached, the sub-process will end. If MaxRequestsPerChild is "0", the child process will never end. Setting MaxRequestsPerChild to a non-zero value has two advantages:
1. It can prevent (accidental) infinite memory leakage and thus exhaust the memory.
2. A limited life cycle is provided for processes, which helps reduce the number of active processes when server load is reduced.
Note that for KeepAlive connections, only the first request is counted. In fact, it changes the behavior of each sub-process to limit the maximum number of links.


# Event MPM

The code is as follows: Copy code


<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
Maxsparethread S 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>


# StartServers: the initial number of server processes started

# MinSpareThreads: the minimum number of worker threads to save for backup.

# MaxSpareThreads: the maximum number of worker threads to save for backup

# ThreadsPerChild: a fixed number of worker threads on each server

# MaxRequestWorkers: maximum number of worker threads

# MaxConnectionsPerChild: a server process service with the maximum number of connections

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.