Wednesday, September 21, 2022

 Choose a database: Cloud Firestore or Realtime Database

https://firebase.google.com/docs/firestore/rtdb-vs-firestore


Choose a database: Cloud Firestore or Realtime Database

 

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing:

  • Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales further than the Realtime Database.

  • Realtime Database is Firebase's original database. It's an efficient, low-latency solution for mobile apps that require synced states across clients in realtime.

Your choice of database solution will depend on many factors, but when it comes to certain features, we can make recommendations about which database is right for you.

Both solutions offer:

  • Client-first SDKs, with no servers to deploy and maintain
  • Realtime updates
  • Free tier, then pay for what you use

Beyond great core features common to both databases, think about how any or all of the considerations listed below will affect the success of your apps.

Role of the database
My app uses a database for...
Primarily synchronizing data, with basic querying.
Advanced querying, sorting, and transactions.
Operations on data
My app's database usage looks like...
A few GBs or less of data that changes frequently.
Hundreds of GBs to TBs of data that is read much more often than it is changed.
Data model
I prefer to structure my data as...
A simple JSON tree.
Documents organized into collections.
Availability
My availability needs are...
Extremely high uptime guarantee of 99.999%.
A guarantee of at least 99.95% uptime.
Offline queries on local data
My app will need to perform queries on devices with limited or no connectivity...
Frequently.
Rarely or never.
Number of database instances
In my individual projects, I need to use...
Many databases, for example a database for each major customer.
Just a single database.

After thinking about the previous key considerations, you might be ready to choose a database. If you're still weighing pros and cons, this section covers other differences between Cloud Firestore and Realtime Database.

Both Realtime Database and Cloud Firestore are NoSQL Databases.

Realtime DatabaseCloud Firestore
Stores data as one large JSON tree.
  • Simple data is very easy to store.
  • Complex, hierarchical data is harder to organize at scale.

Learn more about the Realtime Database data model.

Stores data as collections of documents.
  • Simple data is easy to store in documents, which are very similar to JSON.
  • Complex, hierarchical data is easier to organize at scale, using subcollections within documents.
  • Requires less denormalization and data flattening.

Learn more about the Cloud Firestore data model.

Both have mobile-first, realtime SDKs and both support local data storage for offline-ready apps.

Realtime DatabaseCloud Firestore
Offline support for Apple Android clients.Offline support for Apple, Android, and web clients.

It can be useful to know when a client is online or offline. Firebase Realtime Database can record client connection status and provide updates every time the client's connection state changes.

Realtime DatabaseCloud Firestore
Presence supported.Not supported natively. You can leverage Realtime Database's support for presence by syncing Cloud Firestore and Realtime Database using Cloud Functions. See Build presence in Cloud Firestore.

Retrieve, sort, and filter data from either database through queries.

Realtime DatabaseCloud Firestore
Deep queries with limited sorting and filtering functionality.
  • Queries can sort or filter on a property, but not both.
  • Queries are deep by default: they always return the entire subtree.
  • Queries can access data at any granularity, down to individual leaf-node values in the JSON tree.
  • Queries do not require an index; however the performance of certain queries degrades as your data set grows.
Indexed queries with compound sorting and filtering.
  • You can chain filters and combine filtering and sorting on a property in a single query.
  • Queries are shallow: they only return documents in a particular collection or collection group and do not return subcollection data.
  • Queries must always return whole documents.
  • Queries are indexed by default: Query performance is proportional to the size of your result set, not your data set.
Realtime DatabaseCloud Firestore
Basic write and transaction operations.Advanced write and transaction operations.
  • Write data operations through set and update operations as well as advanced transformations such as array and numeric operators.
  • Transactions can atomically read and write data from any part of the database.
Realtime DatabaseCloud Firestore
Realtime Database is a regional solution.
  • Available in regional configurations. Databases are limited to zonal availability within a region.
  • Extremely low latency, ideal option for frequent state-syncing.
Read more about Realtime Database performance and reliability characteristics in the Service Level Agreement.
Cloud Firestore is a regional and multi-region solution that scales automatically.
  • Houses your data across multiple data centers in distinct regions, ensuring global scalability and strong reliability.
  • Available in regional or multi-regional configurations around the world.
Read more about Cloud Firestore performance and reliability characteristics in the Service Level Agreement.
Realtime DatabaseCloud Firestore
Scaling requires sharding.
  • Scale to around 200,000 concurrent connections and 1,000 writes/second in a single database. Scaling beyond that requires sharding your data across multiple databases.
  • No local limits on write rates to individual pieces of data.
Scaling is automatic.
  • Scales completely automatically. Currently, scaling limits are around 1 million concurrent connections and 10,000 writes/second. We plan to increase these limits in the future.
  • Has limits on write rates to individual documents or indexes.
Realtime DatabaseCloud Firestore
Cascading rules language that separates authorization and validation.Non-cascading rules that combine authorization and validation.
  • Reads and writes from mobile SDKs secured by Cloud Firestore Security Rules.
  • Reads and writes from server SDKs secured by Identity and Access Management (IAM).
  • Rules don't cascade unless you use a wildcard.
  • Rules can constrain queries: If a query's results might contain data the user doesn't have access to, the entire query fails.

Both solutions are available on the Spark and Blaze pricing plans.

Realtime DatabaseCloud Firestore
Charges only for bandwidth and storage, but at a higher rate.

Read more about Realtime Database pricing plans.

Charges primarily on operations performed in your database (read, write, delete) and, at a lower rate, bandwidth and storage.

Cloud Firestore supports daily spending limits for App Engine projects, to make sure you don't go over the costs you're comfortable with.

Read more about Cloud Firestore pricing plans.

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. Keep in mind the differences outlined above if you decide to use both databases in your app.

Learn more about the features available in both Realtime Database and Cloud Firestore.

Hopefully this comparison has helped you settle on a Firebase database solution. Now you can learn how to add a database to your Firebase projects.

No comments:

Post a Comment

So sánh các GitFlow model và áp dụng với CICD

https://medium.com/oho-software/so-s%C3%A1nh-c%C3%A1c-gitflow-model-v%C3%A0-%C3%A1p-d%E1%BB%A5ng-v%E1%BB%9Bi-cicd-b6581cfc893a