6

I have a database with posts, users and other nodes like classic social-media app.

I'm continuing implementing best-practices to my application. And now I want to rewrite my news strip (posts of users, that I follow (like home tab in Instagram)).

I have read some info about fan-out strategy and now I'm confused in how to do it properly.

So, I have 2 opportunities:

  1. Like here: Every time user adds new post, this post with full information will be copied to users-timeline(users news strip)/UID/postId: FULL INFO to all followers of this user. And every edit will edit each mention of this post.

  2. Every time user adds new post, this postID will be copied to user-timeline(users news strip)/UID/postId: true to all followers of this user. And every edit will edit only source post.

Which logic is better with Firebase?

Community
  • 1
  • 1
Vlad Pulichev
  • 3,162
  • 2
  • 20
  • 34

1 Answers1

4

I suggest you using David's East solution. As I see in his post, he uses Firebase denormalization and data flatten, which are the best common practices within Firebase.

I also recommend you reading NoSQL Data Modeling Techniques and Structuring your Firebase Data correctly for a Complex App for a better understanding.

If you have a SQL background, I recommend you seeing also David's East tutorial, The Firebase Database For SQL Developers.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • So, your choice is 1? – Vlad Pulichev Jul 14 '17 at 10:20
  • 1
    Definitely, number 1. – Alex Mamo Jul 14 '17 at 10:21
  • Uparrowed. Thanks for links – Vlad Pulichev Jul 14 '17 at 10:21
  • 1
    btw. If one user pressed follow on my profile. Should I use server-side fan-out for adding my posts to his time-line? – Vlad Pulichev Jul 14 '17 at 10:29
  • Yes, you can use server-side fan-out. – Alex Mamo Jul 14 '17 at 10:40
  • Alex, I'm sorry again. Example. I will have 100 followers. I posted smth. Full info of this post will be copied 100(or more if needed) times? I cant imagine it for real – Vlad Pulichev Jul 14 '17 at 12:00
  • Fan-out itself represents the process in which you duplicate data in your `Firebase database`. Those duplicates helps your eliminates `slow joins` and in the same time help you `increase the read performance of your app`. In this case is only your decision which way you follow. If you want to follow `David's East` suggestions, then duplice data like this, otherwise do it in the other way. But remember, duplicating data is not a bad thing. You can wait for other answers, to see also what orher users can say but IMHO, i'll follow choice no.1. – Alex Mamo Jul 14 '17 at 12:09
  • Ok. Thanks you so much, Mr!! :) – Vlad Pulichev Jul 14 '17 at 12:10
  • I'm so sorry. But in this structure, what should i do with likes and comments count? Should I update each post likes/comments count on each like/comment event? Accepted answer btw. Thx you sooo much! – Vlad Pulichev Jul 20 '17 at 09:09
  • Regarding likes, don't forget to use transactions. And yes, update comments like you said. To count, just use `getchildrencount()` function on the DataSnapshop. – Alex Mamo Jul 20 '17 at 09:17
  • So, in each post mention I should store likes/comments count and on each event update in each post mention this count by taking getchildrencount() from postscomments(or likes)/postid/? So, for one like i should update like 100 nodes? Have I understood correct? btw I'm using cloud fucntions for update count. But its fearing me, that for each like I should update like 100 posts.. – Vlad Pulichev Jul 20 '17 at 09:20
  • Or its better to have another node, like postscommentscount/postslikescount? And on each post download also go to this node and get this count? I'm still confused – Vlad Pulichev Jul 20 '17 at 09:27
  • Second option. Regarding likes, another approach is to create another node, named likes, in which you can store only the comments ids that contains the number of likes. This will save also bandwith. – Alex Mamo Jul 20 '17 at 09:40
  • And just on each post download get this count from this node? correct? – Vlad Pulichev Jul 20 '17 at 09:41
  • Sorry for questions, but i have rebuilt db structure for like 5 times....) – Vlad Pulichev Jul 20 '17 at 09:42
  • 1
    Yes, you can do it exactly like this. – Alex Mamo Jul 20 '17 at 09:42
  • Well. Now I'm almost sure, that I know, how it should be. My big thank for you, Alex! – Vlad Pulichev Jul 20 '17 at 09:44
  • @AlexMamo Let's say I want to sort posts based on a field... would this be possible? each fan-out must be indexed in that case, correct? – Stefano Aug 28 '20 at 17:48
  • 1
    @Stefano Sure, it is possible but without seeing the code and your database schema, I cannot be much of a help. So if you have hard times when sorting elements, please post a new question, so I or other Firebase developers can take a look at it. – Alex Mamo Aug 29 '20 at 07:05