Choosing a database: Firestore to Spanner

Google Cloud · Advanced · 14 min

Picking the wrong database is the most expensive architectural mistake to undo.

Google Cloud offers a database for most shapes of data. A practical map:

  • Cloud SQL: managed PostgreSQL/MySQL/SQL Server on one primary. Default for typical OLTP apps.
  • AlloyDB for PostgreSQL: PostgreSQL-compatible with a disaggregated storage layer, faster transactions, read pool instances and a columnar engine for analytical queries. For demanding Postgres workloads.
  • Spanner: relational, SQL, strongly consistent, scaling writes horizontally across nodes and regions, with up to 99.999% availability in multi-region configs. For global or very large OLTP that outgrows one primary.
  • Firestore: serverless document database (collections of JSON-like documents), strongly consistent, with real-time listeners and offline mobile/web SDKs. Pay per operation; great for app backends.
  • Bigtable: wide-column NoSQL for huge throughput at single-digit millisecond latency: time series, IoT, ad tech. Access by row key only.
  • Memorystore: managed Valkey, Redis and Memcached for caching.
  • BigQuery: analytics warehouse, not an OLTP store.

Questions to ask: Do I need joins and multi-row transactions (relational)? How big will writes get? Is global distribution required? What access patterns exist (key lookups, ranges, ad-hoc analytics)? Is per-operation or per-node pricing better at my traffic?

Example

# pip install google-cloud-firestore
from google.cloud import firestore

db = firestore.Client(project="shop-dev-4821")
users = db.collection("users")
users.document("ada").set({"name": "Ada", "plan": "pro", "age": 36})
users.document("bob").set({"name": "Bob", "plan": "free", "age": 29})

query = users.where(filter=firestore.FieldFilter("plan", "==", "pro"))
for doc in query.stream():
    print(doc.id, doc.to_dict()["name"])
# ada Ada

# Listen for live changes (real-time updates)
watch = users.document("ada").on_snapshot(lambda docs, changes, ts: print(docs[0].to_dict()))

Remember

  • Cloud SQL by default; AlloyDB for heavy Postgres; Spanner when one primary is not enough
  • Firestore for serverless app documents with real-time sync
  • Bigtable for massive key-range workloads; BigQuery for analytics

Watch out for

  • Choosing Spanner or Bigtable for a small app and paying for capacity it never uses.

Check yourself

  1. A global payments system needs relational schema, strong consistency and writes beyond one machine. Which fits?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: cloud.google.com/products/databases