Skip to content Skip to sidebar Skip to footer

Firebase Database - The "fan Out" Technique

I was investigating the Firebase Database sample for Android and realized that it stores its data in the following way: I am not quite familiar with NoSQL techniques and trying to

Solution 1:

Data Fan Out is a great technique to manage massive amounts of data. If you do not use this pattern, you could have serious scaling problems in the future.

What I see from your database structure, is that you are storing the whole post information twice, and that is not a good practice. You want to store just a reference to the post under another node instead. So, you will have a node named users-postswhich will consist of user keys, and each of those keys will have a set of posts keys with value of true. To make it more clear:

enter image description here

This way, you are tracking which posts the user has written under the users-posts node; and also the user that has written each post under the posts node. Now, you may need to get a list of all users' posts. What you would have to do is to synchronize on the users-posts/USER_KEY/ node to get the keys for all the posts that the user has written, and then get more post information using the post key you just got.

Why is this database design recommended? Because you are getting much less information for each synchronization (with Firebase we are not issuing requests per-se, so I call the reading a synchronization). In your example, if you attach a listener to the user-posts/USER_KEY/ to get a list of all posts, you will also ask for ALL the information of EACH AND EVERY post they have written. With the data fan out approach you can just ask for the post information you need because you already have the key of the posts.

Solution 2:

In my opinion this is not a good approach since you need to keep in sync those data and Firebase doesn't provide any tool to keep duplicates in sync. A good approach would be to store only the key in user-posts.

I suggest reading this, it is very interesting to understand how to structure data: https://www.firebase.com/docs/web/guide/structuring-data.html

Post a Comment for "Firebase Database - The "fan Out" Technique"