Multi-Processing Modules (MPMs)

botond published 2019/10/22, k - 19:40 time

Content

 

Overview

Az Apache HTTP server is designed to be a flexible and efficient web server that can run on a wide variety of platforms in a variety of environments. Different platforms and environments often require different features or require the same service to be implemented in different ways for the most efficient operation. Thanks to its modular structure, webmasters have the option to choose which services to run on the server and whether to load them at compile time or at run time.

Apache version 2.0 extends this modular architecture to the most basic functions of a web server. The server includes various modules that can handle incoming requests more efficiently (Multi-Processing Modules, MPM) by starting multiple child processes. These modules are responsible for connecting to the computer's network ports, receiving requests, and initiating child processes, which then handle incoming requests.

At the user level, MPMs appear like other Apache modules. The main difference is that one and only one MPM of the MPM modules at a time must always be loaded on the server.

 

 

Advantages

Extending the modular architecture to this level of the server has two important advantages:

  • Apache HTTPd supports a wide range of operating systems much more clearly and efficiently. The server version of Windows in particular has become more efficient because it mpm_winnt you can use native network functions used in the previous Apache HTTPd 1.3 POSIX instead of layer. This advantage also extends to other operating systems that use special MPMs.
  • The server can be better customized to the needs of the websites running on it. For example, sites that require high scalability can use multi-threaded MPMs, such as Worker, or that Events, while sites that require greater compatibility with legacy software are better suited to older ones prefork MPM.

Known MPMs

Apache distinguishes the following MPMs by operating system:

  • Netware: mpm_netware
  • OS / 2: mpmt_os2
  • Unix: mpm_prefork, mpm_worker, mpm_event
  • Windows: mpm_winnt

Unix refers to Unix-like operating systems such as Linux, BSD, Solaris, Mac OS X, etc.

For Unix-like systems, the decision to select the appropriate MPM is based on two issues:

  1. Does the system support threads?
  2. Does the system support thread - safe requests (specifically KQUEUE and that epoll features)?

If both questions are answered "yes" then the appropriate MPM is Events. If the answer to question 1 is yes, but the answer to the second is no, then the appropriate MPM is Worker. If the answer to both questions is "no", then the appropriate MPM is prefork. And in practice, all this means is that the right MPM is almost always Events since all modern operating systems support these two features.

 

Introduction to Linux MPMs

As you know, Apache has been offering various operating systems for various operating systems since the 2.0 version. This chapter describes the Linux versions.

Before Apache 2.4, only the prefork and the Worker MPMs were defined as stable while Events and was still in an experimental state. Then from Apache version 2.4 onwards Events MPM has also been declared stable, making it suitable for sharp use.

Prefork MPM

This multiprocess (or process-based) module implements a non-threaded "pre-fork" web server. Each server process can handle a single incoming request and a parent process handles the size of the server pool. This method is appropriate for web pages that, for compatibility reasons, need to avoid thread management in order to work with non-threaded modules such as mod_php. This is the best MPM to isolate each request, with one request problem not affecting the other.

The Prefork MPM is very self-regulating, so you rarely need to change your configuration settings. The key is that the MaxRequestWorkers should be large enough to satisfy as many simultaneous requests as the server is expected to receive, but small enough to accommodate all server processes in physical memory.

Apache attempts to maintain multiple spare servers or idle servers ready to handle incoming requests. This way, clients do not have to wait for another child process to occur before requests are served. That's why this MPM is called pre-fork.

This is the default MPM used when installing the Apache web server.

Related Apache Modules: mpm_prefork, php (or php7.0 / php7.2 / php7.3 depending on PHP installed)

Advantages

  • Prefork is faster if there are no parallel requests on the server.
  • Compatible with older, multi-threaded modules like mod_php.
  • Child processes are preloaded into memory for faster requests.

Disadvantages

  • You can only process one request at a time.
  • Therefore, Prefork is slower when multiple concurrent requests are received by the server (in practice, this may occur regularly on a server with higher traffic).
  • Resource-intensive. You need to allocate the required physical memory for each server process.
  • If a request is CPU-intensive, subsequent requests can easily be overwhelmed
  • It does not support the HTTP / 2 protocol.

 

 

Worker MPM

This multi-process MPM implements a hybrid multi-process and multi-threaded server. When serving requests, using threads is able to fulfill a large number of requests with less system resources than a process-based (Prefork) server. At the same time, it also preserves the stability of process-based servers by maintaining multiple subprocesses, each with many threads.

The most important parameters of this MPM are ThreadsPerChild, which controls the number of threads to be created in the subprocesses, and MaxRequestWorkers, which controls the maximum number of threads that can be started.

Worker MPM has laid the foundation for multi-threaded, multi-process processing on Apache web servers, which has been stable since Apache 2.2.

Related Apache Modules: mpm_worker, proxy_fcgi

Advantages

  • It is faster than Prefork MPM because it can process more than one request at a time. This way the website operated by it can serve more visitors.
  • Compatible with HTTP / 2.

Disadvantages

  • Not compatible with multi-threaded modules like mod_php.

Event MPM

Event MPM is based on the source code of the previous Worker MPM, so it uses the same directives as Worker. Event MPM works almost the same as Worker, except when you need to handle KeepAlive requests. The MPM Event dedicated monitoring thread (Listen thread) in all child processes. This monitoring thread is responsible for routing incoming requests to an available Worker thread. This solves the problem of MPM Worker, which limits entire threads to KeepAliveTimeout waiting. The MPM Event Listener approach ensures that Worker threads do not remain “stuck” until the KeepAliveTimeout expires. This method maintains the maximum amount of Worker threads that can handle as many requests as possible.

Event MPM is only considered a stable version from Apache version 2.4 onwards, was considered experimental in earlier versions, and was not compatible with some modules of older versions of Apache.

Related Apache Modules: mpm_event, proxy_fcgi

Advantages

  • It is faster than Prefork MPM because it can process more than one request at a time. This way the website operated by it can serve more visitors.
  • It is also faster and more efficient than Worker MPM because the process-specific Listen thread allows worker threads not to wait for the KeepAliveTimeout event, so they can receive the next request sooner.
  • Compatible with HTTP / 2.

Disadvantages

  • Not compatible with multi-threaded modules like mod_php.
  • Pre-Apache 2.4 versions were still in experimental mode, so if you are using an older web server, errors may occur (of course, Apache 2.4 can be used everywhere today).

 

 

Switch between MPMs

To switch from the MPM you are currently using to another, you must first know which version of Apache and which MPM the server is running. The easiest way to query this is to use the following command:

apache2ctl -V | grep -i 'version\|mpm'
Server version: Apache/2.4.38 (Debian)
Server MPM:     event

So in this Debian 10 example, the server is running an Apache 2.4.38 with Event MPM.

To switch, you first need to a2dismod command should be used to disable the modules associated with the current MPM (the modules listed above for MPMs). Then it will a2andmod command to enable the required MPM modules. Finally, you need to restart Apache.

An example of switching from MPM Prefork to MPM Event:

a2dismod php
a2dismod mpm_prefork
a2enmod mpm_event
a2enmod proxy_fcgi
systemctl restart apache2

During module switching, the system informs you of various dependency issues that are resolved when the appropriate modules are turned on and off.