hamburger icon close icon
Google Cloud Database

Cloud Firestore: An In-Depth Look

What is Cloud Firestore?

Cloud Firestore is a database for web and mobile applications, often chosen for its flexibility and scalability. Cloud Firestore leverages the technologies of Firebase technologies with the Google Cloud Platform. It performs real-time synchronization of data between client applications using event listeners. Additionally, Cloud Firestore provides offline support, letting you build applications that can run regardless of latency or connectivity.

Cloud Firestore integrates seamlessly with other Firebase services, Google Cloud Platform databases and other services, including Cloud Functions.

Cloud Firestore features include:

  • Flexible data model—supports flexible hierarchical data structures. Lets you store data in documents, which you can further organize into collections and sub-collections. Documents can support complex nested objects.
  • Complex queries—enable you to retrieve a specific single document or all documents in a collection, as long as it matches the query parameters. Additionally, you can combine sorting and filtering, as well as include multiple filters in one query.
  • Data sync—updates data automatically on each connected device. The sync operation is designed to efficiently run simple one-time queries.
  • Caching—caches application data, allowing your application to write, read, monitor, and perform data query offline. This mechanism syncs local changes with Cloud Firestore once the system comes back online.

In this article, you will learn:

How Does Cloud Firestore Work?

Cloud Firestore is a cloud-based NoSQL database that provides direct access to web, iOS and Android applications through native SDKs. Cloud Firestore supports Python, Node.js, Unity, Java, C++, REST, Go's native SDK, and RPC APIs.

Cloud Firestore’s data model lets you store data in documents, which contain fields corresponding to values. A Cloud Firestore document is stored in a collection, which serves as a document container for organizing data and creating queries.

Documents support a wide variety of data types, including simple strings and numbers and all the way to complex nested objects. You can even create a hierarchical data structure that evolves as your database grows, by creating sub-collections on your documents.

Additionally, Cloud Firestore queries are highly expressive and flexible, letting you:

  • Use flat queries to fetch document level data without fetching the entire collection or nested subcollections.
  • Manipulate results by adding sort, filter or limit operators
  • Add cursors to paginate results
  • Add real-time listeners to your application, to take a snapshot when data in the client application changes, and retrieve only the new changes rather than the entire database

To learn about SQL-based databases on Google Cloud, see our guides to:

Cloud Firestore vs Realtime Database

The Firebase platform lets you choose between two architectures and data models: Cloud Firestore and Realtime Database. Both are database products created by Firebase and can be easily added to your project with just a few setup steps. Both are closely integrated with other Firebase services.

Cloud Firestore

Real Time Database

Cloud-hosted

Cloud-hosted

Uses collections of documents to store data. Can store data similar to tables and rows.

Only stores key-value pairs using JSON documents.

SDK support for Android, iOS and web, also available for native Node.js, Java, Python, Unity, C++, Go, and offers REST and RPC APIs.

SDK support for iOS, Android, and web. Requires handling most of the logic in client applications and duplicating code across platforms.

Data is typed, including booleans, objects, arrays, numbers, strings, null values, geopoints, timestamps, and references to documents.

Does not use data types. Only supports nested objects and arrays, which are objects with indices set as keys.

 

Best Practices for Cloud Firestore

Here are some best practices for getting the most out of your Cloud Firestore database.

Database Location

When creating a database instance, choose a database location that is closest to your user and compute resources on Google Cloud Platform. Remote network hops are error prone and request latency can be high.

There are two primary options, trading off availability and latency:

  • For maximum application availability and durability, choose a multi-regional location in Google Cloud and place compute resources critical to your application in more than one region.
  • For lower latency, and also lower costs, choose a local region. This reduces lower latency for latency sensitive applications and also enables sharing it at low cost with other resources in the same Google Cloud region.

Gradually Increasing Traffic

Give Cloud Firestore time to prepare documents for higher traffic. To do this, gradually increase traffic to new collections or documents that are close in terms of lexicographic order.

Start with up to 500 operations per second for a new collection, then increase traffic by 50% every five minutes. This is called the "500/50/5" rule. Make sure work is distributed evenly along the range of values of the document key.

You can use this procedure to scale up read traffic to 740,000 operations within 90 minutes. When ramping up write traffic, consult Cloud Firestore’s limits.

Indexes

  • The number of indexes should remain low. Too many indexes create a jump in write latency. You should also note that storing index items incurs costs in Firestore.
  • Fields indexed with monotonically increasing values, such as timestamps, can cause delays in applications with high reads and writes.

Read and Write Operations

  • Avoid writing to the same document several times per second.
  • If possible, use asynchronous calls instead of synchronous calls, to minimize latency impact. For example, if you need to run a document lookup and a query, and there is no data dependency between them, run them both asynchronously and in parallel.
  • Always prefer cursors to offsets. Offsets skip documents in the results returned to the application, but still retrieves them, which impacts query latency, and also incurs additional billing for the retrieved documents.

Cloud Firestore with NetApp Cloud Volumes ONTAP

NetApp Cloud Volumes ONTAP, the leading enterprise-grade storage management solution, delivers secure, proven storage management services on AWS, Azure and Google Cloud. Cloud Volumes ONTAP supports up to a capacity of 368TB, and supports various use cases such as file services, databases, DevOps or any other enterprise workload, with a strong set of features including high availability, data protection, storage efficiencies, Kubernetes integration, and more.

In particular, Cloud Volumes ONTAP helps in addressing database workloads challenges in the cloud, and filling the gap between your cloud-based database capabilities and the public cloud resources it runs on.

New call-to-action
Yifat Perry, Technical Content Manager

Technical Content Manager