0

In the Firebase official guides,

// An index to track Ada's memberships
{
  "users": {
    "alovelace": {
      "name": "Ada Lovelace",
      // Index Ada's groups in her profile
      "groups": {
         // the value here doesn't matter, just that the key exists
         "techpioneers": true,
         "womentechmakers": true
      }
    },
    ...
  },
  "groups": {
    "techpioneers": {
      "name": "Historical Tech Pioneers",
      "members": {
        "alovelace": true,
        "ghopper": true,
        "eclarke": true
      }
    },
    ...
  }
}

This is a necessary redundancy for two-way relationships.

If I want to develop an App with that model, when a user joins a new group, does that client application make 2 requests to my Firebase for adding 2 record?

If something goes wrong in the middle of operations. What should I do? Maybe I need a backend server that periodically check data conflicts?

I can't find any further elaboration on this data redundancy issue on Firebase documentation or in articles about NoSQL (maybe I was using wrong keywords!).

Furthermore, what is the difference between indexing my data and the above approach?

Community
  • 1
  • 1
pk028382
  • 95
  • 1
  • 10

2 Answers2

0

For you specific question about conflicts, you'll want to use multi-location updates to prevent such conflicts. See the blog post introducing multi-location updates, the follow-up post on client-side fan-out, and my answer on strategies for updating dulplicated data.

You seem new to the Firebase Database, in which case it indeed can be quite unsettling to see patterns like data duplication and client-side joins. I recommend reading NoSQL data modeling for a general introduction to NoSQL and then watch Firebase for SQL developers for an intro to Firebase.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

There is a great series of videos in which is explained that Firebase-Database as a nosql provide advantages as speed and data flexibility but also make the developer's responsibility to maintain data coherence.

So what you want is the opposite of what you need, you want data normalization but actually, you need data denormalization. Your data structure will reflect the content that the user is consuming. Your data structure is equivalent to your information architecture.

As Frank van Puffelen explained you can use multi-updates using the ´updateChildren()´ method.

Also, you can use Firebase Functions as a server-side way to sustain business logic. In the long run clients and servers have to balance responsibilities, even in a REST application.

cutiko
  • 9,887
  • 3
  • 45
  • 59