Modular structure of apache is one of the reason for its global dominance among webservers.We have the flexibility to adding or droping modules.Before we deal with apache prefork MPM we need to have a basic idea about apache Multi Processing Modules,Multi processing Modules (MPM) are the multiple request (hundreds to kilos) handling ‘department’ of apache.We can compare MPM as ‘valve’ of apache , it accept requests, spawn the child processes according to the inflow of requests and assign the child process for the incoming connections.
Worker and Prefork are two major MPMs used in Linux platform.Both have their own pros and cons. Prefork is a non-threaded MPM while Worker is a threaded MPM. i.e prefork will generate pre-defined number of httpd procesesses(StartServers parameter) during startup and dispatch more child processes as the request increases. But in the case threaded worker MPM it will startup pre-defined number of httpd processes and a single process will hadle multiple connections.
Since prefork have a one process per thread structure it can provide isolation among different connections. So we can say that it is more secure at the cost of more RAM and CPU power. Worker MPM needs less RAM and CPU power but does not provide same level of request-to-request isolation as a process-based MPM does.Now let us look in to the prefork MPM tuning which is more common MPM than worker.
Prefork Directives
Example for prefork directives
StartServers 8
MinSpareServers 5
MaxSpareServers 10
MaxClients 30
MaxRequestsPerChild 500
StartServers : Number of apache child processes generated during the apache startup.
MinSpareServers: Spare servers are child processes in idle(not handling any requests). If the total idle processes are less than
MinSpareServers directive then apache parent process will create child processes up to MinSpareServers .
MaxSpareServers: If the total idle child processes is more than this directive , then parent pocess will kill additional idle processes.
MaxClients: This is the most important MPM directive for tuning apache performance.Parent process will dynamically spawn new child processes as the incoming request raises. MaxClients is the maximum child processes created for handling requests or in other words it is the max simultaneous requests apache can handle. This value should be optimal. If it is too low apache can only handle few requests and excess requests will be queued and it may get timed out.If it is too high server will begin to swap and it seriously affects entire server performance.
MaxRequestsPerChild: After handling this much requests child process will die .MaxRequestsPerChild is the requests that a child process will handle in its life time.If it is too low in a busy server, apache will utilize a good CPU power for killing and spawning new child processes which may cause more CPU burden. But at the same time it is always good to recycle it in a timely manner, otherwise it may cause common issues like memory leak , process bloat etc.
How to set MaxClients.
Before choosing a value we need to analyze the server , i.e check the RAM size and applications running other than apache like mysql , java applications etc. Then roughly calculate the memory dedicated for those applications . For example ,we might have defined innodb buffer spool size for mysql and java heap size for java applications . Substract that much memory from total RAM size. Suppose you have 2 GB RAM and 500 MB allocated for mysql(it depends on your DB size) and 250 for java and reserved some memory for system applications ,in this setup we can reserve memory up to 1 GB for apache . On next step we need to calculate avereage process memory usage for apache. For calculating it , during the peak traffic time run the following command
ps aux| awk ‘/apach[e]/{total+=$6}END{print total}’ this calculates total physical memory used by all apache process , to calculate average memory usage ,devide above value with total apache processes .
Finally we can optimize MaxClients value by deviding memory reserved for apache (1G) by process’s average memory usage. (Lets imagine its 35M here) .
MaxClients <= 1G/35 M = 30. We can reduce avg memory usage by removing unwanted apache modules and using php cache like PHP op-code cache/accelerator .If serever have good traffic, then fix the MaxRequestsPerChild deirective some higher values like 2000. Thoroughly observe server for some days after tuning , some time you need to tweak it 2 – 3 times for getting the best result. Good Luck!
Recent Comments