Laravel: Asynchronous and concurrent HTTP requests

Antoine Lamé
3 min readNov 4, 2021

--

When reaching an API, sending outgoing HTTP requests can take time. If you are using the Laravel HTTP client, you can implement asynchronous requests to significantly improve performance.

Asynchronous requests

When you send a non-asynchronous HTTP request, your code waits for the response before moving on to the next statement.

Synchronous request with Laravel HTTP Client

This means that if the HTTP request is slow and takes 5 seconds, your code will idle for 5 seconds. It will just wait, without doing anything.

What if you want to take advantage of this idle time to perform other actions? You can do this by using asynchronous requests.

An asynchronous request is a request that is executed in the background, so you don’t have to wait for the response to continue your code. The Laravel HTTP Client has the async() method for that.

Asynchronous request with Laravel HTTP Client

Take a look at the output:

The request has been sent, but we didn't wait for the response.Response received!{"I am the request response"}

We have sent an HTTP request in the background without blocking our code. The HTTP request is encapsulated in a promise, which informs us about the status of the request and will contain the response once it is received.

You can use $promise->getState() in order to know the state of your asynchronous request that is being executed in the background. It will return pending, rejected or fulfilled.

Promise state

Be aware! If your code ends before the asynchronous request is finished, it will be interrupted. This is why you can, at the end of your code, use $promise->wait() to wait for the response to be received.

⚡️ You now know how to perform other tasks while requests are being sent instead of waiting for responses without doing anything.

Concurrent requests

A concrete use of asynchronous requests is to send several requests at the same time rather than one after the other. We call them concurrent requests.

Imagine that you need to retrieve a list of users via an API, but the results are paginated. It’s going to take a lot of time to get the users page by page if you send the HTTP requests sequentially like this:

Synchronous and sequential HTTP requests

Instead of sending requests sequentially, we could send our 30 requests at the same time. This will greatly improve performance, especially if the API is slow.

From what we have just learned, we could do:

Concurrent requests

It works well and this is a good solution. But the Laravel HTTP Client comes with an handy method called pool() to make it even easier. Check this out:

Laravel HTTP Client : Pool

$responses is an array and each response instance can be accessed based on the order it was added to the pool.

So our code becomes:

That’s it!

I hope you enjoyed this post. Feel free to clap this article (👏) and subscribe to my Medium newsletter for more.

👋 I offer tech consulting services for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through LinkedIn, or you can find my email on my GitHub profile.

--

--