← Back to Apache Course | Chapter 5: Performance | Lesson 2 of 4

MPM Worker/Prefork/Event

Apache's Multi-Processing Module (MPM) decides *how* it handles many requests at once -- with separate processes, threads, or an event-driven model -- and choosing the right one has a big effect on performance and memory use.

What an MPM Does

Every request Apache handles needs some unit of execution -- a process or a thread -- to run in. The MPM is a pluggable module that decides what that unit is and how many of them Apache keeps ready. Exactly one MPM is active at a time, chosen at startup.

Prefork

prefork handles each request with a separate, single-threaded process. It's the oldest and simplest model, and its big advantage is compatibility -- non-thread-safe modules (classically mod_php compiled directly into Apache) need it. The cost is memory: every process carries the full weight of Apache, so it scales to fewer simultaneous connections per unit of RAM than the alternatives.

Worker

worker uses multiple processes, each running multiple threads, so many more requests share the memory of fewer processes -- much lighter than prefork under high concurrency. The requirement is that every module in use must be thread-safe, since multiple requests can now run inside the same process simultaneously.

Event

event is a refinement of worker and is the modern default on most Linux distributions. It separates the thread handling an active request from the connections that are just idly kept alive, using a small number of dedicated threads to manage many keep-alive connections at once instead of tying up a full worker thread per idle connection.

Choosing and Checking the Active MPM

apachectl -V shows which MPM a given Apache build is running with. On Ubuntu, switching MPMs is as simple as disabling one and enabling another with a2dismod/a2enmod (e.g. sudo a2dismod mpm_prefork then sudo a2enmod mpm_event), followed by a restart. In practice, use event unless something specific -- usually an old, non-thread-safe module -- forces you onto prefork.

Warning: Switching MPM requires a full restart, not just a reload, and any non-thread-safe module in use (classic mod_php being the textbook example) will break under worker/event -- check module compatibility first.

Example: Checking and switching the active MPM (Ubuntu)

apacheconf
# Check which MPM is compiled/active
apachectl -V | grep -i mpm

# Switch from prefork to event
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Staying on prefork by default without a real reason, missing out on the much better concurrency-per-memory of worker/event.
  2. Switching to worker/event while still relying on a module that isn't thread-safe, causing crashes or corrupted responses under load.
  3. Reloading instead of fully restarting Apache after changing the MPM -- the MPM is chosen at process startup and a reload alone won't apply the change.
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.