Apache Multi-process Processing module (MPM) principle detailed

Source: Internet
Author: User
Tags memory usage

For the latest version of the Web server Apache (version is Apache 2.4.10, published on July 21, 2014), there are three stable mpm (multi-processing module, multi-process processing modules) mode. They are prefork,worker and event, and they also represent the evolution and development of Apache.

To view our Apache model, you can use the HTTPD-V command to view:

Shell

1

2

3

4

5

6

7

8

9

10

# httpd-v

Server version:apache/2.4.10 (Unix)

Server Built:dec 29 2014 11:23:13

Server ' s Module Magic number:20120211:36

Server loaded:apr 1.5.1, Apr-util 1.5.3

Compiled using:apr 1.5.1, Apr-util 1.5.3

Architecture:64-bit

Server mpm:event

Threaded:yes (fixed thread count)

Forked:yes (variable process count)

When compiling, you can specify by configure parameters:

Shell

1

--with-mpm=prefork|worker|event

can also be compiled to three kinds of support, by modifying the configuration to replace

Shell

1

--enable-mpms-shared=all

Modifying Apache's multi-processing mode in httpd.conf MPM (three MPM can be automatically compiled under Modules folder):

Shell

1

2

3

#LoadModule Mpm_prefork_module modules/mod_mpm_prefork.so

LoadModule Mpm_worker_module modules/mod_mpm_worker.so

#LoadModule Mpm_event_module modules/mod_mpm_event.so

1. Prefork MPM

The Prefork model can be considered an ancient but very stable Apache model. At the beginning of the launch, Apache Fork Some of the child processes and waits for the request to come in. This is done to reduce the overhead of frequent creation and destruction processes. Each child process has only one thread, and only one request can be processed at a point in time.
Advantages: Mature and stable, compatible with all new and old modules. Also, there is no need to worry about thread-safety issues. (We do not need to support thread-safe expansion of our common mod_php,php)
Disadvantage: A process consumes more system resources, consuming more memory. Moreover, it is not good at handling high concurrent requests, in which case it puts the request into the queue and waits until there is a process available for the request to be processed.


How to configure Apache in httpd.conf:

Shell

1

2

3

4

5

6

7

<ifmodule mpm_prefork_module>

Startservers 5

Minspareservers 5

Maxspareservers 10

Maxrequestworkers 250

Maxconnectionsperchild 0

</IfModule>

2. Worker MPM

The worker mode uses a multiple-process and multithreaded blending mode compared to the previous one. It also fork several subprocess (the number is less), and then each subprocess creates some threads, including a listener thread. Each request comes over and is assigned to 1 threads service. Threads are lighter than processes because threads typically share the memory space of the parent process, so memory usage can be reduced. In a highly concurrent scenario, the performance would be better because there are more threads available than the prefork.
Some people will find it strange, so why not use multithreading here, and introduce multiple processes.
The main reason is the need to consider stability, and if a thread hangs abnormally, it can cause the parent process to hang with other normal child threads (both of which are under the same process). To prevent this anomaly from appearing, you can't use threads all at once, use multiple processes to add more threads, and if a thread is abnormal, it's only part of Apache that is affected, not the entire service.

Advantages: Occupy less memory and perform better under high concurrency.

Disadvantage: Thread-safe issues must be considered because multiple child threads are the memory addresses that share the parent process. If you use keep-alive long connection, a thread will always be occupied, perhaps almost no request in the middle, need to wait until the timeout will be released. If too many threads are occupied this way, it can also result in the availability of service-free threads in high concurrency scenarios. (This problem will also occur in prefork mode.)

Note: The long connection of the keep-alive is to allow the next socket communication to reuse the connection created before, thereby reducing the overhead of the connection creation and destruction. Keeping a connection allows a process or thread to remain in the waiting state, even if there is no data coming.


How to configure Apache in httpd.conf:

1

2

3

4

5

6

7

8

&lt;ifmodule mpm_worker_module&gt;

Startservers 3

Minsparethreads 75

Maxsparethreads 250

Threadsperchild 25

Maxrequestworkers 400

Maxconnectionsperchild 0

&lt;/IfModule&gt;

3. Event MPM

This is the newest model in Apache, which is already a stable and usable pattern in the current version. It is similar to the worker pattern, and the biggest difference is that it solves the problem of resource wasting of a long-running thread in a keep-alive scenario (some threads are keep-alive, where the hang is waiting, there is almost no request to come in and even wait for the timeout). In event MPM, there will be a dedicated thread to manage these keep-alive types of threads, and when a real request comes in, pass the request to the service thread and allow it to be released after execution is complete. This enhances the request processing capability in high concurrency scenarios.

Event MPM is invalidated when it encounters some incompatible modules and will fall back to worker mode, a worker thread processing a request. The official self-contained modules are all supported by event MPM.

Note that event MPM requires support from the Linux system (Linux 2.6+) for Epoll to enable it.

Also, you need to add HTTPS connection (SSL), which is still running in a way that is similar to the worker, and the thread will be occupied until the connection is closed. Part of the older data, said that event MPM does not support SSL, that is a few years ago, the argument is now supported.


How to configure Apache in httpd.conf:

Shell

1

2

3

4

5

6

7

8

<ifmodule mpm_event_module>

Startservers 3

Minsparethreads 75

Maxsparethreads 250

Threadsperchild 25

Maxrequestworkers 400

Maxconnectionsperchild 0

</IfModule>

In three different modes, I did a performance test through AB, and there was no big difference in the normal full load scenario.
Test statement:

C

1

./ab-k-C 200-n 200000 192.168.0.11/index.html

Test results:

C

1

2

3

Prefork:9556qps

Worker:11038qps

Event:10224qps

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.