Which is better? Multiple parallel method calls to get server data vs One method call to get all data


#1

There if a summary page in my app that will need to fetch 6 groups of data from 6 collections. I already created 6 individual method calls for the different details page so I have an option to just call these 6 method calls in parallel.

Since I don’t have much experience with async performance, I’m unsure which is better:

  • 6 parallel method calls fetching data from each collection
    OR
  • 1 method call fetching data from the 6 collections

#2

My further research ended up with this excellent article by @robfallows:

Since my app doesn’t care about how the parallel methods were called (I’m using React’s state variable), it seems like that calling 6 parallel meteor method calls with “this.unblock()” calls in each method call in the server, will result to the fastest execution (i.e. each method call will start independent of the other method calls).

Now, my question is how expensive is it to have multiple method calls per page?


#3

If the data from individual methods can be displayed independently (without waiting for other methods to complete), use separate method calls:

  • If they need to be shown in a specific order, call the methods sequentially
  • If the order of display doesn’t matter, call the methods “in parallel”
    • Note that to achieve “parallel” calls, the code triggering the method calls can be “synchronous”. Just handle the display part asynchronously. And remember to call this.unblock() in the server-side code of the method.

If all data needs to be rendered together, then a single method call might be slightly better. On the server-side, use fibers or promises to run the queries asynchronously and combine the data before returning the method.


#4

Fantastic. Thanks @gaurav7. That confirms my understanding and helps a lot.


#5

Network-wise, I wouldn’t worry much (assuming you’re not dumping huge collections in your method calls :slight_smile:). Other than that, it boils down to your page and component architecture. The usual rules of minimizing the render passes, and the re-renders per pass would apply. And as much as possible try to go for a design that allows for independent rendering of different sections, and render above-the-fold sections before others.