Firebase Database: Should You Choose Realtime DB or Cloud Firestore?
Firebase is a NoSQL cloud service, offered as part of the Google Cloud Platform database offerings. It is based on a document-model and can be used to store and sync data in real-time with horizontal scaling. You can use Firebase for multi-user applications, such as mobile apps, serverless applications, and offline applications.
Beyond standard NoSQL functionality, Firebase includes features for authentication, crash reporting, messaging, performance monitoring, and analytics. Firebase is accessible through SDK and API, with support for both Unity and C++. You can integrate it with a wide variety of services and utilities, including Kubernetes deployments, BigQuery, Google Marketing Platform, Data Studio, JIRA, and Slack.
This article will explain the differences between Firestore vs Real Time Database.
Contents
Firebase Database Types
In the Firebase service, you have two architectures and data models to choose from — Realtime Database and Cloud Firestore.
Realtime Database
Realtime Database is a database that uses JSON documents to store key-value pairs. It includes features for data synchronization using web sockets and asynchronous syncing for offline device support. The database SDK offers support for web, Android, and iOS applications.
When using Realtime Database you are required to write application code on the client you are using and handle logic in the client app. This means you have to duplicate code for applications on multiple platforms. Alternatively, you can use Firebase Cloud Functions to handle requests but this affects your ability to use many SDK features.
Additionally, when writing your client logic, you are responsible for data validation. Realtime Database does not restrict you by data type, so it is up to you to enforce typing as needed for your application logic.
Cloud Firestore
Cloud Firestore is a newer database version that uses collections of documents to store data. Each document in a collection can hold subcollections or data fields. This structure enables you to store data similar to tables and rows, expanding the database’s compatibility.
In Cloud Firestore, data is typed. Available types include booleans, objects, arrays, numbers, strings, and null values. You also have access to geopoints, timestamps, and shallow references to documents. These types enable you to manage data integrity and hunt type-based bugs more easily.
When defining your documents, you can use references to avoid data denormalization by reducing the number of duplicate copies needed. Additionally, although you can’t query across collections, you can use references to fetch local copies of data.
Realtime Database vs Cloud Firestore: Which Should You Choose?
When selecting between the Firebase options, keep the following factors in mind. Additionally, keep in mind that both are available on a free tier so it is possible to demo both options.
Keep reading to know more about the differences between the Real Time Database vs Firestore.
Data Model or Data Structure
In Realtime Database, you are only able to store data as a document tree. This makes it difficult to organize data, especially at scale. Because of this, Realtime Database is best for simple data.
In contrast, Cloud Firestore collections make it relatively easy to store hierarchical data. These collections enable you to nest objects and require less denormalization than Realtime Database. This makes Cloud Firestore better suited for complex data. For example, you can use it to more easily store and manage infrastructure as code for your DevOps deployments.
Querying in the Database
Queries in Realtime Database are limited to one operation at a time. You can either filter or sort but not both in a single query. Additionally, any queries made return your entire subtree because queries are deep by default. This can make querying slow.
In Cloud Firestore, you can perform indexed queries that enable you to perform compound filtering and sorting. This lets you query for subcollections. It also often means that query performance is better since it is based on result set rather than data set size.
Writing the Data or Transactions
In Realtime Database you can perform write operations individually. Transactions are atomic but only in specific subtrees. Outside of the subtree, you need to configure completion call-backs through the SDK.
With Cloud Firestore you can use both atomic transactions and write operations. You can batch operations and transactions are allowed from any part of your database. This means you do not need to use completion call-backs, the transaction will automatically repeat until successful.
Security Considerations
Authorization and validation are defined through cascading rules in Realtime Database. This limitation requires you to define rules for validation and authorization individually. Additionally, your security provision is limited to the Firebase Security rules you create.
With Cloud Firestore, you can use both Firebase Security rules and Identity and Access Management through server SDKs. Firebase also includes automatic data validation without cascading, although you can enforce cascading manually. The downside of this is that queries may fail if the users don’t have access to all results data.
Reliability and Performance
Realtime Databases are limited to a single region of availability. This offers very low-latency and is best for frequent state-syncing. Additionally, to scale in Realtime Database, you need to implement sharding.
In contrast, you can use Cloud Firestore in multiple regions with automatic global scalability. You have the option to use it regionally or multi-regionally with data automatically duplicated for availability.
Other differences between both databases relate to:
- Multi-region deployment
- Pricing structure
- Scalability
To know more about these differences, please read the article Firebase Databases Explained.
Conclusion
Firebase provides two database services that are the Realtime Database and Cloud Firestore. This article explained the differences between Cloud Firestore vs the Real Time Database.
The Realtime Database uses JSON documents to store key-value pairs, synchronize data via WebSockets, and work for Android, iOS, and web apps.
Cloud Firestore uses collections of documents to store data, each document in a collection can hold subcollections, and the database also delivers realtime updates.
Looking for Alternatives to Firebase database?
If so, please checkout Back4app. It delivers a scalable relational database service. The product is open-source, allows complex queries, and makes the data accessible via REST or GraphQL APIs.
General FAQ
What are the Firebase database options?
– The Realtime Database
– Cloud Firestore
What are the differences between Firebase databases?
– Data Model or Data Structure
– Querying in the Database
– Writing the Data or Transactions
– Security Considerations
– Reliability and Performance
What kind of database is Firebase?
Firebase is a NoSQL database.