Firebase Cloud Firestore v/s Firebase Realtime Database

Rahul vyas
4 min readOct 10, 2017

--

Firebase is a powerful backend service that provides many features like Realtime Database, Cloud Storage, User Authentication, Static Hosting, and many more. In 2017, Firebase introduced a new feature called “Cloud Firestore” which provides a new way to store and manage data. In this post, we will compare Firebase Realtime Database and Firebase Cloud Firestore to help you decide which one is better suited for your needs.

Firebase Realtime Database and Firebase Cloud Firestore are both NoSQL document databases provided by Firebase that offer real-time data synchronization and offline data access. However, there are some key differences between them:

Data Structure:

Realtime Database stored data in JSON tree but Cloud firestore stored data in documents which is very similar to JSON.

In the Realtime Database, Complex, hierarchical data is harder to organize at scale but in Cloud FireStore, Complex, hierarchical data is easier to organize at scale, using subcollections within documents.

In Cloud FireStore, Documents can contain subcollections and nested objects(like ‘phone’ in above figure), both of which can include primitive fields like strings(like ‘name’,’email’ and etc. in above figure) or complex objects like lists.

Firestore offers a more structured way of organizing data, which makes it easier to query and scale for complex data structures.

Querying:

  • Firebase Realtime Database offers limited querying capabilities, such as ordering by a single child key or filtering by a single child value. On the other hand, Firebase Cloud Firestore offers more advanced querying options such as range, compound, and pagination queries.
  • Firestore supports indexing to improve query performance.
  • In the Realtime database, We can only sort or filter on a property in a single query, not both sort and filter on a property while In the Cloud FireStore, You can chain filters and combine filtering and sorting on a property in a single query.
  • If you want to fetch data in descending order then Cloud fireStore is very useful for you but in the Realtime database, There is no available any query for it.
  • You can also chain multiple “where” methods to create more specific queries (logical AND) in Cloud FireStore.
Compound queries

Scalability

Firebase Realtime Database can have performance issues with large datasets, as it fetches the entire tree for every query. Firestore is designed to scale better, as queries only fetch the required data and can be limited to a subset of the collection or document.

It’s important to note that Your query performance is proportional to the size of your result set, not your data set. So searching will remain fast no matter how large your data set might become.

Manual fetching of data

We can listen data realtime in the Cloud FireStore like Realtime database but in the Cloud FireStore, We can also fetch data manualy(If you want to any data only one time).

Pricing

  • Firebase Realtime Database charges based on bandwidth and storage usage, while Firestore charges based on the number of operations performed and the amount of data stored.
  • When you use Cloud Firestore, you are charged for the following: 1) The number of reads, writes, and deletes that you perform. 2) The amount of storage that your database uses, including overhead for metadata and indexes. 3) The amount of network bandwidth that you use.
  • In the Cloud FireStore, When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed.
  • In the Cloud FireStore, Charges is very low than Realtime Database.

Security

  • Firebase Realtime Database has basic security rules that must be defined explicitly, while Firestore has more granular security rules that can be defined at the document and collection level.
  • In the Realtime Database, We need to validate data separately using the validate rule but in the Cloud FireStore, data validation happens automatically.
  • Cloud FireStore has simple and more powerful security for mobile, web, and server SDKs than Realtime Database.

Writing the data

We can execute multiple operations as a single batch and complete them atomically , with any combination of the set(), update(), or delete() methods.

Batch opertaion

You can use both databases within the same Firebase app or project. Both NoSQL databases can store the same types of data and the client libraries work in a similar manner.

Conclusion:

Overall, Firebase Cloud Firestore is a more powerful and flexible option for organizing and querying data, while Firebase Realtime Database may be more suitable for simple applications that require real-time updates.

Both Firebase Realtime Database and Firebase Cloud Firestore have their own strengths and weaknesses. If you need a real-time database with limited querying capabilities and are working with a smaller dataset, then Firebase Realtime Database might be a good fit. However, if you need a more structured database with better querying capabilities and scalability, then Firebase Cloud Firestore is the way to go. Ultimately, the decision depends on the specific needs of your application.

--

--