An in-depth analysis of several mpm of Apache prefork, worker and event

Source: Internet
Author: User

The Apache 2.X supports the plug-in parallel processing module, called the Multichannel Processing module (MPM). When you compile Apache you must choose one MPM only, and there are several different MPM available for Unix-like systems, which can affect the speed and scalability of Apache.

Prefork MPM: This multi-channel module (MPM) implements a non-threaded, pre-built Web server that works like 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 to require each request to be independent of each other, so that if a request has a problem it does not affect other requests.

This MPM has a strong self-regulation capability and requires minimal configuration instruction tuning. The most important thing is to set the maxclients to a value that is large enough to handle the potential request spikes, but not too large to use more memory than the physical memory.

Worker MPM: This multi-channel module (MPM) enables a network server to support mixed multithreaded multiple processes. Because you use threads to process requests, you can handle massive requests, and system resources are less expensive than MPM. However, it also uses multiple processes, each with multiple threads, to gain MPM stability based on process.

The number of threads that each process can hold is fixed. The server will increase or decrease the number of processes based on the load. A separate control process (the parent process) is responsible for the establishment of the child process. 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 subprocess (the Idle Service thread pool) to meet the incoming request. This allows the client to wait for the child process to occur before the service is received.

Event MPM: Both of these stable MPM methods are somewhat inadequate under very busy server applications. Although the Keepalive method of HTTP reduces the number of TCP connections and network load, Keepalive needs to be bound to a service process or thread, which causes a busy server to consume all threads. The Event MPM is a new model for solving this problem, separating the service process from the connection. The Event MPM method is most effective when the server is processing quickly, with very high CTR rates, the number of threads available is the key resource constraint. A busy server that works in a worker MPM can withstand tens of thousands of hits per second (for example, at the peak of a large news Service site) and the Event MPM can be used to handle higher loads. It is noteworthy that Event MPM cannot work under secure HTTP (HTTPS) access.

For event mode, Apache gives the following warning:

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

The MPM is now in an experimental state and he may not be able to work as expected.

How to configure three kinds of MPM

Prefork is the default MPM on UNIX platforms, and the pre-derivation subprocess is the pattern used in Apache 1.3. The prefork itself is not used to thread, and version 2.0 uses it to maintain compatibility with version 1.3, and on the other hand, perfork with separate subroutines to handle different requests, and the process is independent of each other, making it one of the most stable MPM.

How to view the three kinds of MPM that are currently installed in Apache.

[Root@localhost apache]# Httpd-l
Compiled in Modules:
Core.c PREFORK.C
Http_core.c
Mod_so.c
If you see perfork.c it means that the current is Perfork MPM mode. WORKER.C is represented as the worker MPM mode.

So how do you set up Apache MPM?

You need to specify a pattern when you need to install the Apache configuration:

[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
Specifies that the--with-mpm=name option specifies 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 to configure the event MPM?

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

Note that the event MPM may not be supported for older CPUs.

Parameter analysis of three kinds of MPM

No matter what kind of MPM you're installing 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 5
Minspareservers 5
Maxspareservers 10
Maxrequestworkers 250
Maxconnectionsperchild 0</ifmodule>
# startservers: Number of server processes starting

# Minspareservers: Minimum number of server processes, saving standby

# Maxspareservers: Maximum number of server processes, saving standby

# Maxrequestworkers: Maximum number of server processes allowed to start

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

The Prefork control process creates a process, waits a second, continues to create two, waits a second, continues to create four, after the "startservers" child process is initially established, to meet the needs of minspareservers settings ... This increases the number of processes created, up to 32 per second, until the value of the Minspareservers setting is met. This pattern can reduce system overhead to increase performance by eliminating the need to generate new processes when requests arrive. Maxspareservers sets the maximum number of idle processes, and if the number of idle processes is greater than this value, Apache automatically kill some redundant processes. This value should not be set too large, but if the value set is smaller than Minspareservers, Apache will automatically adjust it to minspareservers+1. If the site load is large, consider increasing both minspareservers and maxspareservers.

Maxrequestsperchild sets the number of requests that each child process can handle. Each subprocess is automatically destroyed after processing the "Maxrequestsperchild" request. 0 means infinite, that is, the child process never destroys. Although the default setting of 0 allows each subprocess to handle more requests, it has two important benefits if set to a value other than 0:

1, can prevent accidental memory leakage. 2. The number of sub processes will be reduced automatically when the server load drops.

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

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

The previous version of Apache2.3.13 Maxrequestworkers was called maxclients.

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

# worker MPM

<ifmodule Mpm_worker_module>startservers 3
Minsparethreads 75
Maxsparethreads 250
Threadsperchild 25
Maxrequestworkers 400
Maxconnectionsperchild 0</ifmodule>
# Startservers: Initial number of server processes started

# Minsparethreads: Minimum number of worker threads, save standby

# Maxsparethreads: Maximum number of worker threads, save standby

# Threadsperchild: Fixed number of worker threads per server process

# Maxrequestworkers: Maximum number of worker threads

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

The Worker generates a "Startservers" subprocess by the main control process, each containing a fixed number of threadsperchild threads, and each thread processes the request independently. Similarly, in order not to generate a thread when the request arrives, the Minsparethreads and maxsparethreads set the minimum and maximum number of idle threads;

Maxrequestworkers sets the maximum total number of clients that are connected at the same time. If the total number of threads in the existing child process does not meet the load, the control process derives the new child process

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

Threadsperchild is the most performance-related instruction in worker mpm. The maximum default value for Threadsperchild is 64, and 64 is not enough if the load is large. 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 the total number of child processes multiplied by the Threadsperchild value and should be greater than or equal to maxrequestworkers. If the load is large and the number of existing child processes is not satisfied, the control process derives the new child process. The default maximum number of child processes is 16, which also requires an explicit declaration of serverlimit (maximum is 20000). It is important to note that if Serverlimit is explicitly declared, then the value multiplied by threadsperchild must 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 3
Minsparethreads 75
Maxsparethreads 250
Threadsperchild 25
Maxrequestworkers 400
Maxconnectionsperchild 0</ifmodule>
# Startservers: Initial number of server processes started

# Minsparethreads: Minimum number of worker threads, save standby

# Maxsparethreads: Maximum number of worker threads, save standby

# Threadsperchild: Fixed number of worker threads per server process

# Maxrequestworkers: Maximum number of worker threads

# Maxconnectionsperchild: One server process service for 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.