Three MPM analysis of Apache prefork, worker and event

Source: Internet
Author: User

the Apache 2.X supports a plug-in parallel processing module called a multi- path processing module (MPM). When compiling Apache, you have to choose only one mpm, and there are several different MPM options for Unix-like systems that can affect the speed and scalability of Apache.

  prefork MPM : 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 MPM : This multi-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 number of threads that each process can have is fixed. The server increases or decreases the number of processes depending on the load. A separate control process ( parent Process ) is responsible for the creation of child processes. Each child process can establish a threadsperchild number of service threads and a listener thread that listens to the access request and passes it to the service thread for processing and answering.

  Whether it's worker mode or prefork mode, Apache always tries to keep some spare (spare) or idle child processes (the free service thread pool) to meet the incoming requests. This way, the client does not need to wait for the child process to be generated before the service is received.

   Event MPM: The above two kinds of stableMPMThe approach is somewhat inadequate under very busy server applications. DespiteHTTPof theKeepaliveWay to reduceTCPnumber of connections and network load, butKeepaliveneeds to be tied to a service process or thread, which causes a busy server to consume all the threads. The Event MPM is a new model for solving this problem, which separates the service process from the connection. When the server is processing fast and has a very high CTR, the number of threads available is a critical resource constraint, at which pointEvent MPMWay is the most effective. One toWorker MPMcan withstand a good tens of thousands of visits per second (for example, at the peak of a large news Service site) andEvent MPMcan be used to handle higher loads. It is important to note thatEvent MPMNot in SecurityHTTP(HTTPS) access to work under.

For the event mode,Apache gives the following warning:

This MPM was experimental, so it could or may not be work as expected.

This MPM is currently under trial, and he may not be able to work as expected.

How to configure three types of MPM

  Prefork  Yes unix default on platform mpm PREFORK  The thread itself is not used, 2.0  The version used it for the 1.3 version to maintain compatibility; On the other hand, perfork MPM

How to view the three types of MPMcurrently installed on Apache .


[Email protected] apache]# httpd-lcompiled in modules:core.c prefork.c http_core.c mod_so.c


If you see perfork.c , the current perfork MPM mode is indicated. The worker.c is represented as the worker MPM mode.

So how do you set up Apache 's MPM ?

You need to specify the mode when the Apache configuration is installed:

[Email protected] httpd-2.4.1]#/configure--prefix=/usr/local/apache2worker--enable-so--with-mpm=worker [email Protected] httpd-2.4.1]# make [[E-mail protected] httpd-2.4.1]# make install

Specify the--with-mpm=name option to specify mpm,which is the name of the MPM you want to use . If you do not specify a pattern, the default is Prefork MPM.

So how do I configure it as an event MPM?

As with the method above, just add the following parameters to the installation:--enable-nonportable-atomics=yes

It is important to note that the event MPM may not be supported for older CPUs .

Analysis of three MPM parameters

No matter what kind of MPM you're installing in Apache ,

After the installation is complete, open the. /apache/conf/extra/httpd-mpm.conf file and locate the following configuration:

# perfork MPM


<ifmodule mpm_prefork_module>startservers 5MinSpareServers 5MaxSpareServers 10MaxRequestWorkers 250MaxConnectionsPerChild 0</ifmodule>


# startservers: Number of server processes started

# Minspareservers: Minimum number of server processes, saving alternate

# Maxspareservers: Maximum number of server processes, saving alternate

# Maxrequestworkers: Maximum number of server processes allowed to start

# Maxconnectionsperchild: One server process service with maximum number of connections

Prefork control process After initially establishing the "startservers" sub-process, in order to meet the needs of minspareservers settings to create a process, wait a second, continue to create two, wait a second, continue to create four ... This increases the number of processes created by the number of digits, up to 32 per second, until the value of the Minspareservers setting is met. This pattern eliminates the need to generate new processes when the request arrives, reducing overhead to increase performance. Maxspareservers sets the maximum number of idle processes, and if the number of idle processes is greater than this value, Apache will automatically kill some of the unwanted processes. This value should not be set too large, but if you set a value 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 can be processed by each child process. Each child process is automatically destroyed after it has processed "maxrequestsperchild" requests. 0 means infinity, that is, the child process never destroys. Although the default setting of 0 enables each subprocess to process more requests, there are two important benefits if set to a value other than 0:

