Apache Worker and PHP
TweetApache threaded MPMs
Well, first, what is an MPM? It stands for Multi-Processing Module. It is the process model that Apache uses for its children process. Each request that comes in is handed to a child. Apache 1 used only one model for this, the prefork model. That uses one process per Apache child. The most commonly used threaded MPM is the Worker MPM. In this MPM, you have several processes that run multiple threads within it. This is the one I will be talking about. You can read more on Apache MPMs at the Apache web site.
Huge memory savings
With the Apache prefork or even FastCGI, each apache/php process allocates its own memory. Most healthy sites I have worked on use about 15MB of memory per apache process. Code that has problems will use even more than this. I have seen some use as much as 50MB of RAM. But, lets stick with healthy. So, a server with 1GB of RAM will only realistically be able to run 50 Apache processes or 50 PHP children for FastCGI if each uses 15MB or RAM. That is 750MB total. That leaves just 256MB for the OS and other applications. Now, if you are Yahoo! or someone else with lots of money and lots of equipment, you can just keep adding hardware. But, most of us can't do that.
As I wrote above, the worker MPM apache uses children (processes) and threads. If you configure it to use 10 child processes, each with 10 threads you would have 100 total threads or clients to answer requests. The good news is, because 10 threads are in one process, they can reuse memory that is allocated by other threads in the same process. At dealnews, our application servers use 25 threads per child. In our experience, each child process uses about 35MB of RAM. So, that works out to about 1.4MB per thread. That is 10% the usage for a prefork server per client.
Some say that you will run out of CPU way before RAM. That was not what we experienced before switching to worker. Machines with 2GB of RAM were running out of memory before we hit CPU as a bottleneck due to having just 100 Apache clients running. Now, with worker, I am happy to say that we don't have that problem.
Building PHP for best success with Worker
This is an important part. You can't use radical extensions in PHP when you are using worker. I don't have a list of extensions that will and won't work. We stick with the ones we need to do our core job. Mainly, most pages use the mysql and memcached extension. I would not do any fancy stuff in a worker based server. Keep a prefork server around for that. Or better yet, do funky memory sucking stuff in a cron job and push that data somewhere your web servers can get to it.
Other benefits like static content
Another big issue you hear about with Apache and PHP is running some other server for serving static content to save resources. Worker allows you to do this without running two servers. Having a prefork Apache/PHP process that has 15MB of RAM allocated serve a 10k jpeg image or some CSS file is a waste of resources. With worker, like I wrote above, the memory savings negate this issue. And, from my benchmarks (someone prove me wrong) Apache 2 can keep up with the lighttpds and litespeeds of the world in terms of requests per second for this type of content. This was actually the first place we used the worker mpm. It may still be a good idea to have dedicated apache daemons running just for that content if you have lots of requests for it. That will keep your static content requests from over running your dynamic content requests.
Some issues we have seen
Ok, it is not without problems (but, neither was prefork). There are some unknown (meaning undiagnosed by us) things that will occasionally cause CPU spikes on the servers running worker. For example, we took two memcached nodes offline and the servers that were connected to them spiked their CPU. We restarted Apache and all was fine. It was odd. We had another issue where a bug in my PHP code that was calling fsockopen() without a valid host name and a long timeout would cause a CPU spike and would not seem to let go. So, it does seem that bad PHP code makes the server more sensitive. So, your mileage may vary.
As with any new technology, you need to test a lot before you jump in with both feet. Anyone else have experience with worker and want to share?
One last tip
We have adopted a technique that Rasmus Lerdorf had mentioned. We decide how many MaxClients a server can run and we configure that number to always run. We set the min and max settings of the Apache configuration the same. Of course, we are running service specific servers. If you only have one or two servers and they run Apache and MySQL and mail and dns and... etc. you probably don't want to do that. But, then again, you need to make sure MaxClients will not kill your RAM/CPU as well. I see lots of servers that if MaxClients was actually reached, they would be using 20GB of RAM. And, these servers only have 2GB of RAM. So, check those settings. If you can, configure it to start up more (all if you can) Apache process rather than a few and make sure you won't blow out your RAM.
PhYrE Says:
How very misguided.
> Most healthy sites I have
> worked on use about 15MB of
> memory per apache process.
Most UNIX variants, and actually most operating systems these days, don't actually use 15MB of memory (as you say) even thought they say that they do. The applications themselves are shared across the operating system. The actual program is loaded only once into memory, and then mapped into each process space. Should a program change the code in memory, it creates a copy of the program and then goes from there. Not much should change the program code itself. So apache and PHP are loaded only once.
The variable data that is process specific is not significantly larger than in the case of a threaded API. The rest is all mapped by the operating system without using any more memory.
> Savings
The main savings is reducing the connection pool to databases, and using a lighter weight context switch (that has to swap out less) in switching threads instead of switching programs.
That said, good use of resources in a program fixes the first. A modern operating system on modern processors is actually very fast in its context switches. With multi-core systems and processors that are built with large-scale multitasking in mind, as wella s efficient operating system scheduling, you'll find this is less of an issue.
> Pitfalls
When something fails, your whole program goes down. I'd rather a process crash and be done with it. It makes more sense from an isolation perspective and protecting a large-scale site.
> Threading libraries
Personally, I trut the operating system's scheduler a little more than anything built into a library or program. The OS by design needs to be robust and a nice balance of performance versus safety. Threading modules just emulate a scheduler in software, but without the advanced knowledge of how the kernel is performing and what it can schedule around or with.
> FastCGI
The end effect is minimal to a mod_php system. PHP is pre-launched by fastCGI, but the context switches needed to pipe back and forth between Apache and PHP takes much more time than just sending it straight out to the client from the Apache process. On the other hand, it's not loaded when sending an image using sendfile, which can reduce memory usage. So yes- if you have a mix of PHP and static files, FastCGI can improve memory consumption by removing PHP. If your traffic isn't consistent, or if you have a lot of PHP, then you risk having lots of PHP processes, for which you might as well have just kept PHP in Apache for simplicity.
-M