If you're still using Apache when the whole world is going to be prone to nginx, you might be able to do everything you can to keep speed. You might tweak the modules to be loaded, using keepalive, fiddling with the Content negotiation module (mod_negotiation: Select a document that best matches the client's requirements from several documents), The followsymlinks directive, which allows soft links to be used in this directory, and the ability to rewrite it, you might even put in more hardware and pretend you didn't. However, if you are running a site that is very busy and don't want to cause the site to crash because of memory, then you should focus on the MPM you are using.
MPM , also known as the Multi-channel processing module (Multi processing module), is related to the HTTP session. From the listening on the network, the request goes to the most important-how to handle the child request, here we discuss the child process and the child thread. For UNIX system machines, Apache offers 3 types of MPM for choice:; Prefork, Worker, and Event. At the same time, there are many other types of MPM available only for other systems Apache, but we are here to focus on the most common, and probably the most you want to see (and what I'm good at). These MPM handle processes and potential threads that the Apache server uses to accept, process, and server HTTP requests.
From a non-strict point of view, a process is an instance of a program. Each process is fully self-contained and is executed completely independently and isolated in terms of address space, variables, memory, and so on. To understand the purpose and content of this article, you can consider the following sentence: 5 Apache processes represent 5 different Apache instances running. While this is quite a warning, it is safe enough to remember the purpose of this article.
In other words, a thread is created and owned by a process. A process can have more than one thread, and these threads are not completely independent. They share the same state and address space that the process assigns to them.
In summary, a process is an instance of a program that is used to tell the entire system that it exists and that it needs resources and that it can execute its own resources; Therefore, threads do not need to publish themselves but are made by the entire application as a process, and threads essentially use less system resources such as memory.
Apache prefork MPM
prefork the multi-processing module is non-threaded. It does not use threads at all and uses the entire process to process each HTTP request. When an HTTP request comes in, assuming it is used to request a picture of your cat, the entire process will go to the request bundle and be responsible for the person's request. If there are other people at the same time browsing your cat's picture, then another completely different process will be used.
prefork very good in terms of speed and stability. Prefork has only a slight edge response time, because it does not need to process different threads in its process, and it is stable in the case of a particular request error, because the entire process is determined only by one request, and other requests are handled by another process, so other requests are not affected.
prefork It also applies if you are using an Apache module that cannot handle threading scenarios. The most common is the mod_php module (although its latest effort is in the direction of Zts). You may encounter this problem with PHP and/or some scripts that cannot be processed by Apache, except for some scripts such as PHP-FPM.
However, if you need to handle a large number of concurrent requests, this approach will "eat" the system resources wildly. Note that each process will publish itself as a full Apache instance. This means that it loads all the modules and is a complete Web server for each request. If the number of requests is very large, and because the coordinator needs to publish the same process as the number of requests to process these requests, this can quickly force memory usage to the limit.
Apache Worker MPM
Work A multiplexed module uses threads, which are handled well in terms of memory usage when high concurrency occurs. In a multiplexed module, fewer processes are required because it does not want Prefork to create a process for each request, the worker pattern creates a thread in the process, and the incoming connection is handled by a different thread in a process. In worker mode, the new connection only needs to wait for the idle thread, rather than waiting for the idle process as it did in Prefork.
Apache Event MPM
Event the pattern is very new. In fact, it was released as a stable version only in the Apache2.4 version. The event mode works the same as worker mode, and it also uses processes and threads. Their biggest difference is that the event mode creates a thread for each request, rather than a thread for an HTTP connection.
There is a situation where you like to use threads but there is an application that uses a longer keepalive time-out period when this pattern works well. In the worker mpm, the thread is bound to the connection and remains occupied regardless of whether the HTTP request is processed.
In event MPM, if the thread that handles the connection is only used to process the current request and is released immediately after the request processing is complete, regardless of the HTTP connection being processed by the parent process. At the same time, the thread can be used to process other requests when the request is disposed of immediately. This means fewer threads are needed!
What do I do with this information?
In that case, which one should you use? In my opinion, with my most loyal advice, everyone should use the worker MPM because it is quite good in concurrency and uses less RAM memory, although if there is a problem with compatibility, you may need to switch back to Prefork.
If you are using the latest version of Apache, you can try using the event MPM.
If PHP is used, you must use PHP-FPM when using the worker MPM or the event mpm. If you use mod_php in worker mode, your app might get an error in a form like this or that. In fact, you might want to use the PHP-FPM, regardless of which MPM mode Apache is in.
1. This article is a translation of the Programmer's architecture
2. This article is translated from vps.net article Apache mpms–prefork, Worker, and Event
3. Reprint Please be sure to indicate this article from : Programmer Architecture (No.:archleaner )
4. More articles please scan the code:
Apache multiplexing Module (MPMs)-Prefork, Worker, Event