Google Cloud Firestore

 

Google Cloud Firestore — Easy Notes

🔥 What is Firestore?

Firestore = Google Cloud's fully managed NoSQL database.

It is mainly useful for:

  • 📱 Mobile applications
  • 🌐 Web applications
  • 🖥️ Server-side applications

Unlike Cloud SQL and Spanner, Firestore is NoSQL, not a traditional relational database.


📦 How does Firestore store data?

Firestore uses:

Collections → Documents → Fields

Example:

Users (Collection)
│
├── user001 (Document)
│     ├── firstname: "Shivam"
│     ├── lastname: "Kumar"
│     └── age: 25
│
└── user002 (Document)
      ├── firstname: "Rahul"
      └── lastname: "Sharma"

Collection

A collection is like a group of related documents.

Example:

Users
Products
Orders

Document

A document contains the actual data.

Example:

user001

Key-value pairs

Inside the document:

firstname → Shivam
lastname  → Kumar
age       → 25

🪆 Documents can contain nested data

A Firestore document can contain:

  • Simple fields
  • Complex/nested objects
  • Subcollections

For example:

User
 ├── name: Shivam
 ├── age: 25
 └── address
      ├── city: Pune
      └── country: India

🔎 Queries

Firestore lets you search for specific documents.

For example:

Find users whose age is greater than 25.

You can also combine:

  • Multiple filters
  • Filtering + sorting
  • Chained conditions

Important point: Indexing

Firestore indexes data by default.

This means query performance is primarily related to how many results are returned, rather than scanning the entire dataset.

Think:

1 million documents
       ↓
Query
       ↓
100 matching documents
       ↓
Performance mainly depends on those results

🔄 Data Synchronization

This is one of Firestore's important features.

Suppose a user has your mobile app open on:

📱 Phone

and another device is connected:

💻 Laptop

When data changes, Firestore can synchronize the updated data with connected devices.


📴 Offline Support

Firestore can also work when the device has no internet connection.

It caches data that the app is actively using.

So while offline, the application can:

  • Read data
  • Write data
  • Listen for changes
  • Query data

Example:

Internet ❌

Phone
  ↓
Local cached Firestore data
  ↓
Read / Write

Then:

Internet comes back ✅
          ↓
Local changes
          ↓
Firestore
          ↓
Synchronization 🔄

This is particularly useful for mobile applications.


🌍 Replication & Reliability

Firestore uses Google Cloud infrastructure to provide:

  • Automatic multi-region replication
  • Strong consistency
  • Atomic batch operations
  • Transactions

Atomic batch operation

Suppose you need to update:

Account A: -₹500
Account B: +₹500

You don't want only one update to happen.

An atomic operation means the group of operations is treated together:

Both succeed ✅
       OR
Both fail ❌

🆚 Cloud SQL vs Spanner vs Firestore

FeatureCloud SQLSpannerFirestore
Database typeRelationalRelationalNoSQL
SQL
Tables
Documents
Horizontal scalingLimited compared with Spanner
Global scaleLimited
Offline support
Mobile/Web appsPossiblePossibleExcellent
Transactions
Strong consistency✅ Global

🧠 Easy memory trick

Cloud SQL → Traditional SQL database

Spanner → SQL database at global massive scale

FirestoreNoSQL documents for web/mobile/server apps

So remember:

Firestore = Collections + Documents + Key/Value + NoSQL + Offline Sync.

==================================

Sure 👍 Let's take a very simple real-life example.

📱 Example: Food Delivery App

Imagine you open a food delivery app while traveling in an area where internet is not available.

Internet ❌
     ↓
Food App
     ↓
Cached data on phone

You had previously opened your orders, so the app has some data saved locally.

You can still see:

🍕 My Orders
Order #123 — Pizza — ₹300

Now suppose you mark the order as "Favorite" while offline.

Offline ❌

You → Mark Pizza as Favorite ⭐
             ↓
       Saved locally

Later, your internet comes back:

Internet ✅
     ↓
Local change
     ↓
Firestore
     ↓
⭐ Favorite saved

🧠 In one line:

Firestore lets the app continue working with cached data when offline, and synchronizes the changes with the cloud when internet comes back.

Think of it like "work now, sync later." 🔄



======================================

Yes 👍 Instagram is a good example.

Suppose you open Instagram when there is no internet:

📱 Step 1 — You open Instagram

Internet ❌

Instagram
   ↓
Firestore/local cache
   ↓
Previously cached data

Instagram-like app may still be able to show some previously loaded content from the phone's local cache.

For example, you previously saw:

Post 1: 🏞️ Photo
Post 2: 🐶 Dog
Post 3: 🍕 Food

Those items may still be available locally.

📵 Step 2 — You do something offline

Suppose you like a post:

❤️ Like Post 2

The app can temporarily record that action locally:

Phone
  ↓
"Post 2 = Liked" ❤️

🌐 Step 3 — Internet comes back

Internet ✅
     ↓
Local change
     ↓
Firestore
     ↓
Server data updated ❤️

So the basic idea of offline support is:

The app doesn't necessarily have to stop working completely just because the internet is temporarily unavailable. It can use locally cached data and synchronize changes when connectivity returns.

⚠️ Small clarification: Instagram itself is not a Firestore example—Instagram uses Meta's own backend infrastructure. We're using an Instagram-like app only to understand how Firestore's offline capability works.

Comments

Popular posts from this blog

Async/await

First negative in every window of size k

Valid Parentheses