Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 45 ยท Storage and Databases

Spanner, AlloyDB and Choosing a Database

Compare Cloud Spanner and AlloyDB with Cloud SQL, Firestore, Bigtable and BigQuery, and use a decision function to pick the right Google Cloud database.

Google Cloud has more databases than most teams need, and picking the wrong one is expensive to fix later. You have already met Cloud SQL, Firestore and Bigtable already. Two more complete the relational side: Cloud Spanner, a relational database that scales horizontally across regions, and AlloyDB, a high-performance PostgreSQL-compatible service.

This lesson explains what makes each special, then gives you a practical decision guide and a small decision function to practice with. Product capabilities, editions and prices change quickly, so verify against the current documentation.

Cloud Spanner

Spanner is a fully managed relational database with SQL, schemas and transactions, but unlike a single-server database it spreads data over many servers. Its headline features:

  • Horizontal scale: add compute capacity (measured in nodes or processing units) and Spanner splits data into ranges and moves them automatically. Writes scale too, not just reads.
  • Strong consistency with ACID transactions, even across rows stored on different servers and in different regions. Google achieves this with a replication protocol and carefully synchronized clocks (TrueTime), which you never manage.
  • High availability by design: a regional configuration replicates across zones; multi-region configurations replicate across regions for very high availability and global reads.
  • Online schema changes without downtime, and a standard SQL dialect (GoogleSQL), with a PostgreSQL-compatible interface also available.

The trade-offs: it costs more than a small Cloud SQL instance (there is a minimum capacity you pay for), schema design needs care, and some features of other engines (arbitrary extensions, the exact behavior of MySQL or PostgreSQL) are not identical.

Designing keys for Spanner

Like Bigtable, Spanner splits data by primary key ranges. A key that always increases (an auto-increment id or a timestamp first) sends all new writes to the same split and creates a hotspot. Prefer random identifiers such as UUIDs, or a hash prefix. Child tables can be interleaved with a parent (stored physically together), which makes "a customer and all its orders" cheap to read.

SQL
-- Spanner GoogleSQL (read-only example)
CREATE TABLE Customers (
  CustomerId STRING(36) NOT NULL,
  Name       STRING(100),
) PRIMARY KEY (CustomerId);

CREATE TABLE Orders (
  CustomerId STRING(36) NOT NULL,
  OrderId    STRING(36) NOT NULL,
  Total      NUMERIC,
) PRIMARY KEY (CustomerId, OrderId),
  INTERLEAVE IN PARENT Customers ON DELETE CASCADE;

AlloyDB

AlloyDB for PostgreSQL is a managed database that speaks the PostgreSQL protocol and works with PostgreSQL tools and most extensions, but uses a Google-built storage layer. It separates compute from storage, caches data intelligently, and offers read pool instances for scaling reads. Google positions it for demanding transactional workloads and for mixing transactions with analytical queries, and it supports vector search for AI features. Compared with Cloud SQL for PostgreSQL, you can expect more performance headroom and more features, at a higher price.

AlloyDB is still a regional database with one primary instance (with high availability across zones). If you need writes that scale across regions, think Spanner. If you need an ordinary PostgreSQL database, Cloud SQL is often enough.

Relational options

Cloud SQL Standard MySQL, PostgreSQL, SQL Server. Simple and familiar.
AlloyDB PostgreSQL-compatible with more performance.
Spanner Relational, global scale, strong consistency.

All three speak SQL; they differ in scale and cost.

1 / 2

Choosing a database

Start from the access pattern, not from the brand name.

  1. Analytics over large datasets, scanning many rows? Use BigQuery (BigQuery Basics). It is a warehouse, not a database for app transactions.
  2. Huge key-based reads and writes, time series, telemetry? Bigtable.
  3. Flexible documents for web and mobile apps, real-time updates? Firestore.
  4. Relational data with transactions?
    • Fits on one primary with read replicas and a region is acceptable: Cloud SQL.
    • Needs heavier PostgreSQL performance or mixed analytics: AlloyDB.
    • Needs horizontal write scaling or multi-region strong consistency: Spanner.
  5. Whole files, images and backups? Not a database: use Cloud Storage.

Other factors that matter as much as features:

  • Team skills: a team that knows PostgreSQL gets productive on Cloud SQL or AlloyDB immediately.
  • Portability: Cloud SQL and AlloyDB use open-source engines, so moving away is easier than from Spanner or Bigtable.
  • Cost at your size: start with the simplest service that meets your needs. Migrating up is easier than paying for scale you do not use.
  • Consistency needs: financial ledgers need strong transactions; a view counter does not.
Service Model Scale shape Typical use
Cloud SQL Relational One primary, read replicas Most business apps
AlloyDB Relational (PostgreSQL) One primary, read pools Heavy PostgreSQL workloads
Spanner Relational Horizontal, multi-region Global, massive transactional systems
Firestore Document Automatic App backends, live sync
Bigtable Wide-column Add nodes Telemetry, time series
BigQuery Warehouse Serverless Analytics and reporting

A decision function you can run

The code below encodes the guide as a function. It is a teaching model, not an official tool: real decisions weigh cost, team skills and many details that a few yes/no answers cannot capture.

JS Which database?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Keys that always increase are a trap in both Bigtable and Spanner. The next stand-in compares how evenly sequential ids and random ids spread across 4 imaginary key ranges, and why Spanner users avoid auto-increment keys. A hash is deterministic here so the output is stable.

JS Sequential vs hashed keys

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real and what is the stand-in: the Spanner SQL and the service descriptions are real concepts you can find in Google's docs; nothing here connects to a database. The JavaScript functions are simplified teaching models.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which service offers a relational database whose writes scale horizontally with multi-region strong consistency?

    Spanner combines SQL and ACID transactions with horizontal scaling and multi-region configurations.
  2. 2. A small team needs a standard PostgreSQL database for a typical web app with moderate traffic. What is the sensible default?

    Start with the simplest service that fits. Cloud SQL is familiar, portable and economical for most applications.
  3. 3. Why should you avoid auto-incrementing primary keys in Spanner?

    Data is split by key ranges, so monotonically increasing keys concentrate writes on one split. Use UUIDs or hashed prefixes.
  4. 4. Which tool is built for analytics queries that scan billions of rows rather than for application transactions?

    BigQuery is a serverless data warehouse. Use transactional databases for application reads and writes.

Summary

  • Spanner is a relational database with horizontal scaling and strong consistency, including across regions, at a higher cost.
  • AlloyDB is a PostgreSQL-compatible service with extra performance, still regional with one primary.
  • Choose by access pattern first: analytics, time series, documents, or relational transactions.
  • Start with the simplest service that fits (often Cloud SQL) and migrate when real needs appear.
  • Avoid monotonically increasing keys in key-range-split databases such as Spanner and Bigtable.

Next: VPC Networks, Subnets and Firewall Rules