P. Nick
P. Nick

Reputation: 991

How to deal with slow Web API from external source

I have a small problem with using a Web API from an external Source (Steam). I need to do multiple calls to the API and fetch its results, and this slows it down extremely, causing my application to load for a long time.

I'm using Laravel for my application. I have thought of caching the result and showing the cached (old) results to the user while fetching new in the background, which would be visible on the next page load. The problem with this is how I would increase the speed of page load the first time, because I have to get the data somehow.

Other than that I don't have a clue on how I can increase the performance on my side. Any suggestions would be appreciated!

Upvotes: 3

Views: 4211

Answers (1)

Mathew Tinsley
Mathew Tinsley

Reputation: 6976

There are a lot of ways you can approach this problem, but if the objective is to increase the speed of the initial page load then every solution is going to involve making your application asynchronous.

Rather than waiting for a third party service to respond, dispatch a job to fetch the data. You can then respond with stale data from your cache. If there is no data in the cache you can either render a loading message to the user or you can dispatch the job synchronously, which will increase load time.

On the client side, you will either need to poll for the new data or use a websocket to push the data to the client. The websocket approach is more difficult to setup but will result in fewer requests made to your application and quicker responses for the end user.


Depending on your specific use-case, you can further optimize this approach. For example if you are always fetching the same data set from the third party service you can always serve data from the cache and use a cron job to refresh the cache. If this is a viable solution for you then you don't have to worry about dispatching jobs or updating the client asynchronously.

Upvotes: 6

Related Questions