Wednesday, September 14, 2011

Apache MPM - Worker and Prefork


Apache is a powerful and flexible webserver, that has a verymodular architecture. Apache version 2 ports even the basic functionality of a web-server, such as binding to network ports on the machine, accepting requests, and dispatching children to handle the requests to a module named Multi-Processing Module (MPM).
Using a modular approach even for this basic operation, makes apache portable to a wider variety of Operating Systems. It also adds to the flexibility in using the Module that is apt for the scenario.
MPM’s are modules that are chosen during configuration, and compiled into the server, and hence can be changed by a re-compilation, as and when the need arises.
Here I would be giving a brief introduction on process and threads. Then briefly cover the popular MPM’s Worker and Prefork.

Threads vs. Processes

A process is usually defined as an instance of a program that is being executed, including all variables and other information describing the program’s state. Each process is executed in it’s separate address space.
Processes have a life cycle. They are spawned, they fork newer child processes and eventually die. Each process is an independent entity to which resources are allocated(say memory).
Threads can be called “units of execution”. A process can be thought of as a thread, plus an address space, file descriptors, and a bunch of other data.
A single process might have multiple threads, and all threads within a process share the same state and same memory space, and can communicate with each other directly, because they share the same variables.

Prefork

The Prefork MPM implements a non-threaded, pre-forking web server. A single process launches child processes which listen for connections and serve them when they arrive. Each time a request arrives, the web-server serves the request with a process that is dedicated for that particular request.
If a spare web-server process is not readily present to process an incoming request, a new child process is spawned. The method imparts a lot of stability because each server runs in its own process. If a process dies/or is killed it will not affect other processes, and hence new requests could still be served.

Worker

The Worker MPM implements a hybrid multi-process multi-threaded server. Here, threads serve requests instead of processes. Hence it is able to serve a large number of requests with fewer system resources than a process-based server.
Remember, threads use very little resource when compared to a process.
Since it is not a fully threaded version, it retains much of the stability of a process-based server by maintaining multiple processes, each comprising of numerous threads(like a tree structure - with processes as branches and threads as the leaves).

Which one to choose

If you want all the available server resource put to the best use and get the best performance out of your server, you could switch to worker from the default prefork.
If you would rate stability more, then you would rather prefer prefork to worker.
The decision actually is not that obvious. It warrants expertise, and in-depth analysis of your exact requirements.
Even though other web-servers are making inroads into Apache’s market share, Apache is still the pick, since it has a lot of brains behind it…
Above all, it has a lot of open eyes.. looking through it, and taking care of it always!