Comparison of three modes of httpd

Source: Internet
Author: User

See which mode your httpd uses:

/usr/local/apache2/bin/httpd-v |grep ' Server MPM '

Which mode to use, you need to specify at compile time

--with-mpm=prefork|worker|event

Of course, you can compile the time, let all three support:

--enable-mpms-shared=all

Then in the configuration file, modify the

LoadModule Mpm_worker_module modules/mpd_mpm_worker.so

Version 2.2 defaults to worker,2.4 version default to Event

Let's compare the differences between the three modes.

1 prefork

Prefork mode can be considered an old but very stable Apache model. When Apache starts, it will fork some sub-processes in advance and wait for the request to come in. This is done to reduce the overhead of frequently creating and destroying processes. each child process has only one thread, and within a single point in time, only one request can be processed.
Advantages: Mature and stable, compatible with all new and old modules. At the same time, there is no need to worry about thread safety. (The expansion of our common mod_php,php does not need to support thread safety)
Disadvantage: A process consumes more of the system resources, consuming more memory . Moreover, it is not good at handling high concurrent requests , in which case it will put the request into the queue and wait until there are available processes before the request is processed.

2 worker

Worker mode is a mixed mode that uses multi-process and multithreading compared to the previous one. It also pre-fork several sub-processes (a smaller number), and then each child process creates some threads, including a listener thread. Each request comes over and is assigned to 1 threads to service. Threads are lighter than processes because threads typically share the memory space of the parent process, so memory consumption is reduced. in high concurrency scenarios, the performance is better because there are more threads available than prefork.
Some people will find it strange, so why not use multithreading here, but also to introduce multi-process?
The main reason is that stability needs to be considered, and if one thread hangs abnormally, it causes the parent process to hang with the other normal child threads ( they are all under the same process). In order to prevent this unusual scenario from appearing, you cannot use all the threads, use multiple processes and add multiple threads, and if a thread has an exception, the only part of the service that is affected is Apache, not the entire service.

Pros: Take up less memory and perform better with high concurrency.

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

Note: The long connection mode of the keep-alive is to allow the next socket communication to be reused before the connection is created, thereby reducing the overhead of connection creation and destruction. Keeping a connection keeps a process or thread waiting, even if no data comes in.

3 Event

This is the newest pattern in Apache, which is already stable and usable in the current version. It is similar to the worker pattern, the biggest difference is that it solves the keep-alive scenario, the long-occupied threads of the resource waste problem ( some threads because of being keep-alive, where the empty hanging wait, the middle of almost no request to come, or even wait until the timeout). In the event mpm, there will be a dedicated thread to manage these keep-alive types of threads, and when there is a real request coming in, pass the request to the service thread, and then allow it to be released when the execution is complete. This enhances the request processing capability in high concurrency scenarios.

Event MPM fails when it encounters some incompatible modules, it will fall back into worker mode, and a worker thread can process a request. The official comes with the module, all support event MPM.

Note that the event MPM requires the support of the Linux system (Linux 2.6+) for Epoll to be enabled.

Also, the need to add is the HTTPS connection (SSL), its operating mode is still similar to the worker way , the thread will be occupied, know the connection is closed. Part of the older information, said that the event MPM does not support SSL, the statement is a few years ago, it is now supported.

Comparison of three modes of httpd

Related Article

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.