Question
- What are the differences between MPM Event and MPM Prefork?
- When should I use MPM Event and when MPM Prefork?
Answer
An Apache Multi-Processing Module (MPM) provides a modular approach to web server functions.
The MPM that you select determines how Apache binds to network ports, accepts HTTP requests, and dispatches child processes to handle the HTTP requests.Both of these Apache MPMs (Multi-Processing Modules) have their own benefits and drawbacks and must be used in consideration with the requirements of all websites on the server.
A short explanation for each one is available below:
Prefork MPM
When the Prefork module is used, Apache is a non-threaded, pre-forking web server. That means that each Apache child process contains a single thread and handles one request at a time.
Because of that, it consumes slightly more resources than the threaded Event MPM, but it also provides better stability. MPM Prefork takes advantage of a parent process to make an effort to keep a number of extra or idle processes open to accommodate fresh requests. Prefork is the default MPM.
It still is the best choice if Apache has to use non-thread safe libraries such as mod_php (DSO), and is ideal if isolation of processes is important. It’s crucial to strike the correct balance between the maximum number of processes and the available RAM on a pre-forking server, because it consumes a lot of RAM. Prefork must always be used whenever something that isn’t thread-safe needs to run.
Most versions of PHP are still not thread safe, and the only MPM that's safe to use with non-thread-safe code is MPM Prefork, since it creates new processes, instead of threads, to handle new requests.
Event MPM
Event MPM is introduced in Apache 2.4, It is pretty similar to the old Worker MPM, but it designed for managing high loads.
Each process under Event MPM can contain multiple threads, but unlike Prefork, each is capable of more than one task.
This mode is supported only on servers running Apache 2.4. It allows more requests to be served simultaneously by passing off some processing work to supporting threads.
While using the Event MPM mode, Apache tries to fix the "keep alive problem" faced by other MPM modes.
When a client completes the first request, then the client can keep the connection open, and send further requests using the same socket, which reduces connection overload, however it can also cause processes to hang for long periods of time in certain situations where code is not written optimally.
If Apache processes remain active for too long, that may make the Apache server itself exhaust the server resources and stop working as a result of that.