Bring down a poorly deployed WordPress (and how to stop it from happening)
jalada.co.uk
jalada.co.uk
--------- Apache processes ----------
PID PPID VMSize Private Name
-------------------------------------
480 21168 17.6 MB 0.5 MB /usr/sbin/apache2 -k start
1216 21168 17.6 MB 0.6 MB /usr/sbin/apache2 -k start
5252 21168 17.6 MB 0.6 MB /usr/sbin/apache2 -k start
5340 21168 17.6 MB 0.6 MB /usr/sbin/apache2 -k start
5992 21168 17.6 MB 0.5 MB /usr/sbin/apache2 -k start
[REMOVED TO SAVE SPACE]
17163 21168 17.6 MB 0.5 MB /usr/sbin/apache2 -k start
21168 1 17.3 MB 0.4 MB /usr/sbin/apache2 -k start
### Processes: 89
### Total private dirty RSS: 42.10 MB
-------- Nginx processes --------
### Processes: 0
### Total private dirty RSS: 0.00 MB
---- Passenger processes -----
PID VMSize Private Name
------------------------------
5997 55.2 MB 29.6 MB Rack: /var/www/redacted/current
6006 60.8 MB 34.5 MB Rack: /var/www/redacted/current
14262 63.4 MB 50.1 MB Rack: /var/www/redacted/current
20205 63.1 MB 49.7 MB Rack: /var/www/redacted/current
25250 5.1 MB 0.2 MB PassengerWatchdog
25253 14.1 MB 0.6 MB PassengerHelperAgent
25255 10.6 MB 4.8 MB Passenger spawn server
25259 9.0 MB 0.5 MB PassengerLoggingAgent
25412 63.2 MB 49.9 MB Rack: /var/www/redacted/current
32492 51.8 MB 39.6 MB Rack: /var/www/redacted/current
### Processes: 10
### Total private dirty RSS: 259.50 MB
I don't know what the author's experience has been, but I've seen the recommendation to trim Apache back to 10-15 MaxClients all over the place, and when we took that advice, the results were catastrophic to app performance. We almost fell apart during an event because of slow clients tying up Apache processes. We did plenty of benchmarking prior to going live, but getting a picture of real world performance is a lot harder than running `siege -c 250 http://hostname` against your server. If you're going to trim Apache back that far, I'd recommend turning off KeepAlive altogether. You want that Apache process free immediately. If you are forced to run a config this tight, it's likely that you can afford the CPU and interrupt overhead of setting up a new TCP/IP session, but you can't afford the extra processes lying around idle, sucking up memory and MaxClient slots. In other words you're heavily memory bound.So let me be clear about this: Trimming MaxClients to 10-15 is horrible advice if you don't understand what your actual memory usage (maybe you can run more) is and the impact of slow clients. I trimmed our Apache modules back to a bare minimum, resulting in an average Apache process size of 0.45 MB. Yes, less than 512 KB per Apache process. We run 'MaxClients' 150 with zero fear of running out of memory. If you're running a PHP based site, your Apache process size is going to be larger, but you had better know what that is before you start tweaking your MaxClients config.
I should probably write a blog post about tweaking Apache settings, because I've learned a lot of "hard knocks" lessons, not the least of which is "understand your web server's memory usage", but here are some quick tips in the mean time:
* Unload modules you don't need! Saving a couple megabytes of memory across 100 Apache processes means saving 200 MB of memory.
* To understand how much memory Apache processes are using, DO NOT rely on `top`. Have a look at the way `passenger-memory-stats` calculates real memory usage [1].
* Understand the output of `free -m`, especially the "-/+ buffers/cache" line.
* Do not rely on benchmarking tools like siege to tell you the whole story. Slow clients are difficult to account for.
* If you have to run a limited number of MaxClients, or you run up against your MaxClients limit frequently, try turning off KeepAlive [2].
If you follow those tips, you'll see a dramatic improvement in the accuracy of your Apache config for your environment, because that's what it comes down to. Apache is a fantastic web server, but it has to be configured to fit your environment. I don't have any dislike for nginx, but a lot of the nginx recommendations I see are born out of the inability to properly configure their existing web server. If you don't already know/understand Apache, I wouldn't hesitate to make the jump to nginx. It is also a fantastic web server. Hopefully this has helped someone.
1 - https://github.com/FooBarWidget/passenger/blob/master/bin/pa...
2 - http://serverfault.com/questions/86550/apache-keep-alive-or-...
1 - restart apache
2 - run a handful of typical requests through it a few times (or just leave it live for a minute)
3 - look for the largest few workers by the RSS column in "ps"
4 - average that, divide it into the amount of memory you consider it acceptable for apache to use (64mb per worker, 512mb available for apache, therefore 8)
voila, thats your maxclients. If its greater than 256, congrats, you have more ram than you need[1], find something else to do with it.
the real trick here is when you realize "oh thats the exact same math I have to do with fastcgi worker processes in nginx" and realize its all the same.
[1] this statement will become untrue somewhere around 2013 when 128-core servers become cheap and standard
I don't consider WordPress to be server agnostic - it's built for Apache and it only works well on IIS because Microsoft made some effort to make it happen.
http://blog.rassemblr.com/2011/01/wordpress-need-for-speed-o...
http://www.kalzumeus.com/2009/08/22/using-wordpress-and-rail...
So therefore you're down to external caching, which is another layer of complexity and detrimental to anything dynamic you have on your blog (which is pretty common).
Your larger point, BTW, that Apache can be overwhelmed, and that it makes a lot of sense to do some math and tune the configuration -- makes a lot of sense. I'd not really thought about that in depth before, but it seems obvious now. :-)
Thank you sir for standing up for Apache!