Hi, from a performance perspective (server and mobile app) is it better to gather all the information needed to display on a page from multiple tables with one Server Connect API call or do it with multiple SC API’s?
An example would be in a mobile app, a page displays data related to a listing (like eBay), the listers profile and related information.
Would it be easier for the server to assemble all the data from relevant tables (listing, profile, related listings etc.) and return everything is one relatively large json from one API call?
Or would it be easier to receive multiple API requests from the mobile app and return each section bit by bit?
In the mobile app page, is it better to send one API call to get all the data returned at once and present the various sections on the page or send multiple API calls and deal with each one as they are returned?
Or are there positives and negatives to both options on both the server and mobile app which cancel each other out so would end up with personal preference?
Historically, fetching all the relevant data for the current page view at once was/is the right approach, as it usually saved server resources by not creating multiple TCP connections or incurring the cost of spawning multiple PHP processes (each API call on a PHP has a start-up overhead).
Nowadays, with HTTP/2.0 I believe the concern of multiple TCP connections is negligible as I believe it multiplexes just one connection (so you’re able to encapsulate multiple HTTP requests in just one connection, instead of creating multiple). The PHP overhead still exists. On NodeJS I believe that’s not much of a concern, but usually you have a reverse proxy in front (e.g.: Traefik or nginx) and they also have to create a socket connection for each request, and there’s no guarantee your back-end is ready to accept HTTP/2.0 even if your reverse proxy exposes that to the end user.
This can be a complex topic as there are lots of variables in play in regards to the exact hosting setup you’re using, but my recommendation is the historical recommendation, which is to use one big API call instead of several smaller. But you may find lots of people here perfectly happy with multiple small API calls. A famous Wappler told he has 200 SC API calls on his website and everything’s working great
The way the Wappler documentation is setup, I believe they use a different SC for every different thing
Here’s what I think about when deciding; and for what it is worth, I use both at various times.
Start with what the user will experience and tolerate. For example, a user is much more tolerant of a longer delay on the first hit than on subsequent hits. I think for a mobile app this is especially true—people expect a snappy mobile app, more than they expect a snappy web app, but are accepting of some limited load time up front. The point being, consider what the users expectations are for this particular task.
Will the data returned need refreshing? If a component piece of data will need updating, then it might not make sense to retrieve everything in one call (because you’ll then be retrieving everything each time one little bit gets updated.)
As far as I know, the browser will limit you to six simultaneous connections to the same domain, so if you have lots of small requests, they might queue up. I haven’t really researched this though.
Can the data be retrieved in the background while the user is doing other tasks—so delaying the retrieval, but also pre-loading.
Keep in mind that you might also want to be using web sockets to refresh things and that caching is at your disposal. And in a mobile app like an SPA, you can load things once in the main page, and then move the user through a series of views without losing what was already returned to the main page.
As always, the answer is not simple as there are so many factors.
For my particular case I think I will go with 3 SC API calls for the single page.
The data is static (like the example of an eBay listing) but dependent on the list item selected on another page so will only be loaded as needed.
The initial API call will return the location of some images to be displayed so it can get on with that whilst getting the other two API calls and as mebeingken mentioned, the responsiveness of the app needs to come before keeping the server happy
It will also for my old brain to create and check 3 specific calls rather than one big one.