Apache Three modes of operation

Source: Internet
Author: User
Tags epoll

As a veteran server, Apache is still evolving, for now, it has a total of three stable mpm (multi-processing module, multi-process processing module).

They are prefork, worker, and event respectively.

1. Prefork MPM

Keywords: multi-process

Prefork mode can be considered an old but very stable pattern. Apache starts by pre-deriving some child processes from the fork, then waits for the request to come in, and always the view keeps some spare child processes.

This is done to reduce the overhead of frequently creating and destroying processes. There is only one thread in each child process, and only one request can be processed within a single point in time.

In Unix systems, the parent process typically runs as root for 80 ports, and Apache-generated child processes are typically run with a low-privileged user.

User and group directives are used to configure low-privileged users of child processes. The user who runs the child process must have read access to the content he serves, but must have as few permissions as possible for resources other than the content of the service.

Pros: Mature, compatible with all new and old modules. The process is completely independent, making it very stable. 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 this scenario,

It puts the request in the queue and waits until a process is available for the request to be processed.

Related configurations in httpd-mpm.conf:

<ifmodule mpm_prefork_module>#服务器启动时建立的子进程数量 startservers5#空闲子进程的最小数量, default 5, and if the number of currently idle child processes is less than minspareservers, then Apache will produce new child processes.    Do not set this parameter too large. Minspareservers5#空闲子进程的最大数量, default 10; If there are currently more than maxspareservers of idle child processes, the parent process kills the extra child processes. This parameter also does not need to be set too large, if you set it smaller than Minspareservers, Apache will automatically modify it to Minspareservers+1. MaxspareserversTen#限定服务器同一时间内客户端最大接入的请求数量, the default is 150; Any requests that exceed this limit are queued and once a connection is released, the request in the queue will be serviced. MaxClients Max#每个子进程在其生命周期内允许最大的请求数量, if the total number of requests has reached this value, the child process will end, and if set to 0, the child process will never end.    If this value is set to a value other than 0, you can prevent memory leaks caused by running PHP. Maxrequestsperchild0</IfModule>

The number of processes created, up to 32 per second, until the value of the Minspareservers setting is met.

This is the origin of pre-derivation (prefork). This pattern eliminates the need to generate new processes when the request arrives, reducing overhead to increase performance.

When the number of concurrent requests reaches maxclients (such as 256), only 10 of the idle processes are available. Apache continues to increase the creation process. Until the number of processes reaches 256.

When the concurrency spikes are over, and the number of concurrent requests is probably only one, Apache gradually deletes the process until the number of processes reaches maxspareservers.

2, the Worker MPM

Keywords: multi-process + multithreading

Worker mode is a multi-process + multithreaded mode, compared to the previous one.

It also pre-fork several sub-processes (a smaller number), each of which can generate some service threads and a listener thread that listens to the access request and passes it to the service thread for processing and answering.

Apache always tries to maintain a standby (spare) or free service thread pool. This way, the client does not have to wait for new threads or new processes to be established to be processed.

In Unix, in order to be able to bind port 80, the parent process is typically started as root, and then Apache creates child processes and threads with lower-privileged users.

The user and group directives are used to configure permissions for the Apache child process. Although the child process must have read access to the content it provides, it should give him less privileges as much as possible.

Also, unless suEXEC is used, the permissions configured by these directives will be inherited by the CGI script.

Threads are lighter than processes because threads typically share the memory space of the parent process, so memory consumption decreases, and in high concurrency scenarios, it behaves better than the prefork pattern.

Some people may find it strange, so why not use multithreading here directly (i.e. to implement multiple processes within a process) and to introduce multiple processes?

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 multi-process + multithreaded mode, each process is independent, and if a thread has an exception, the only part of the service that is affected is Apache, not the entire service. Other processes can still work.

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 Keep-alive long connection method,

Perhaps there is almost no request in the middle, then the blocking occurs, the thread is suspended and needs to wait until the timeout is 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.)

The long connection of the ps:http1.1 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.

<ifmodule mpm_worker_module>    #服务器启动时建立的子进程数量    startservers          2    # The number of requests that limit the maximum client access to the server at the same time, the default is 150, and any requests that exceed the limit are queued, and once a connection is freed, the request in the queue will be serviced.    maxclients            Max     #空闲子进程的最小数量    minsparethreads          #空闲子进程的最大数量    Maxsparethreads           #每个子进程产生的线程数量    threadsperchild           #每个子进程在其生命周期内允许最大的请求数量, if the total number of requests has reached this value, the child process will end, and if set to 0, the child process will never end. Setting this value to a value other than 0 prevents the memory leaks that are caused by running PHP.    Maxrequestsperchild    0</IfModule>

Understanding configuration: The master control process generates "Startservers" sub-processes, each of which contains a fixed number of threadsperchild threads, and each thread processes requests independently.

Similarly, in order to avoid generating threads when requests arrive, Minsparethreads and maxsparethreads set the minimum and maximum number of idle threads;

Instead, MaxClients sets the total number of threads in all child processes. 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.

3. The Event MPM

Keywords: multi-process + multi-threaded +epoll

This is the newest pattern in Apache, which is already stable and usable in the current version.

It is much like the worker pattern, the biggest difference being that it solves the problem of resource wasting of long-occupied threads under the keep-alive scenario

(Some threads are keep-alive, hanging there waiting, there is almost no request in the middle, wait until the timeout).

In the event mpm, there will be a dedicated thread to manage these keep-alive types of threads, and when a real request comes in, the request is passed to the service thread,

Once execution is complete, it is allowed to be released. In this way, one thread can handle several requests and implement asynchronous non-blocking.

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.

<ifmodule mpm_worker_module>    #服务器启动时建立的子进程数量    startservers             3    # Minimum number of idle child processes    minsparethreads             #空闲子进程的最小数量    maxsparethreads          -     #每个子进程产生的线程数量    threadsperchild             #限定服务器同一时间内客户端最大接入的请求数量, The default is 150, and any requests that exceed this limit are queued, and once a connection is released, the request in the queue will be serviced.    maxrequestworkers        -     #每个子进程在其生命周期内允许最大的请求数量, if the total number of requests has reached this value, the child process will end, and if set to 0, the child process will never end. Setting this value to a value other than 0 prevents the memory leaks that are caused by running PHP.    Maxrequestsperchild    0</IfModule>

Apache Three modes of operation

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.