Laravel REST API and high CPU load Laravel REST API and high CPU load json json

Laravel REST API and high CPU load


You're trying to retrieve data from others' servers. That is putting your CPU "on hold" until the data is fully retrieved. That's what is making your code be so "CPU expensive" (couldn't find other stuff that fits here =/ ), cause your script is waiting until the data is received and then release the script (CPU) work.

I strongly suggest that you make asynchronous calls. That would release your CPU to work on the code, while other part of your system is getting the information you need.

I hope that'll be some help! =D

UPDATE
To make examples, I'd have to re-factor your code (and I'm lazy as anything!). But, I can tell you for sure: If you put your request code, those who make calls to others site's XML, onto a queue you would gain a lot of free CPU time. Every request are redirected for a queue. Once they're ready, you treat them as you wish. Laravel has a beautiful way for dealing with queues.


what I would do first is to use a profiler to find out which parts would need an optimization. You can use for example this:

http://xdebug.org/docs/profiler

As well you didn't specify what kind of cpu is it, how many cores are you using? Is this a problem that your cpu is getting used that high?


you should use Laravel's Queue system along with beanstalkd for example and then monitor the queue (worker) with artisan queue:listen