We are developing a REST API which among others is going to be consumed by an HTML5 frontend via javascript. The application is for use within the organization and usually has about 300 users, but we want to scale well up to 1000 users or so.
Normally connections to the API will be made within the LAN so the quality and latency of the connection will be good, although it is not excluded occasional use over the Internet where connections could be slower and with more lag via 3G/4G.
The two options that we thought are:
-
The frontend will make several simultaneous asynchronous calls to the API to load the various components of the interface.
- Pros: Simplicity.
- Cons: More connections to the server.
-
The controller of the frontend will make a single call to the API passing as parameters which objects need to be fetched.
- Pros: Only one connection to the server, although the server will make several connection to the database.
- Cons: Requires mechanisms in both frontend and API. It complicates the design.
Further explanations:
There will be different resources …/Product …/Locations etc. These resources could be fetched alone, but there will be another abstract resource
…/screen?Product&Locations
that will fetch both in one call.
Best Answer
Option 1 (multiple async calls) is the best choice because:
/products
to get a list of products to show on a page, and you also want to display a list of locations where popular products are sold, you have two separate resources:Product
andLocation
. Though they are displayed on the same page, you can't logically make a call to/products
and have it also return locations as well/products
and you're also loading locations, your log files are going to be really confusingThe whole point is that resources are separate, and in a REST API returning many separate resources from a single API path doesn't make sense, even if you're "saving connections to the server". By the way, using parameters to conditionally load (different) resources is not RESTful.
All that said, the only logical option is to make multiple async requests to separate resources: take the modular approach!
PS - Don't prematurely optimize away "connections to the server", especially when HTTP connections are incredibly low overhead and you're on a LAN. That kind of thinking instead of choosing the simpler design right off the bat is going to get you into trouble later.