0

My app let user sell and buy products that other users posted.

I have a general recent post fragment where i show the most recent post of all users no matter if you follow them or not. So with this aproach(table below), getting the last 30 posts ordered by timestamp is very fast because of indexing in firestore.

users(collection){    
   user1(document){
    username: rocky
    following:[user2,user3....]     // or subcollection with users as documents
    }
   user2(document){
    username: paul
    following:[user2,user5....]     // or subcollection with users as documents
    }
}

posts(collection){
     post1(document){
     author:user1
     timestamp:...
     imageurl:...
     ...
     }
     post2(document){
     author: user55
     timestamp:...
     imageurl:...
     ...
     }
}

But i also have a feed fragment that works exactly like instagram's feed, it shows the post of the people i follow ordered by date.

I was thinking i can store the users that a user follow inside a list or create a subcollection in every user, then when i load the app get that list of ids of users and here is where i dont know how to procced.

So lets say i get a list of 5k ids, now i have to search for all the posts that contain every id, get the posts and order them by date.

I would have to make 5k reads and some more to read the posts. Is there an efficient way of doing that?

I reasearched a bit and found that a good way is to denormalize the data and add a new collection like this:

feed(collection):{ 
    user1(document){
        posts(collection){
            post1(document){
                //post info
            }
            post2(document)
                //post info
            }
    }
}

But like in instagram the main focus of the app is to create post and have people see it. So most of the users will create many posts per day.

So if i have 100 users with 10k followeers and those 100 users create 3 post a day i would have like 3 millon writes just for that 100 users plus a few more if they edit that post in the future and that sounds scary!!

Is there a better way of doing this?

Maybe im thinking this the wrong way, if i have 100 users with 10k followers this means my app is very active so i should be able to generate revenue to cover that cost.

Sorry fot the long post. Any help is appreciated.

Puntogris
  • 325
  • 2
  • 13
  • I think this answer, **[how to structure a feed and follow system](https://stackoverflow.com/a/52153332/5246885)** might help you. – Alex Mamo Mar 11 '20 at 10:50
  • It does, if i organize the posts in that way and i need to get the most recent 100 posts made in my app(no matter who is the author) by all the users ordered by date. Would i need to use a group collection query and because that system uses an index i could sort by timestamp and would be as similar as if have my posts in a top level collection as documents and the author id and date inside as a field, right? – Puntogris Mar 11 '20 at 15:40
  • Yes, I think it might work. – Alex Mamo Mar 11 '20 at 15:48
  • So first i query the uid's of the people i follow, now how do i query and tell the database: hey, search in all the userPost ordered by date and if you see that the authorId match any uid in the userFollowing, get that post. I could do like you said and get the last 3 post of every user i follow but what if a user last post was 1 year ago, i would get his last 3 post but i wouldnt use them. the app doesnt care to get the same amount of post from evey user i follow, it just want lets say the last 20 posts across all users i follow. Is this posible or im better of duplicating the posts? – Puntogris Mar 11 '20 at 18:53

0 Answers0