Overview and What You Will Learn
Choosing a GCP database starts with the access pattern, not the scale: workloads that need joins and transactions belong on a relational database, and workloads with flexible documents belong on a document database. In this lab you will model the same kind of data in Cloud SQL, Spanner and Firestore, run a query against each, and see why each product fits a different shape of workload.
- Using a decision path from access pattern to product
- Running a relational JOIN on Cloud SQL
- Running the same JOIN on Spanner, a horizontally scalable relational database
- Storing flexible documents in Firestore through its REST API
- Knowing where Bigtable and BigQuery fit
Why This Matters in Production
Picking a database by reputation instead of access pattern is a costly mistake, because migrating data models later takes months. Picture a team at a company like Shopify choosing Firestore for an order system because "NoSQL scales better", then needing joins across customers, orders and inventory with multi-row transactions. The failure was never technical. The team never asked whether the workload needed joins and transactions before choosing.
Core Principles
GCP's managed databases are not interchangeable, and each one fits a different data shape and scale. Start with the questions below, and treat expected data volume as the second question, not the first.
transactions?}:::q -->|yes| Q2{Global scale or
multi-region writes?}:::q Q2 -->|no| SQL[(Cloud SQL)]:::tool Q2 -->|yes| SPN[(Spanner)]:::tool Q1 -->|no| Q3{App data with
flexible schema?}:::q Q3 -->|yes| FS[(Firestore)]:::ok Q3 -->|no| OTH[Bigtable or BigQuery]:::soft classDef q fill:#FFEDD5,color:#1E293B classDef tool fill:#326CE5,color:#FFFFFF classDef ok fill:#10B981,color:#FFFFFF classDef soft fill:#EDE9FE,color:#1E293B
The table below summarizes what each product is best at:
| Product | Best for |
|---|---|
| Cloud SQL | Relational workloads on MySQL, PostgreSQL or SQL Server with one writable primary |
| Spanner | Relational SQL that must scale horizontally with strong consistency, even globally |
| Firestore | Serverless document data for mobile and web apps, with real-time client sync |
| Bigtable | Very high-throughput wide-column data such as time series and IoT telemetry |
| BigQuery | Analytical queries over large datasets, not live transactions |
Detailed Step-by-Step Practical Lab
- Create the project, link billing, and enable the APIs:
gcloud projects create mumbai-db-lab-2026 --name="Database Decision Lab"gcloud billing projects link mumbai-db-lab-2026 \ --billing-account=0A1B2C-3D4E5F-6A7B8Cgcloud config set project mumbai-db-lab-2026gcloud services enable sqladmin.googleapis.com firestore.googleapis.com spanner.googleapis.comRememberCloud SQL and Spanner bill by the hour while they exist. Run the lab in one sitting and complete the cleanup step as soon as you finish.
- Create a Cloud SQL instance for a relational, transactional workload and set the postgres password:
gcloud sql instances create mumbai-pg-orders-1 \ --database-version=POSTGRES_15 \ --edition=ENTERPRISE \ --tier=db-f1-micro \ --region=asia-south1 gcloud sql users set-password postgres \ --instance=mumbai-pg-orders-1 \ --password="REPLACE_WITH_GENERATED_PASSWORD" gcloud sql databases create orders_db --instance=mumbai-pg-orders-1- Connect with the
psqlclient (it must be installed) and create a small relational schema:
gcloud sql connect mumbai-pg-orders-1 --user=postgres --database=orders_db-- Run these inside psqlCREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT);CREATE TABLE orders ( id SERIAL PRIMARY KEY, customer_id INT REFERENCES customers(id), total NUMERIC);INSERT INTO customers (name) VALUES ('Priya Sharma');INSERT INTO orders (customer_id, total) VALUES (1, 4599.00); SELECT customers.name, orders.totalFROM orders JOIN customers ON orders.customer_id = customers.id;The JOIN across two related tables is the access pattern that points to a relational database. Firestore has no native cross-collection join.
- Create a small Spanner instance and run the same relationship as a JOIN. Spanner uses primary keys and typed columns, and 100 processing units is the smallest size:
gcloud spanner instances create mumbai-orders-spanner \ --config=regional-asia-south1 \ --description="Orders lab" \ --processing-units=100 gcloud spanner databases create orders \ --instance=mumbai-orders-spanner \ --ddl='CREATE TABLE Customers (CustomerId INT64 NOT NULL, Name STRING(100)) PRIMARY KEY (CustomerId); CREATE TABLE Orders (OrderId INT64 NOT NULL, CustomerId INT64 NOT NULL, TotalPaise INT64) PRIMARY KEY (OrderId)' gcloud spanner rows insert --database=orders --instance=mumbai-orders-spanner \ --table=Customers --data="CustomerId=1,Name=Priya Sharma"gcloud spanner rows insert --database=orders --instance=mumbai-orders-spanner \ --table=Orders --data="OrderId=1001,CustomerId=1,TotalPaise=459900" gcloud spanner databases execute-sql orders \ --instance=mumbai-orders-spanner \ --sql="SELECT c.Name, o.TotalPaise FROM Orders o JOIN Customers c ON o.CustomerId = c.CustomerId"- Create a Firestore database in Native mode for a different workload shape, user profile data with an evolving schema:
gcloud firestore databases create --location=asia-south1- Write two documents with different shapes into the same
userscollection through the REST API:
TOKEN=$(gcloud auth print-access-token)BASE="https://firestore.googleapis.com/v1/projects/mumbai-db-lab-2026/databases/(default)/documents" PRIYA=$(jq -n '{fields: {name: {stringValue: "Priya Sharma"}, city: {stringValue: "Mumbai"}}}')RAHUL=$(jq -n '{fields: {name: {stringValue: "Rahul Verma"}, theme: {stringValue: "dark"}, devices: {integerValue: "3"}}}') curl -s -X POST "$BASE/users?documentId=priya" \ -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \ -d "$PRIYA" curl -s -X POST "$BASE/users?documentId=rahul" \ -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \ -d "$RAHUL" curl -s -H "Authorization: Bearer $TOKEN" "$BASE/users"Compare the three models. Cloud SQL and Spanner enforce a fixed schema with explicit relationships and answer JOINs. Firestore stored two documents with different fields in one collection and needed no schema change.
Clean up. Deleting the project removes everything, and the explicit deletes stop billing immediately:
gcloud spanner instances delete mumbai-orders-spanner --quietgcloud sql instances delete mumbai-pg-orders-1 --quietgcloud projects delete mumbai-db-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakeChoosing a database by general reputation ("NoSQL scales better") instead of by the access pattern: joins, transactions or flexible schemas. A workload that needs relational joins and multi-row transactions points to Cloud SQL or Spanner, whatever its expected volume.
TipIf a Cloud SQL workload outgrows a single primary and still needs SQL semantics, Spanner is the natural next step rather than a NoSQL database, because the requirement (SQL) has not changed, only the scale. Plan for schema and application changes in that move.
- Do not use BigQuery as a transactional database. It also speaks SQL, but it is priced and tuned for analytical queries over large datasets, not frequent small reads and writes from a live application.
- Use Firestore for real-time client sync. Mobile and web apps can listen for document changes and update instantly, which Cloud SQL and Spanner do not provide natively.
- Scale Cloud SQL up first and out second. One writable primary scales vertically, and read replicas absorb read traffic, which is enough for most workloads before Spanner is justified.
- Use Bigtable for time-series and telemetry at very high write rates. Its row-key design drives performance, so model access patterns before loading data.
- Weigh cost and operations honestly. Spanner and multi-region setups cost more than a single Cloud SQL instance, so choose them for a real requirement, not a hypothetical one.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud sql instances create |
Create a managed Cloud SQL instance |
gcloud sql connect |
Connect to a Cloud SQL instance with psql or mysql |
gcloud spanner instances create |
Create a Spanner instance |
gcloud spanner databases execute-sql |
Run a SQL query against Spanner |
gcloud firestore databases create |
Create a Firestore database |