How to effectively handle a large API call

  softwareengineering

1

I have an API call that returns products to be displayed on a Point-of-Sale system. the current account I have has about 7000 products.

Initially I made the API return all products at once, this works for users that have 1-1000 products and the Point-of-Sale loads relatively quickly. However when users have more than 1000 products, the loading of the system takes more than 15 seconds to fetch and load.

So, I changed how the API works. I made the API return 200 products at a time, and the Point-of-Sale has to repeatedly call the API to collect all of them (essentially pagination).

But when doing this, I have introduced a new issue. The Point-of-Sale loads quickly, however there might be a chance that all of the products have not loaded yet. So when using the POS, you might not see everything.

My question is. Is there a better way to handle something like this?

7

3

You could try:

  • Streaming the response.

    Rather than waiting for the full result set before displaying the UI, start drawing as the items come in and continue to update until they are all there

  • Push Updates

    Get the whole list when you first load the app, but then register for push updates. So when the source data changes the app receives just those updates and keeps its local list up to date with the server

  • Calling before the user clicks.

    Rather than waiting for the user to request the list, make the call in the background on a regular basis and display the already fetched list when the UI needs it.

2

Most likely you will have to revisit how products are queried and returned. From a practical standpoint can a user even manage 7000 products at once? Probably not.

This is usually solved via paging. That is, Returning a fixed amount of products, usually 10, 20, 100 at a time that can be paged through. The UI only displays a fixed amount and if more products are required another “page” of products is returned.

This allows for efficient queries and return of data no matter if you have 10, 100, 1000, 10000, or a million products.

Also, allowing users to narrow search of products helps out. Even if they don’t put in any criteria only the 1st page is returned.

There are different approaches to accomplish paging depending on the technology stack that is being used.

LEAVE A COMMENT