0

Firebase documentation article Structure Your Database section Best practices for data structure says "Avoid nesting data".

What are the benefits for performance and maintenance to have several DBs vs a flat structure, or they will work same in average?

In SQL we create tables and then set relationships between them. In a NoSQL I can create several DBs and imitate that.

For example in a flat structure:

{
  "chatsId": [
    "some-chat-id-1",
    "some-chat-id-2",
    ...
  ],
  "chatsInfo": {
    "some-chat-id-1": {
      "between": [
         "some-user-id-1",
         "some-user-id-2"
       ],
       messages: "some-messages-id-1"
    },
    "some-chat-id-2": {
      "between": [
         "some-user-id-3",
         "some-user-id-4"
       ],
       messages: "some-messages-id-2"
    },
    ...
  },
  "messages": {
    "some-messages-id-1": [
      {
        ... some message data 1
      },
      {
        ... some message data 2
      },
      ...
    ],
    "some-messages-id-2": [
      {
        ... some message data 1
      },
      {
        ... some message data 2
      },
      ...
    ]
  },
}

But with multiple databases:

ChatsDB

{
  "chatsId": [
    "some-chat-id-1",
    "some-chat-id-2",
    ...
  ]
}

ChatsInfoDB

{
  "chatsInfo": {
    "some-chat-id-1": {
      "between": [
         "some-user-id-1",
         "some-user-id-2"
       ],
       messages: "some-messages-id-1"
    },
    "some-chat-id-2": {
      "between": [
         "some-user-id-3",
         "some-user-id-4"
       ],
       messages: "some-messages-id-2"
    },
    ...
  }
}

MessagesDB

{
  "messages": {
    "some-messages-id-1": [
      {
        ... some message data 1
      },
      {
        ... some message data 2
      },
      ...
    ],
    "some-messages-id-2": [
      {
        ... some message data 1
      },
      {
        ... some message data 2
      },
      ...
    ]
  }
}
7
  • What is your 1 specific researched non-duplicate question? How are you stuck answering yourself? Yes or no questions are seldom useful & (since they ask for a yes or a no) are seldom asking for what the asker actually wants answered. It is unhelpful & unclear to ask 'A or B' when A & B are not mutually exclusive and/or not the only options. (And you may think they are when they are not.) Strategy for “Which is better” questions Commented Dec 30, 2023 at 1:31
  • @philipxy Thnak you for edits, that are great! But I didn't understand you clearly. I mentioned which criterias I'm interested in. I'm stucked because I'm not DB architect so I don't know in which scenarios which option is better. I can't ask about third, forth ... options because I just don't know them because (as I mentioned but you removed it) I'm frontend developer so I don't know anything about DB architecture and it is my first time. When I tried to google this question, almost all questions was about "nested data vs flat data" Commented Dec 30, 2023 at 2:01
  • Your question is too general & vague & wide in scope & is unclearly an OR of parts (which merit closing) & shows no research effort at answering what you are asking (meriting downvotes) & even if you gave enough specific criteria & specifically how to balance them & how you were stuck deciding, ultimately this comes down to asking for an opinion, which is off-topic. Not all questions are on-topic. "What is your 1 specific researched non-duplicate question?" Please clarify via edits, not comments. Please delete & flag obsolete comments. tour How to Ask Help center Commented Dec 30, 2023 at 2:15
  • PS Look at the "answer" post: "it's hard to say anything conclusive". A "question" post evoking that response is not useful & an "answer" post containing is no answer & shouldn't be posted. Commented Dec 30, 2023 at 2:16
  • 1
    You should really go through the getting started guide as well - it's very helpful and answer a lot of questions. I always suggest starting with a very basic "To-Do" type application to get the feel for the SDK and working with an online first database. Commented Dec 30, 2023 at 20:55

1 Answer 1

2

You should not use multiple databases for splitting the data like that. Just use a single database and model within that.

Honestly, aside from that it's hard to say anything conclusive, as the best data model for a NoSQL database depends mostly on the use-cases of your app. I often explain this as using a data model that closely matches what is shown in the screens of your app.

Start with a simple model for your first few use-cases, then evolve that as you add more use-cases that need additional data. Your initial model seems fine for a first stab, although I'd probably change the chatsId array to a chatsId map with the same keys and true as their value. For more on this, see Best Practices: Arrays in Firebase.

To learn more about data modeling, I recommend reading NoSQL data modeling, watching Firebase for SQL developers, and probably also Get to know Cloud Firestore (which is for Firebase's other database, but has lots of great explanation of how to think of NoSQL data modeling too).

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please give a simple scenario when I need more than 1 DB in 1 application?
If you have more than 200k concurrent users, or have a load that is more than can be handled by one database. In those cases you'll want to shard the information in the way that is documented: firebase.google.com/docs/database/usage/sharding. While it's good to consider a sharding strategy early on, it's probably best to wait until you have some experience with NoSQL data modeling and Realtime Database performance before doign so. In general, chat applications are not the hardest to scale - as they have natural shards in "chat rooms".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.