1

I'm currently working on an app quite similar to Instagram.

The relationships are:

  • Users follow other users
  • Users follow hashtags

They should then get a personalized feed with posts according to their subscriptions.

It took me a long time to come up with a fitting database structure and I'm still not sure if it is the best approach. Please have a look and let me know what you think.

users (col)
  - user_id (doc)
    - posts (col)
      - post_id (doc)
        -> contains only post id
    - followed_users (doc)
      - user_id (doc)
        -> contains only user id
    - feed (col)
      - post_id (doc)
        -> contains only post id
      
hashtags (col)
  - hashtag_id (doc)
    - posts (col)
      - post_id (doc)
        -> contains only post id
     
posts (col)
  - post_id (doc)
    -> contains full post

I would then write a cloud function to populate the feed collection of each user which will be triggered every time a new post is added.

In the frontend there will be multiple db calls when a feed is loaded: one call to get the post IDs (users/feed) and another call for the content of each post (posts/id) like in this mockup code:

posts = getData($userid/feed).limit(10)

posts.each:

  getData(posts/$post.id)

Is this a viable solution for cost efficiency as well as performance?

TimN
  • 87
  • 1
  • 1
  • 8
  • You can take a look at **[this](https://stackoverflow.com/questions/46979375/firestore-how-to-structure-a-feed-and-follow-system/52153332)** and remember, there is **[no best](https://stackoverflow.com/questions/53053768/what-is-the-correct-way-to-structure-this-kind-of-data-in-firestore/53057707)** database structure. – Alex Mamo Feb 11 '19 at 09:39
  • @AlexMamo I've seen that post, however my model is a bit more complex with more relationships. You are right though, for sure there are many solutions – so I guess I just want to know if my idea would work well enough. – TimN Feb 11 '19 at 11:55
  • As long as you found the corresponding queries, why will not work well? – Alex Mamo Feb 11 '19 at 11:59
  • Basically I want to make sure that performance and costs are kept low and balanced :) E.g. I could have denormalized even more and copied the whole post into each collection, but I think updating all these posts would tax the cloud functions too much. – TimN Feb 11 '19 at 12:11
  • What is the solution you went with? – MobDev Sep 06 '19 at 02:35
  • I have similar situation like yours, can you make some update about your solution, please ? – Walid Mar 14 '20 at 16:34

0 Answers0