Blog entries   |  Very high Mysql CPU usage and low memory usage with Nginx+php-fpm solution

Very high Mysql CPU usage and low memory usage with Nginx+php-fpm solution

Just a very short post here as I wanted to share some finding on a problem I recently encountered.
On one of my websites I was implementing a Shopping cart instead of just the previous ‘buy now’ button to do some A/B testing to see what does better. When the implementation was done everything was working however I noticed the site was getting unresponsive and eventually the server even crashed.

So I started to investigate and I found that Mysql was suddenly using 80 – 150% of the but with almost no memory being used (weird!) and this was putting the server on its knees. Coincidentally I did had a surge in visitors so I started to tune the Mysql config however that didn’t help. Then I noticed that a lot of -fpm processes were active (using ‘top’ command) and stayed active. They weren’t being killed for some reason. So I tried tuning the -fpm config. Again no results.

At this point I turned back to the code and I found that when I disabled the shopping cart class and restarted Mysql the problems were gone. Weird! Now after a lot of debugging I found that due to the config file setup of I had unknowingly created a circular inclusion in php like this:
— config.php
.php ( class)
—- config-loader.php (part of )
—– cartconfig.php (part of )
—— config.php (back to the beginning)

I had to make some changes to solve this problem but when I did all was working correctly and no more Mysql problems.
Conclusion: creating a circular (in my case 4 steps) inclusion in php on Nginx + PHP-FPM + Mysql causes php-fpm worker processes not to be killed and Mysql CPU usage to go haywire.

Tech details:
PHP extension: mysqli
Server version: 5.5.31-1
MySQL client version: mysqlnd 5.0.8-dev – 20102224
Connection via: socket
php version: 5.3.27

Leave a Reply

Your email address will not be published. Required fields are marked *