Firestore
Firestore is Google's fully managed serverless NoSQL document database. Choose it for mobile and web applications that need real-time updates, offline sync, and a flexible document schema. Documents are organized into collections. Every field is automatically indexed. Firestore scales from zero to millions of documents with no capacity planning.
Native Mode vs Datastore Mode
Native mode is the current model, supporting real-time listeners, offline sync for mobile clients, and a richer query model. Always use Native mode for new applications. Datastore mode maintains backward compatibility with the legacy Cloud Datastore service and lacks Native mode capabilities.
Data Model
Collections hold documents; documents hold fields and can nest subcollections. Queries are shallow by default — a query on one collection does not scan its subcollections.
Collections contain documents. Documents contain key-value fields, nested objects, and arrays. Documents can have subcollections for hierarchical data. Documents are limited to 1 MiB and a maximum of 40,000 index entries each. In Firestore, store data in the shape it will be read — duplicating some fields across documents is expected and normal.
Consistency and Transactions
Firestore provides strong consistency: reads always return the most recently written data. Multi-document ACID transactions are supported; a transaction must complete within 270 seconds, and its total size is bounded by a 10 MiB request limit rather than a fixed write count.
Queries and Indexing
Firestore indexes every field automatically — single-field queries work without any setup. Queries that filter or order on multiple fields need a composite index that you define in a configuration file and deploy with the application. Without the matching composite index, the query fails at runtime. Firestore does not support full-text search.
Real-Time Listeners
Native mode Firestore pushes document and query changes to connected clients as they happen. Each update generates a billed read operation. Design data models to minimize documents changing per user action.
- Designing data with a normalized relational schema. Firestore is fastest when you store data in the shape it will be read, even if that means duplicating some fields across documents.
- Hitting the 1 MB document limit from unbounded arrays.
- Not planning composite indexes until queries fail in production.
- Using real-time listeners for extremely high-frequency updates.
- Using Firestore for workloads needing SQL joins or analytical aggregations.
- Model data around access patterns.
- Keep documents well under 1 MB.
- Define composite indexes in code and deploy with the application.
- Test security rules with the emulator before deploying.
- Export to BigQuery for analytical queries.
Knowledge Check
What happens if you run a Firestore query that filters on multiple fields but the required composite index does not exist?
- Firestore falls back to a full collection scan and still returns the results, but much more slowly than an indexed query would
- Firestore automatically builds and deploys the required composite index the first time that query is executed for you
- The query fails at runtime with an error naming the composite index that has to be created and deployed before it will run
- The query returns partial results limited to single-field matches, and it silently drops every other filter you gave it
Which Firestore mode should you always use for new applications?
- Datastore mode — supports more complex queries than Native mode, including arbitrary joins
- Legacy mode — it provides backward compatibility with the older Firebase SDKs only
- Native mode — it supports real-time listeners, offline client sync, and a considerably richer query model
- Hybrid mode — it combines real-time listeners with SQL-style joins across collections
What is the maximum size of a single Firestore document?
- 10 MB — large enough for most documents, including small embedded media files
- 100 KB — documents are hard-capped this small for performance reasons
- 1 MB — an unbounded array inside a single document can easily push past this limit
- 5 MB — the same as the limit on a single Cloud Storage object that is embedded in a document
How does Firestore's consistency model work?
- Eventual consistency — reads may return slightly stale data at times
- Strong consistency — a read always returns the most recently written value
- Session consistency — a client always sees its own writes, but others may see stale data
- Causal consistency — a read reflects every write that causally preceded it in the session
What search capability does Firestore NOT support natively?
- Filtering documents on the value of a single indexed field
- Ordering results by one field while range-filtering another
- Full-text keyword search across document field values
- Real-time listeners that push document changes out to connected clients
You got correct