By john_b on
I am setting the MaxClients directive in Apache's .conf. The setting seems pointless setting when using a reverse proxy since there can only ever be one client, Varnish, even where requests are being passed to the backend (unless of course the browser is pointed to the backend port). Is that correct?
Comments
Not quite...
Varnish and apache are both multithreaded so can create/handle multiple requests simultaneously. MaxClients should be calculated the same as if you were taking direct requests to apache.
That is to say, take your total RAM, subtract enough for the OS, and any other running applications on the server (mysql/apc/memcache/varnish) and then divide what is left by your php memory limit. You can get into a lot more detail, but that is the basic formula to keep you out of swap.
I should also say...
Keep in mind one page load != one Client. Every page has many requests - one for each image, CSS file, js file, and index.php.
Thanks for that. Very
Thanks for that. Very helpful. But then what is the difference between ServerLimit and MaxClients?
I am tuning Apache on a VPS with a few Drupal sites. Problem is it starts sapping like mad overnight, probably while Virtualmin's backup is running, so I am trying to get Apache's swapping under control.
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
ServerLimit is the number of
ServerLimit is the number of threads that Apache will use to handle the Clients. Generally, you want them to be the same or close to it. You never want MaxClients higher than ServerLimit or else there won't be threads to handle the incoming requests. You'll see that there are a few other settings MinSpareServers, MaxSpareServers, and MaxRequestsPerChild. These instruct apache how many threads to keep around for incoming requests, and how long to keep them around for.
Let's take a sample configuration:
When apache is restarted it spawns 5 threads (StartServers). The first 5 incoming requests go to those threads, on the 6th request, if apache hasn't finished with one of the first 5, it spawns a 6th thread, (and 7th and 8 and so on up to 15).
Then when apache finishes with each request, that thread sticks around - unless there are already 8 other idle threads (MaxSpareServers) then apache would kill off the 9th thread.
This is important, because each thread holds on to the RAM that it uses, so if you have a heavy page - even if you subsequently server lighter pages - that thread holds on to the RAM and does not release it until that thread is killed.
In this even if a thread is one of those original 5 and never gets pruned by MaxSpareServers, it will get killed after it serves its 100,000th request and respawned.
Spawning threads is intensive, so it is good not to do it too often, but so is having each thread consume the maximum amount of RAM, so it is good to re-spawn them occasionally.
SO, so answer your question, ServerLimit is the hard maximum of threads allowed, MaxClients is the number of simultaneous requests allowed to come in to those threads. Usually, you want these to match, occasionally if you are experiencing a spike you'll lower just MaxClients and NOT ServerLImit so as not to flood the server, but that is the only use case I've found for not having them always match.
Many thanks. My LInode has
Many thanks.
My LInode has 1.5GB RAM. I have got mysql caching pretty much under control for myisam and innodb, and I kept reducing Apache demands and am now at:
StartServers 5
ServerLimit 15
MinSpareServers 2
MaxSpareServers 3
MaxClients 15
MaxRequestsPerChild 1000
Which probably is going to have a hard time coping with any kind of traffic spike (even with Varnish).
And yet Apache swaps a lot, and swap gradually grows (I clear it every morning), with Apache as the main culprit. Other main swap users are Virtualmin, mysql, and bind. Varnish uses malloc but seems to be the first to clear cached data when necessary, and does not swap much. Based on your advice, maybe I am better of with more requests per child. But I can afford to use more CPU.
Any further suggestions about rule of thumb (or more intelligent ways) to tune Apache would be welcome. You have already been helpful, and I will think about your posts some more. Maybe I just need more RAM....
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
What is your php memory
What is your php memory limit, and mysql footprint. How have you determined that apache is the main culprit?
PHP memory set to 128mb per
PHP memory set to 128mb per site, all sites use fcgid. It does swap a bit. Mysql usually shows 17% memory usage when warm. I test for main culprit by restarting services one by one and seeing how much the swap usage drops, and cut mysql usage until it was not swapping much.
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
A few problems with that...
That isn't really how swap works. It's not like each process has a designated amount of swap that it uses when it needs to go into swap. The whole system has a set amount of RAM and whenever anything pushes the whole system over that amount of RAM then any new requests land in swap space (it's slightly more complex than that, but for the purposes of this, that is sufficient). If you stop a service and the whole system is still in swap - it is not really your culprit (although in minimal swap situations stopping any service could take you out of swap, if that makes sense).
What you actually need, is to see how much RAM each process on your system *is* taking up. Generally, using ps on commandline gives you a sufficiently accurate idea, or you can google and find numerous scripts for getting the memory usage per application on a given system.
Now, your second problem, if that is what php memory limit is set to (128M) and your apache config is as stated (MaxClients 15) then you have configured apache alone to be able to utilize 1920M of available RAM, or 1.875 Gigs. (15*128M/1024).
You said you have 1.5G on this system and are also running Varnish, MySQL, and a few other things. I would do something like the following to start:
Mysql: minimum 256M allocated to various caches (see some tutorial on tuning MySQL), but basically try to keep the table and query cache variables combined under this.
Varnish: Depending on your hit rate and page size, 128M or less should probably be sufficient.
Memcache/APC/ whatever else you need insert here, let's say X for now.
System: minimum 512M - depending on how lightweight the OS is, it needs to run everything, so I like to reserve a good bit for it to do that - however, this isn't so much a configuration option as what I like to have leftover at the end.
If these are your given's we then need to solve for MaxClients on apache:
(1536M total - (256M mysql - 128M Varnish - 512M system)) = 640M
If you had an X from above for other applications (mail server, memcache, anything else) - subract it here. I'm going to assume nothing for easy math, but you should really check)
640M Apache / 128M (php consumption per thread) = 5
And before you start tearing your hair out about how few requests that is - remember that that is 5 *simultaneous* requests, and if you look at YSlow one of the number one things you can do to improve page performance is REDUCE THE NUMBER OF REQUESTS A PAGE NEEDS. Also remember, several requests on a page load will be served through varnish if you have it appropriately configured (images, css, javascript).
So, with that number, let's look at your apache configuration again - try something like this:
If you really feel like you don't use the full 128M php limit on most pages - try edging up to 6 or 7, but if you start to swap, bring it back down.
This, also, I hope, makes it clear why a primary first point to scaling is a dedicated MySQL macine (reclaim that memory for apache), and/or a CDN (request all static assets from another machine), and/or dedicated Varnish machines to stop requests from ever getting to apache.
Hope this helps.
-Sam
thanks
Hi Sam
I greatly value your advice. Incidentally I looked at your site and suggested it a couple of times already here on d.o. Looks good, and I think people shouldn't cut corners on hosting costs. But maybe I am...
With around 6 fairly low traffic Drupal sites and 6 WP sites on a VPS would you seriously recommend moving MYSQL to a different server? And does the MYSQL server need to be in the same data centre to cut down latency?
For a handful of sites I hesitate to go the route of separate mysql or varnish server. I tried Cloudflare free CDN and did not like it. Probably I just have to up the ram. Mysql is a problem because I have WP sites with MYISAM which also needs to be cached as well as Innodb (as I hate my sites to run slow: how do people run Drupal on 500MB RAM VPSs, I wonder?). Then there is APC, Virtualmin, mail, bind, spam filter. On that basis it leaves nothing for Apache. I guess with a 1.5GB Linode I am being cheap. Yet it is hard to charge my clients more for hosting Drupal, when they see the shared hosting packages out there for a fraction of what I charge.
John
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
One by one: "I think people
One by one:
"I think people shouldn't cut corners on hosting costs. But maybe I am..." - Depends on your needs. My personal blog gets like 2 hits a month - who cares if it's slow? However, I will say that I get a lot of requests to pay my sysadmin rate to 'optimize' a site in order to avoid a higher recurring monthly, and that usually makes little to no sense. Realistically, what are we talking about in an increase? $10/month? $25/month? That seems like a jump, but when you realize that that amounts to less than 4 hours of my time over the course of a year, why would you pay someone to optimize as opposed to just getting on the right hardware.
"With around 6 fairly low traffic Drupal sites and 6 WP sites on a VPS would you seriously recommend moving MYSQL to a different server?"
Again, depends on your plans. However, I will say I usually get a server up to 4G of RAM before considering breaking it out into *any* configuration of separate servers.
"And does the MYSQL server need to be in the same data centre to cut down latency?"
For any reasonable configuration, yes.
" Mysql is a problem because I have WP sites with MYISAM which also needs to be cached as well as Innodb" - Do some research to see if you can get them all on one DB engine or the other. Usually you can ALTER TABLE to innodb and remove any myISAM caching to recover some RAM.
" how do people run Drupal on 500MB RAM VPS" - I would hazard the guess that anyone doing this either isn't running a very fast site or is doing their performance metrics in an isolated single user environment.
'Yet it is hard to charge my clients more for hosting Drupal, when they see the shared hosting packages out there for a fraction of what I charge." - Well, that comes in setting expectations. Personally, I think that no one running on shared hosting is really all that serious about their website. And, not to be rude, but with 6 sites and multiple 'clients' you fall in the realm of 'shared' as well. I'll also say, pay attention to what you're paying for. You can get very reasonable fully managed hosting that you don't need to chase all these numbers around for, with dedicated resources. $30-$100 a month seems high, until you realize you have someone like me on call 24/7 for emergencies, and as much back and forth like this all year as you need.
All very interesting. I do
All very interesting. I do Drupal and WP sites and maintenance for a living, and don't really like hosting (though I enjoy learning about servers), but it happened, and some clients like the one stop shop. Now there are more Drupal specialists around I may stop taking sites on, though most high grade hosts are in America, my clients are mostly in UK, so for now I only send WP clients to outside shared hosting companies. If I cost my hours for giving my clients really fast sites, it makes a massive loss for me. My own thoughts about your service are 1. I hesitate to put a UK client on a US server; 2. not sure about no email but I am moving one client to Google Apps email to see how it goes.
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
I hope that didn't come
I hope that didn't come across as a sales pitch. Yes, I work(ed) for a hosting company I prefer, but look into managed hosting on your side of the pond. All the same points I made above apply in the same way.