Realtime Database vs Cloud Firestore – Choosing the Right Firebase Database for Your App

5 min read
realtime-database-cloud-firestore-firebase-database

Realtime Database and Cloud Firestore are both commonly used, popular NoSQL databases, preferred for speed and efficiency. Both being firebase products, have lots of features in common including ease of set-up and firebase console for admin users.

Choosing between Realtime Database (RD) and Cloud Firestore (CF) is a tough decision indeed. You need to make a decision based on your specific needs and budget considerations. Both have their pros and cons that need to be analyzed.

While RD is old, tested, and already well-known in the market, CF is a new, improved product developed to offer some advantages over RD. However, if analyzed deeply, CF also has some specific limitations against RD that you cannot afford to ignore either.

So, here are some major points of difference between Realtime Database and Cloud Firestore that can help you decide between the two, for your project!

1. Data Model or Data Structure

While simple data is very easy to store in both Realtime Database and Cloud Firestore, there comes a prominent point of difference when you are dealing with complex data.

RD stores data as one large JSON tree. With this structure, complex hierarchical data gets harder to organize at scale.

CF stores data as a collection of documents. So, even complex, hierarchical data is easier to organize at scale using sub-collections within documents. It is more structured and utilizes nesting of objects. CF thus requires less data flattening or denormalization compared to RD.

2. Querying in the Database

With Realtime Database, you can either sort or filter in one query, not both at the same time. In short, you cannot make use of both filter and sort successfully in a single query. Moreover, queries in RD are deep by default and return the entire subtree as a result.

In comparison, CF supports indexed queries that are the default query type in CF. This facilitates compound sorting and filtering, which means you can combine filtering and sorting on a property in a single query. The queries are not deep and you can easily query for sub-collections within a document and the performance of the query is proportional to the size of your result set, not data set.

3. Writing the Data or Transactions

Realtime Database offers basic write operations and transactions where data can be written as individual operations. With RD, transactions are atomic only on a specific data subtree. Else, transactions in the native SDKs require a completion call-back. It is thus not automatic.

Cloud Firestore offers atomic (either all or nothing executes) write and transaction operations. Here operations can be batched and completed atomically. Transactions can read and write data atomically from any part of the database and so it will keep repeating automatically until completion.

4. Security Considerations

Realtime Database uses cascading rules language that separates authorization and validation. Thus, you need to validate data separately using the validate rule. The security provision is limited to the Firebase Database Rules.

Cloud Firestore has simple, flexible, and more powerful security for both mobile and web platforms with Cloud Firebase Security Rules (with flexible rules syntax), and for server SDKs with Identity and Access Management. It offers automatic data validation with non-cascading rules that combine authorization and validation. In short, the rules don’t cascade by default, without a wildcard. Though it offers very high security, the rules here can limit the queries. The entire query can fail if the results contain data that the user does not have access to.

5. Pricing Pattern

Realtime Database charges only for bandwidth and storage. But, the rates are higher compared to Cloud Firestore.

Cloud Firestore has much lower rates for network bandwidth and the amount of storage used compared to RD. It also charges for operations performed in the database to include the number of reads, writes, and deletes. Collectively, using CF is much cheaper than RD due to lower overall rates.

6. Reliability and Performance

Realtime Database is a single-region solution where databases are limited to zonal availability in a single region. It is tried and tested for years and offers extremely low latency. It is thus the appropriate option for frequent state-syncing.

Cloud Firestore is a multi-region solution that scales automatically. It offers global scalability and strong reliability as it distributes your data across multiple data centers in distinct regions. It is available in regional or multiregional configurations around the world.

7. Scalability – Concurrent Connections

Realtime Database lets you scale to around 200,000 concurrent connections and 1000 writes/sec in a single database. Beyond this, scaling requires sharding or database partitioning.

With Cloud Firestore, scaling is completely automatic and it allows scaling up to 1 million concurrent connections and 10,000 writes/sec.

8. Realtime and Offline Support

Both Realtime Database and Cloud Firestore have mobile-first, real-time SDKs and both support local data storage for offline-ready apps.

While the RD offers offline support for only mobile-platform apps for iOS and Android, CF provides offline support for mobile (iOS, Android) as well as web clients.

9. Import and Export of Data

Realtime Database leads the race when it comes to import and export of data. With RD, importing or exporting data across different databases is much simpler using a single JSON file.

In CF, import and export of data across the databases are not that easy. It poses a major challenge to the users. This is a major limitation of CF in comparison to RD that needs to be considered while choosing the right database for your project.

To Summarize

In short, both Realtime Database and Cloud Firestore are good and popular database options and you need to analyze your specific project needs while selecting the right database based on the pros and cons of RD and CF.

Before selecting the database for your project, you need to understand the data structure needed for your project, speed and scaling requirements, and the amount of data you need to store and retrieve. Though there are many added advantages of using CF over RD, you may even need the specific pros of RD over CF in your project. If you need to import and export data across databases with ease and it is possible to compromise on certain features lacking a bit in RD, you can rightly opt for it.

Most importantly, it commonly happens that projects require more than one type of database and it is easily possible to use multiple at a time. This way you can leverage the advantages of both and have a perfect database solution with multiple databases for the project!

Your development partners can solve the task of database selection for you. Contact Blue Whale Apps for all your development needs!