Google-Firebase has recently launched Cloud Firestore. In this tutorial, we are going to look at what is Firestore in detail and how is it different from Firebase Real-time Database both technically and from the pricing perspective.
This tutorial is part of the series of tutorials that I’m going to write on this topic. After this, I’m going to write about how to make Simple Note Taking app using Cloud Firestore and Android.
What is Cloud Firestore?
First of all, the Firestore is the Google’s new database and a great addition to Realtime database that you might already know.
It is also a NoSQL based database that allows you to store and sync data in Realtime just like Firebase Realtime database. It offers Event Listeners just like Real-time database and supports offline mode for both Mobile and Web. The Firebase Real-time database only does Offline Support for Mobile Apps only. This makes it possible develop responsive apps that work regardless of network latency or internet connectivity.
The Cloud Firestore offers Native SDKs for all major platforms like Android, iOS, and Web which makes it super easy to make Android, iOS, and Web Developers.
Realtime Database vs Cloud Firebase
Cloud Firestore is made by keeping problems faced by developers in mind while developing apps using Firebase Real-time Database which is sometimes hard in terms of organizing data through nodes and especially in querying data through nodes. In Firebase Realtime database you cannot chain multiple conditions together in the query, instead, you only have the option to query based on one field only. Let’s look into Cloud Firestore features further and see how it resolves these common problems
First of all the Cloud Firestore data models support flexible and hierarchical data structures so you store your data in the documents organized into collections.
Much Better Querying and Structure
As compared to Realtime database which is JSON-Tree based, the Cloud Firestore is the document-model based database which can store strings, floats, and as key-value pairs called maps. The documents are grouped into Collections.
One Firebase Project can contain multiple Collections of Document and each document can point to sub-collectiosn of documents etc.
The greatest strength of the Cloud Firestore on Realtime database is the Expressive Querying which allows you to query specific document or you can use these queries to get all the documents in a collection that matches query parameters. Your queries can include multiple chained filters and combined filtering and sorting. You can use operators like >, <, == and chain them to create complex queries after creating indexes on the fields.
In Firebase Realtime database there are several limitations in querying the data and sometime to make the database efficient you have to denormalize the data.
Another great thing is Realtime updates, so like Realtime database from Firebase, Cloud Firestore uses synchronization to update data on any connected device. However, It can also perform simple one-time fetch queries very efficiently.
Just like Firebase Realtime database, Cloud Firestore also supports offline synchronization so that you can do writes and query data even when the device is not connected to the internet. It basically caches the data when you do the query for the first time. And when the devices become online the Cloud Firestore synchronizes the offline changes automatically. All of that happens automatically so, it’s really really cool. The key difference here is that Cloud Firebase supports offline capabilities even on Web which makes it interesting and cool. Isn’t that amazing?
In Firebase Realtime database, the things can become difficult to handle when the dataset is massive. Also, Firebase Realtime database can only handle 100,000 simultaneous users in its Spark and Blaze plan and will stop working when there are more users then this number. This limit was previously 10,000 simultaneous users and you had to contact Firebase Support if you ever wanted to increase the limit. Definitely, if you have more users than this then the Firebase Realtime database is not for you. Cloud Firestore, on the other hand, is more scalable because it uses dynamic pricing based on reads, writes, and bandwidth.
The advantage of the new querying structure is that it scales at the size of your result set, and not based on the size of your data. In simple words, it means that the query of Top 10 hotels will take the same amount whether you have 1000, 10,000 or 10 million records in the database.
Cloud Firestore is the multi-region database which means that your data is automatically copied to multiple geographically separate regions. It gives better accessibility of the database and at the same time makes the database safe by saving the data in multiple data centers.
Firebase Realtime database is determined based on outgoing bandwidth which means that you are charged based on how much data is downloaded (Blaze Plan). On the other hand, Cloud Firestore charges you based on writes, reads, and bandwidth. While keeping the Blaze Plan in mind, I did basic calculation to find out how many writes you will get for 1 Dollar and found that you will get approximately 5,555,556 writes which are definitely a lot. You can read more about Cloud Firestore Pricing here.
How and where Realtime Database is still better Option?
By reading the above sections, you might be thinking that Cloud Firestore is the better option, but no. Still, in certain situations, Firebase Realtime database is better option.
- As reported by Firebase Team, The Firebase Realtime Database has better latency, it’s not much but it is a couple hundred milliseconds difference which might be important for your app.
- Firebase Realtime Database provides the feature to check the presence of the user by default, which means that you can check whether a user is online or offline but native support in the SDKs. There is a solution available for Cloud Firebase but it is not as elegant as Firebase Realtime Database. It’s very important in cases when you are developing Realtime chatting based apps where the presence of the users is important.
- The apps that do a lot of reads and writes can be costly than the Realtime database which charges only for outgoing bandwidth.
- Cloud Firestore is still in beta which means that there can be problems till it does not available in production. On the other hand, Realtime Database is available from a long time and is used by a large number of devices.
Which one you should use? Cloud Firestore or Realtime Database?
Just read the above points and see which one suits you better. I suggest going with Cloud Firestore if you are starting a new project and if querying and scaling are important for you and Realtime Database if you app downloads a-lot of data.
How to get started with Cloud Firestore?
If you want to start then you can follow the official documentation provided by Google-Firebase, Checkout Cloud Firebase sample apps for Android, iOS, and Web and try the interactive code lab. I am very excited about it and in future tutorials I am going to write a small note taking app using Android Studio and Cloud Firestore, so stay tuned 🙂
I am personally very excited about Cloud Firestore because there were many things which I had difficulty in implementing while using Realtime Database and Cloud Firestore has overcame those issues.
In future tutorials (which I am going to write on Weekly bases hopefully) I am going to further explore Cloud Firestore, Android and other things for which I have some knowledge, so subscribe to my newsletter if you want to get my thoughts straight into your Inbox.