Having a big trouble with CPU leak in Laravel 5.2 fresh install as well as in app in production mode. So the problem is when there are a lot of concurrent requests php-fpm uses 100% of CPUs.
Server setup:
Host machine: core i7 (4 cores 8 threads) + 8GB RAM
Vagrant + Homestead with PHP v7
2 CPUs
2 Gigs of RAM
Nginx and PHP-FPM default settings
The main idea of this tests is debugging server failures at documents storage service I've made a month ago which is having same issues when heavy loaded.
The production server (dedicated) setup is:
Nginx v1.8.0
PHP-FPM v5.5.9
CPU 1 with 6 cores and 12 threads
RAM 32GB
Additional settings made there to handle heavy load:
worker_processes 12;
worker_connections 12228;
@willvincent this is not a problem at all because this are nginx settings and all the CPU goes to php-fpm processes. My current php pool settings are like so:
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500
The CPU usage distributes between 10 php-fpm processes and the CPU load is between 60 and 100% when a lot of concurrent requests are made. And it's the same with fresh Laravel install, no packages, no database, nothing.
Any ideas how to handle heavy load systems in Laravel?
It does this on any request regardless of what the code is. Your machine/setup just can't handle the connections. This is where you would need a scalable solution in place.
Still not :(. But before to do anything else I'll ask a sys admin guy to do some server kernel tuning. As he explained me there is a chance that the network adapter can be flooded because of the heavy traffic. To be hones he was speaking in Chinese when explaining this and was science-fiction to me :D. He told that for heavy loaded server each network adapter interrupt needs to be handled from a different CPU core. This will make your setup to be able to handle much more traffic. In some web articles I red about that the people were speaking for from 20% to 220% better results when doing this. In his opinion the problem is that you/we are receiving too much requests in same time. The server can handle them, the bandwidth can handle them but network adapter not. In that case you will get a lot requests, but the responses will delayed because of this problem and the php will wait and will not release the resources it's using. He had another experience with this problem before with a system that is used 24/7 from 700 people. The issues there were just the same I have- slow response and timeouts. After this tuning it's working as a charm.
Not sure that this will help but it's another point of view.
@aginev What cache do you have? I usually use the default settings and have no issues.
About the max_spare_servers, I know it is used to calculate the pm.start_servers but I wouldn't worry too much and put more than expected. My default value is 35.
About the pm.max_requests = 500. It is usually default to 0 for "endless request processing" as in the doc (I am working with CeNtOS)
Now that said I am not sure I understand chinese either ;-) Hope this help
@mdecooman I tested with a lot of different values in the php config. I tried adding many pools, different types of php pooling, HHVM... but with no reasonable improvement. The app is really heavy loaded 1000 active user in the same time. Nginx CGI cache is enabled (static pages only), all user related data is stored in Redis for 10min, all css and js files are merged and minified to limit the number of requests to the server, all the file downloads are via Nginx X-Accel...
Hi @aginev, @willvincent made a valid point about worker_processes. The nginx doc:
## If you're using an Nginx version below 1.3.8 or 1.2. then uncomment
## the line below and set it to the number of cores of the
## server. Otherwise nginx will determine it automatically.
#worker_processes 4;
You probably checked this but just in case. Try to test from a very basic environment (with Nginx and Php-fpm before adding the other layers). I would love to have more time to help since I love playing with servers...