Analysis of Apache prefork, worker, and event mpm

Source: Internet
Author: User

 

 

Introduction to three MPM types

 

 

Apache 2.x supports plug-in parallel processing modules, called multipleRoad Processing Module(MPM ). When compiling Apache, You must select or select only one MPM. For Unix-like systems, there are several different MPM options that affect Apache's speed and scalability. 

Prefork mpm: This multi-path Processing Module(MPM)Implements a non-thread-type, pre-derivedWebServer, which works in a way similarIn Apache 1.3. It is suitable for systems that do not have a thread security library and need to avoid thread compatibility issues. It is best to require that each request be independent of each other.MpmIn this way, if a request fails, other requests will not be affected.

ThisMpmIt has strong self-regulation capability and requires only a small amount of configuration instruction adjustment. The most important thing isMaxclients is set to a value large enough to handle potential request peaks, and cannot be too large, resulting in memory usage exceeding the physical memory size.

 

Worker mpm: This multi-path Processing Module(MPM)Make the network server support mixed multi-threaded Multi-process. Because threads are used to process requests, a large number of requests can be processed, and the overhead of system resources is less than that of process-basedMpm. However, it also uses multiple processes, and each process has multiple threads to obtain process-basedMpmStability.

The number of threads that each process can have is fixed. The server increases or reduces the number of processes based on the load. A separate control process(Parent Process)Establishes sub-processes. Each sub-process can establishThe number of threadsperchild service threads and a listening thread. The listening thread listens for access requests and sends them to the service thread for processing and response.

 

Whether in worker mode or prefork mode, ApacheAlways try to keep some standby(Spare)Or idle sub-process(Idle service thread pool) is used to receive incoming requests. In this way, the client does not need to wait for the sub-process to generate before obtaining the service.

 

