API Call Required for Project Stupidly Slow

Soldato
Joined
26 Aug 2012
Posts
4,428
Location
North West
I have a project for work where it relies on heavy use of API calls to a 3rd party service. However, these calls are painfully slow and when adding some PHP back end or JS processing on this data-set can take up to 30-40 seconds (with small amount of objects which will grow exponentially) to return the result to the user. Now while I can shave a tiny bit of this I'm sure with improved code, it's not going to make the world of difference.

The call gets a list of objects and then basically loops through and gets a list of attributes for those objects, so essentially looping through the original returned objects the API offers no other way to get this required result.

Now, I looked into running background API calls and caching the result and even caching the result every set run through, however the problem is the resulting information needs to be completely accurate as important decisions could be made on the result of it (not that they aren't reversible just a pain in the arse). The alternative is to load the page and then give a spinning wheel of death for 30+ seconds.

Now I know I'm pretty stuck as poor user experience is the lesser of two evils compared to accurate data but I'm wondering if anyone knows of any better tricks/trips to get around this?
 
Pre-calculate it on a job?

I.e every 20 minutes build the cache, serve only cache to the users? Make clear it's delayed by 20 minutes information if crucial to do so.
 
If your problem is as stated :

1) API call is slow
2) API is black box and can't be altered
3) Caching is not an option because results cannot be stale

Then the only solution is to have users wait for the API call to complete.

You need to be able to negotiate on point 3) to have any kind of solution....and depending on the usage patterns, even that may not make much difference. i.e. if you set a short lifespan of 20 minutes or something on the cache, and multiple users aren't requesting the same data in that 20 minute period usually then you'd get no benefit.
 
Which part is slow, the API call returning data or the calculation loop?

Can the loop be parallel? Can it be batched in any way?


So first API query is sent and takes <1s to return. Then processing makes an API call for each element of the array returned in the original API query. The subsequent queries onwards are what take the time and as their are always multiple it all stacks up.

Perhaps running the subsequent could be ran parallel but potential their could be an infinite number of these queries, so potentially that could be difficult to manage.
 
Are the follow up queries made to the same 3rd party?

If so, could you speak to them about changing what is return with the 1st call so you get everything you need in one hit.
 
Perhaps running the subsequent could be ran parallel but potential their could be an infinite number of these queries, so potentially that could be difficult to manage.

This would be the way I would approach it. Do the initial call and then run the resulting calls in parallel. You'll have to write something to cap how many threads you've got going at once (so you could have 10 calls running in parallel, for example).
 
Then processing makes an API call for each element of the array returned in the original API query.

This is the red flag for me. Is there really no way to query batch data from the API? How many elements are we talking here? Tens, hundreds, thousands? Assuming the data for each of the items you're querying is related in some way, it seems bizarre there's not a better way than querying one by one.

Without any cooperation from the API provider I'm really struggling to see how you could remedy this without some sort of compromise.
 
Have you discussed the issue with the third party providing the API? They may be able to either tune it their side or advise on ways to influence the processing by amending the data in the call.
 
Back
Top Bottom