3

I am building a social media database schema, in which I have users, followers, tags and posts. To conform to the firebase model I have flattened the structure as suggested in the firebase documentation as seen below. The issue that I am struggling with is when a user selects a tag and sees a bunch of posts from the tagPosts table all related by tag returned, I would then like to show the posts created by the current users followers first.

In SQL this would be done with an inline query checking the users followers, against the posts returned by a specific tag. However in firebase I am not sure how do this without downloading all the posts contained under the tagID node in tagPosts and checking through each post's creator against the node of Followers for the current user userID. This operation could easily grow out of hand for 100s of posts amongst 100s of users. Ive tried modeling off of this answer, How do I check if a firebase database value exists? and this article From SQL to Firebase — How to structure the DB for a social network app. Am I poorly structuring the data how do I fix this thank you so much.

 `
Users-
     -userID1
         -misc. userData
     -userID2
          -misc. userData
Followers-
     -userID1
        -userIDOfFollower1   
        -userIDOfFollower2
Following-
     -userID1
        -userIDOfFollower1   
        -userIDOfFollower2

Posts-
    -postID1
       -userIDFromCreator
       -misc. PostData


Tags-
    -tagID1
       -misc. TagData
TagsUsers
    -tagID1
       -userID1
       -userID2
TagsPosts
     -tagID1
        -postID1
        -postID2

Edits-Thank you Frank

In our storyboard flow we plan to have a user see a wall of tags determined by constantly updating popular score based on properties of the tag and where we predict the user may have interest. The user will then select a tag and see posts related to that tag, from those posts I would like to show the posts from a users followers before those of everyone else who’s post falls in the category of a specified tag.

I have considered two possibilities either I optimize on reads in which I would have to keep track of every time a users follower posts to a tag and record the tagID along with the postID in a node for every follower a user has who posted in a special node of FollowersTags which would have a structure of listing for each userID a list of users and the all the followers of a user posted to which would become 100s of writes for each post created directly proportional to the number of followers a friend has.

*creates a list of posts to a specific tag made by followers
FollowersTags
    -userID1_tagID1(composite key)
            -postID1
            -postID2
            -postID3
            -postID4
    -userID1_tagID2
            -postID1
            -postID2
            -postID3
            -postID4

Or I could optimize on writes as tried above, which presents us with our current predicament of having to perform a query 100s of times directly proportional to the number of posts in a tag.

Is there any way around these two options which of the two is the better approach.

Unfortunately I would not be able to predict the posts displayed to the user before they select a tag.

3
  • just curious, why did you choose Realtime Database over Firestore? Dec 28, 2018 at 6:21
  • @RonRoyston At first we were using Firestore due to its advanced Querying capabilities. As Firestore has quotas regarding reads and writes, we ran into issues of cost effectiveness regarding performing many frequent read and write operations and decided to optimize for reads and writes over storage for our particular app. The problem became most evident to us in performing counting operations as discussed here stackoverflow.com/a/48540276/9367155 and here stackoverflow.com/questions/53507083/… Dec 28, 2018 at 17:17
  • ...very interesting! Thanks. Dec 28, 2018 at 21:07

1 Answer 1

1

In the Firebase Realtime Database, I typically model the data in the database to what I show on the screen. So if you have a "wall" of recent, relevant posts for each user, consider modeling precisely that in your database: a list of recent, relevant posts (or post IDs) for each user.

UserWalls
  userID1
    "timestamp_or_push_id": "postId1"
    "timestamp_or_push_id": "postId2"
  userID2
    "timestamp_or_push_id": "postId1"
    "timestamp_or_push_id": "postId3"

While the problem of determining what to show remains the same, with this database model it's now a write-time problem, instead of a read-time problem.

1
  • Thank you so much for taking the time to answer my question. Your answers on other questions and your videos have helped me so much, I don't think this project would have been possible without the resources you provide. Your presentation on geohashing in firebase was a huge help. I am truly a very big fan. Taking into account your advice please see the edits I have made. Dec 28, 2018 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.