Event mpm : The above two types of stable Mpm This method is insufficient for extremely busy server applications. Although HTTP Of Keepalive Method can be reduced TCP Number of connections and network load, Keepalive It needs to be bound to a service process or thread, which causes a busy server to consume all the threads. Event MPM is a new model to solve this problem. It separates service processes from connections. When the server processes fast and has a very high click rate, the number of available threads is the key resource limit. Event mpm Method is the most effective. A Worker mpm Busy servers that work in this way can withstand tens of thousands of visits per second (for example, during peak hours of large news service sites), while Event mpm It can be used to handle higher loads. It is worth noting that, Event mpm Cannot be in Security HTTP ( HTTPS .

For eventMode,ApacheThe following warning is given:

This MPM is experimental, so it may or may not work as expected.

This mpmHe is currently in trial and may not work as expected.

 

 

How to configure three MPM types

 

prefork Yes UNIX default MPM , the pre-Dispatch process is also used Apache 1.3 . prefork threads are not used. 2.0 This is used in conjunction with 1.3 versions maintain compatibility, sort k separate sub-processes are used to handle different requests. The requests are independent of each other, this also makes it the most stable MPM .

How to view the currently installedApacheOfMpm.

 
[Root @ localhost Apache] # httpd-lcompiled in modules: core. cPrefork. cHttp_core.c mod_so.c

If you seePerfork. cIndicates thatPerfork mpmMode.Worker. cIndicatesWorker mpmMode.

 

How to SetApacheOfMpmWhat about it?

RequiredApacheYou must specify the mode When configuring the installation:

 
[Root @ localhost httpd-2.4.1] #./configure -- prefix =/usr/local/apache2worker -- enable-so-- With-MPM = worker[Root @ localhost httpd-2.4.1] # Make [root @ localhost httpd-2.4.1] # make install

Specify -- With-MPM = NameMpm,Name is what you want to useMpm.If the mode is not specified, the default value is prefork MPM.. 

 

How to configure it to event mpm?

Like the above method, you only need to add the following parameters during installation: -- enable-nonportable-atomics = Yes

It should be noted that the event MPM for the old CPUIt may not be supported.

 

 

Analysis of Three MPM Parameters

 

Whether you installApacheMpm

Open.../Apache/CONF/extra/httpd-mpm.confFile, find the following Configuration:

 

# Perfork mpm

<IfmoduleMpm_prefork_module>Startservers 5 minspareservers 5 maxspareservers 10 maxrequestworkers 250 maxconnectionsperchild 0</Ifmodule> 

# Startservers: Number of server processes started

# Minspareservers: the minimum number of server processes to save for backup

# Maxspareservers: the maximum number of server processes. Save the backup.

# Maxrequestworkers: the maximum number of server processes allowed to start

# Maxconnectionsperchild: A server process service with the maximum number of connections

After the "startservers" sub-process is initially created, the prefork control process creates a process to meet the needs of the minspareservers settings. Wait for one second to create two more processes. Wait for another second to create four more processes ...... In this way, the number of Created processes is increased exponentially, up to 32 processes per second until the value set by minspareservers is met. This mode eliminates the need to generate new processes when requests arrive, thus reducing system overhead and increasing performance. Maxspareservers sets the maximum number of idle processes. If the number of idle processes is greater than this value, Apache automatically kill unnecessary processes. This value should not be too large, but if it is smaller than minspareservers, Apache will automatically adjust it to minspareservers + 1. If the site load is large, consider increasing minspareservers and maxspareservers at the same time.

Maxrequestsperchild sets the number of requests that each sub-process can process. Each sub-process will automatically destroy it after processing "maxrequestsperchild" requests. 0 means that the sub-process will never be destroyed. Although the default value is 0, each sub-process can process more requests, setting it to a non-zero value also has two important advantages:

1. prevent accidental Memory leakage.2. When the server load drops, the number of sub-processes is automatically reduced.

Therefore, you can adjust this value based on the server load.

MaxrequestworkersThe Instruction Set also limits the number of service requests. Any connection attempts inMaxrequestworkerslimitWill usually be queued up to several based onThe listenbacklog command. 

In apache2.3.13Previous versionsMaxrequestworkersCalledMaxclients.

(Maxclients is the most important of these commands. It sets the request that Apache can process at the same time and is the parameter that has the greatest impact on Apache performance. The default value of 150 is far from enough. If the total number of requests has reached this value (you can confirm it through PS-Ef | grep HTTP | WC-l), the subsequent requests will be queued, until a processed request is completed. This is the main reason why there are still a lot of system resources and HTTP access is slow. In theory, the larger the value, the more requests can be processed, but the default limit of Apache cannot be greater than 256.)

 

# Worker mpm

 
<IfmoduleMpm_worker_module>Startservers 3 minsparethreads 75 maxsparethreads 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

A worker generates a "startservers" sub-process by the main control process. Each sub-process contains a fixed number of threadsperchild threads, and each thread processes Requests independently. Similarly, minsparethreads and maxsparethreads set the minimum and maximum number of Idle threads in order not to generate a thread when the request arrives;

Maxrequestworkers sets the maximum number of clients simultaneously connected. If the total number of threads in the existing sub-process cannot meet the load, the control process will derive a new sub-process

The maximum default values of minsparethreads and maxsparethreads are 75 and 250, respectively. These two parameters have little impact on Apache performance and can be adjusted as needed.

Threadsperchild is the most performance-related command in worker MPM. The maximum default value of threadsperchild is 64. If the load is large, 64 is not enough. The threadlimit command must be explicitly used. The maximum default value is 20000.

In worker mode, the total number of requests that can be processed simultaneously is determined by the total number of sub-processes multiplied by the value of threadsperchild. The value must be greater than or equal to maxrequestworkers. If the load is large and the number of existing sub-processes cannot meet the requirements, the control process will derive a new sub-process. By default, the maximum number of sub-processes is 16. When you increase the number, you must explicitly declare serverlimit (the maximum value is 20000 ). Note that if serverlimit is explicitly declared, the value multiplied by threadsperchild must be greater than or equal to maxrequestworkers, and maxrequestworkers must be an integer multiple of threadsperchild, otherwise, Apache automatically adjusts to a corresponding value.

 

# Event mpm

 
<IfmoduleMpm_event_module>Startservers 3 minsparethreads 75 maxsparethreads 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

 

 

 

 
This article content, will be updated to the new blog: http://tt-topia.rhcloud.com/reprint Please note:

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.