We have 3 products from which user can order.
Eg:
- Itunes music
- Itunes Books
- AppleStore Apps
Given:
3 microservices, which gives 3 individual paginated responses for order history based on timestamp.
Need:
To display order history from all 3 sources in descending order of the timestamp and paginate them as user scrolls. i.e., sort and merge responses from 3 sources based on timestamp and display.
It is possible that user has purchased:
- From all 3 services
- From any 2
- From any 1
- None
Also, the timeline of purchase can be skewed as well. eg: The user has purchased 10 songs in Itunes music alone.
Approaches I can think of:
1. Heavy-lifting on Client Only:
Will call all 3 API's parallelly, wait until all the API's respond. Store them locally.
Merge and sort based on timestamp.
Clip the topmost n items and display on the device.
As and when user scrolls, will check if there is any data present locally and call APIs accordingly.
2. Heavy-lifting on the Server side only:
Have a proxy which will talk to all the services.
Upon receiving request from client fetchData (noOfItems, fetchedUntilTimestamp)
,
proxy shall fetch data from different sources by calling getData(noOfItems, fetchFromTimeStamp)
for each one of them.
Each data source shall Fetch noOfItems starting with timestamp fetchFromTimeStamp and below and returns a list of data.
Proxy shall:
- SORTED_LIST = sort list of data from multiple sources based on timestamp
- RESULT_LIST = First noOfItems of the SORTED_LIST
- fetchedUntilTimestamp = Timestamp of Last item of RESULT_LIST
Proxy shall return to client: RESULT_LIST + fetchedUntilTimeStamp
A client, from next request onwards, shall call fetchData (noOfItems, fetchedUntilTimestamp)
with the fetchedUntilTimeStamp
received from a server from the previous call
Which among the two would be preferred? Any better approach to solving this?