1

Question in short, how to easily update a value that is duplicated in multiple locations?

I have spent days to try to grab data structure design in firebase. I have studied many resources like:

Then I got the point that duplicating some data to speed up read action is a key point in firebase.

A typical design: the user's display name shall be duplicated in multiple locations, like in article list, in comment list, in follower list, or following list, etc.. I cannot imagine not to duplicate this piece of data but async retrieve them one by one from users node.

What if a user updates his display name? It seems that we need to update the value in all places, which is a pain in butt to maintain in long term, isn't it?

Xi Xiao
  • 953
  • 1
  • 12
  • 28
  • Whether it is a pain in the butt is opinionated. But to get the best mileage out of Firebase, you're likely going to have duplicated data. Here are the three ways I know of keeping the duplicates up to date: https://stackoverflow.com/questions/30693785/how-to-write-denormalized-data-in-firebase – Frank van Puffelen Jun 10 '17 at 20:08
  • 1
    Thanks @FrankvanPuffelen to point to your other answer. I am aware of the new atomic update API and it does ease my pain (surely opinionated). I was more in favor that if firebase can come up with an out-of-box json file to record the duplicate relationships, and when users update the master copy, all duplicates are updated automagically. – Xi Xiao Jun 10 '17 at 20:14
  • 1
    I totally agreee with @FrankvanPuffelen. But... if you a lot of duplication there may be better options. Or perhaps your data is overdenormalized (a new word I recently needed to use). Without understanding your use case, having a specific example of a 'pain in the butt', and seeing your Firebase structure I would hesitate to send you in any particular direction. Can you elaborate a bit? Perhaps include a snippet of your structure and explain what you are doing that's causing so many duplicates? – Jay Jun 11 '17 at 00:13
  • @Jay I know this is a bit dated at this point, but I can see this being an issue in the future. Let's say that a user updates their profile avatar. Each friend has a particular friend node, with their friend's information (such as user name, avatar, etc...). If the user has 100 friends, then we would have to update 100 different nodes just for this one "simple" update. – Jordan Benge Mar 10 '18 at 14:35
  • @JordanBenge Don't misunderstand my comment; I was agreeing that in *some cases* data needs to be duplicated. But in other cases it doesn't. Having a /users node that contains user data like an avatar or their favorite food is something that may not need to be duplicated so when the user changes their avatar it's changed in just one place. Any places where that avatar is needed would reference that particular /users/uid_x node. There wasn't (and still isn't) enough information in the question to really know what it being asked. Duplication is based on what you want to get out of Firebase. – Jay Mar 10 '18 at 15:24

0 Answers0