Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice for Laravel Asynchronous Requests

I have a Laravel application. The application loading perfectly fine, when I only make 3-10 API per page in my controller. Now, I start to see the latency when I start making 200 API requests per page in my controller.

Since Laravel is MVC.

All the code in the controller need to be fully executed and finished, and then it will send all the data/variables to the view. But that is leading to a lot of latency.

I’m thinking to perform that APIs call asynchronously, but I am not sure which one is the best move,

I did a quick search, I found :

  • PHP cURL Async: http://php.net/manual/en/function.curl-multi-init.php
  • Laravel Async: https://laravel.com/docs/5.1/queues
  • PHP Promise: https://github.com/reactphp/promise

Any directions/suggestions on this will mean a lot to me, and others that faced this issue.

like image 436
code-8 Avatar asked Oct 29 '22 02:10

code-8


1 Answers

Explore using Queues for this. Offload any calls to the queue, await for response.

I would recommend against 200 requests per page, it seems excessive. Perhaps start with trying to get that down before rearchitecting.

like image 129
trh88 Avatar answered Nov 11 '22 19:11

trh88