Bo Z
Bo Z

Reputation: 2607

What is better solution for online/offline user feature firestore or realtime?

I got a database of users. Need to implement list which shows only online users. Unfortunately realtime does not filter by key. So it is possible only in Firestore. But Firestore charge for each write/read/update operation so it might be very expensive to use that feature since users are going offline/online all the time and there will be a lot of write/read/update operations. As a solution, I did separate tables in realtime which saves only online users and here I don't need to pay for write/read/update. But at the same time, it is duplicating the database which is also not good practice.

Could you please share your thought on which solution is better from "price" and "clean" point of view. To pay for Firestore and use one db, or use two db but not to pay for each operation?

Upvotes: 2

Views: 318

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

What is better solution for online/offline user feature Firestore or realtime?

It depends on what your requirements are, but you should not consider using one or the other. There is nothing wrong with using both.

Unfortunately realtime does not filter by key.

If you check the docs, there is a section named sorting and filtering data, where you'll find a method named orderByKey():

Order results by child keys.

So actually it is possible to filter the results of a query and order them in the same time.

As a solution I did the separate tables in realtime which saves only online users and here I don't need to pay for write/read/update.

That's a good solution you can go ahead with. Actually, both Cloud Firestore and Firebase Realtime Database work very well together.

But at the same time, it is duplicating the database which is also not good practice.

Oh, it is. This practice is called denormalization and is a common practice when it comes to Firebase. For a better understanding, I recommend you see this video, Denormalization is normal with the Firebase Database and check my answer from the following post:

So you can denormalize the data to obtain queries that are actually not possible or to save some write operation when it comes to Cloud Firestore.

Could you please share your thought on which solution is better from "price" and "clean" point of view. To pay for Firestore and use one db, or use two db but not to pay for each operation?

Use them both.

Upvotes: 1

GrahamD
GrahamD

Reputation: 3165

You can perform filtering in FB RTDB, it is not as sophisticated or flexible as Firestore (you can only filter on one key or value at once) but if it is as trivial as your example then it is certainly possible (you need to use indexing to improve performance and limit data download when in production).

On a db structure like this (ignore the nodes below 'root' that are not 'users', I use this 'root' node for multiple code tests during my development):

enter image description here

and using this code (in Flutter/Dart but the same parameters are available in other languages):

    _referenceRoot
    .child("root")
    .child("users")
    .orderByChild("gender")
    .equalTo("male")
    .once()
    .then(
  (DataSnapshot snapshot) {
    print("snapshot.value: ${snapshot.value}");
    if (snapshot.value != null) {
    print("snapshot.key: ${snapshot.key}");
    }
  },
);

I get this output ie.only the male users. Note that in the snapshot the selected users are unordered:

I/flutter ( 4307): snapshot.value: {a2: {hair: brown, gender: male, age: 20}, a3: {hair: brown, gender: male, age: 20}, a4: {hair: brown, gender: male, age: 20}, a6: {hair: brown, gender: male, age: 20}, a8: {hair: brown, gender: male, age: 20}, a9: {hair: brown, gender: male, age: 20}, a0: {hair: brown, gender: male, age: 20}} I/flutter ( 4307): snapshot.key: users

See this for more details of what is possible: https://firebase.google.com/docs/database/rest/retrieve-data

So, to answer your specific question, if the filtering you need to do is trivial, I would use only FB RTDB. If more complex, then will have to be Firestore. You would need to do the math on estimated data storage/download volumes vs. db calls and storage for a view on minimising costs.

Upvotes: 1

Related Questions