1. To prevent accidental memory leaks. 2. When the server load drops, the number of child processes will be automatically reduced.

Therefore, this value can be adjusted according to the load of the server.

The maxrequestworkers instruction set limits the number of service requests at the same time. Any connection attempts at Maxrequestworkerslimit will normally be queued, up to several based on the listenbacklog directive.

prior to apache2.3.13 version maxrequestworkers was called maxclients .

(MaxClients is the most important of these directives, setting the request that Apache can process at the same time, which is the most influential parameter to Apache performance.) Its default value of 150 is far from enough, if the total number of requests has reached this value (can be confirmed by Ps-ef|grep Http|wc-l), then the subsequent request will be queued until a processed request is complete. This is the main reason why the system resources are still a lot left and HTTP access is slow. Although theoretically the larger the value, the more requests can be processed, but the Apache default limit cannot be greater than 256. )

# worker MPM


<ifmodule mpm_worker_module>startservers 3MinSpareThreads 75MaxSpareThreads threadsperchild 25MaxRequestWorkers 400MaxConnectionsPerChild 0</ifmodule>


# Startservers: Initial number of server processes started

# Minsparethreads: Minimum number of worker threads, saving alternate

# Maxsparethreads: Maximum number of worker threads, saving alternate

# Threadsperchild: Fixed number of worker threads in each server process

# Maxrequestworkers: Maximum number of worker threads

# Maxconnectionsperchild: One server process service with maximum number of connections

The Worker generates a "startservers" sub-process by the master control process, each of which contains a fixed number of threadsperchild threads, and each thread processes the request independently. Similarly, minsparethreads and maxsparethreads set the minimum and maximum number of idle threads in order not to generate threads when the request arrives;

The maxrequestworkers sets the maximum number of clients that are simultaneously connected. If the total number of threads in an existing child process does not meet the load, the control process will derive the new child process

The maximum default values for Minsparethreads and Maxsparethreads are 75 and 250, respectively. These two parameters have little effect on Apache performance, and can be adjusted according to the actual situation.

Threadsperchild is the most performance-related instruction in the worker mpm. The maximum default value for Threadsperchild is 64, and 64 is not enough if the load is large. At this point, to explicitly use the THREADLIMIT directive, its maximum default value is 20000.

The total number of requests that can be processed concurrently in worker mode is determined by multiplying the total number of child processes by the Threadsperchild value, which should be greater than or equal to maxrequestworkers. If the load is large and the number of existing child processes is not met, the control process derives the new child process. The default maximum number of child processes is 16, and you need to explicitly declare serverlimit (the maximum value is 20000) when you increase it. It is important to note that if Serverlimit is explicitly declared, then it must be multiplied by the value of threadsperchild to be greater than or equal to maxrequestworkers. And maxrequestworkers must be an integer multiple of threadsperchild, otherwise Apache will automatically adjust to a corresponding value.

# event MPM


<ifmodule mpm_event_module>startservers 3MinSpareThreads 75MaxSpareThreads 250ThreadsPerChild 25MaxRequestWorkers 400MaxConnectionsPerChild 0</ifmodule>

# Startservers: Initial number of server processes started

# Minsparethreads: Minimum number of worker threads, saving alternate

# Maxsparethreads: Maximum number of worker threads, saving alternate

# Threadsperchild: Fixed number of worker threads in each server process

# Maxrequestworkers: Maximum number of worker threads

# Maxconnectionsperchild: One server process service with maximum number of connections


This article from "愺 Burgundy pounding his 豩" blog, declined reprint!

Apache prefork, worker, and event three MPM analysis

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.