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

MPM Worker, Prefork और Event

Apache का Multi-Processing Module (MPM) decide करता है कि यह एक साथ कई requests को *कैसे* handle करे -- अलग processes, threads, या एक event-driven model से -- और सही एक चुनना performance और memory use पर बड़ा असर डालता है।

एक MPM क्या करता है

Apache जो हर request handle करता है उसे चलने के लिए कोई execution unit -- एक process या एक thread -- चाहिए होता है। MPM एक pluggable module है जो decide करता है कि वह unit क्या है और Apache उनमें से कितने ready रखता है। किसी भी समय exactly एक MPM active होता है, startup पर चुना गया।

Prefork

prefork हर request को एक अलग, single-threaded process से handle करता है। यह सबसे पुराना और सबसे simple model है, और इसका बड़ा advantage compatibility है -- non-thread-safe modules (classically Apache में सीधे compiled mod_php) को इसकी ज़रूरत होती है। cost memory की है: हर process Apache का पूरा वज़न carry करती है, इसलिए यह RAM की per-unit बाकी alternatives से कम simultaneous connections तक scale करता है।

Worker

worker कई processes इस्तेमाल करता है, हर एक कई threads चलाती है, ताकि ज़्यादा requests कम processes की memory share करें -- high concurrency में prefork से कहीं halka। requirement यह है कि इस्तेमाल में हर module thread-safe हो, क्योंकि अब कई requests same process के अंदर simultaneously चल सकती हैं।

Event

event, worker का एक refinement है और ज़्यादातर Linux distributions पर modern default है। यह active request handle करने वाले thread को सिर्फ idle रखे गए connections से अलग करता है, हर idle connection के लिए एक पूरा worker thread बांधने की बजाय कई keep-alive connections manage करने के लिए dedicated threads की एक छोटी संख्या इस्तेमाल करते हुए।

Active MPM चुनना और Check करना

apachectl -V दिखाता है कि किसी given Apache build पर कौन सा MPM चल रहा है। Ubuntu पर, MPMs switch करना बस एक disable करके दूसरा enable करने जितना simple है a2dismod/a2enmod से (जैसे sudo a2dismod mpm_prefork फिर sudo a2enmod mpm_event), उसके बाद एक restart। practice में, event इस्तेमाल करें जब तक कि कोई specific चीज़ -- आमतौर पर एक पुराना, non-thread-safe module -- आपको prefork पर force न करे।

Warning: MPM switch करने के लिए सिर्फ reload नहीं, पूरा restart चाहिए, और इस्तेमाल में कोई भी non-thread-safe module (classic mod_php textbook example है) worker/event के under टूट जाएगा -- पहले module compatibility check करें।

उदाहरण: 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. #}
आम गलतियां
  1. बिना किसी असली वजह के default से prefork पर रहना, worker/event की memory-per-concurrency में कहीं बेहतर performance miss करते हुए।
  2. worker/event पर switch करना जबकि अभी भी किसी module पर depend कर रहे हों जो thread-safe नहीं है, load में crashes या corrupted responses cause करते हुए।
  3. MPM बदलने के बाद Apache को पूरी तरह restart करने की बजाय reload करना -- MPM process startup पर चुना जाता है और सिर्फ एक reload change apply नहीं करेगा।
🔒

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.