Splitting RAM between MySQL and PHP-FPM on a single VPS, how do you decide?

Mujkanovic

Well-known member
Collaborate
Registered
Joined
Apr 24, 2016
Messages
439
Points
18
On a box running both the database and the PHP workers, the two are competing for the same RAM, and I keep discovering I gave one of them too much only when the OOM killer picks a side. It usually picks MySQL, which is the worst available outcome, because the site then serves errors instead of merely being slow.

My rough method is to size the InnoDB buffer pool to fit the working set if it fits at all, then work out how many PHP-FPM workers the remainder allows using the memory those workers actually use rather than the limit in php.ini. Measured, not configured, since the configured limit is a ceiling almost nothing reaches and sizing off it wastes half the box.

Where I am less sure is headroom. I have seen anything from ten to twenty five percent suggested for the OS and page cache, and on a small VPS that gap is the difference between two workers and five. I leave more than feels necessary, mostly because I would rather waste a bit of RAM than tune this at 2am.

For anyone running both on one box, do you cap PHP-FPM workers and let requests queue, or let it scale and rely on swap to absorb the overshoot? And has anyone had decent results setting cgroup memory limits per service instead of tuning the config values against each other?
 

Chris Worner

Well-known member
Registered
Joined
Apr 15, 2016
Messages
635
Points
28
Cap the workers and let them queue. Swap on a box running MySQL is not a safety net, it is a slower way to reach the same failure, because once the buffer pool starts getting paged the database stalls hard enough that requests pile up faster than the workers drain them. A request waiting in the FPM queue is at least still holding almost no memory.

On cgroups, useful but not as a substitute. Setting MemoryMax on the mysql unit turns an OOM kill of the whole service into something you find in the journal before the site goes down, and MemoryHigh a little below it gives you reclaim pressure first rather than a straight kill. It does not tell you the right number though, it only stops the wrong number taking the box with it. I still size the buffer pool by hand and use the cgroup as the fence.
 

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top