Glossary · Data architecture
What is Change data capture (CDC)?
Change data capture (CDC) reads inserts, updates and deletes from a source database's transaction log and streams them to another system, keeping a copy in sync without full reloads. On Google Cloud, Datastream replicates databases into BigQuery this way.
How CDC works
Every relational database writes changes to a log before applying them: the MySQL binary log, PostgreSQL's write-ahead log through logical decoding, Oracle's redo logs, SQL Server's transaction log. A CDC tool reads that log and emits one event per changed row with the operation type, the new values and a position or timestamp. Compared with querying tables for changed rows, log-based CDC sees deletes, captures every intermediate change, and puts little load on the source.
A replication usually starts with a backfill (a snapshot of existing rows) and then streams changes from the log position where the snapshot ended.
CDC into BigQuery
BigQuery's own CDC ingestion accepts upserts and deletes through the Storage Write API: each streamed row carries a _CHANGE_TYPE of UPSERT or DELETE, and the target table needs a declared primary key. Background apply jobs merge the changes into the table. Datastream, Google Cloud's serverless CDC and replication service, uses this to replicate databases and offers two write modes:
- Merge: the BigQuery table mirrors the current state of the source table.
- Append-only: every change is a new row, and a
datastream_metadatacolumn records the change type (INSERT,UPDATE-INSERT,UPDATE-DELETEorDELETE), the source timestamp and sort keys. You keep full history and build current state yourself.
A table that receives CDC changes declares its key and how stale it may be:
CREATE TABLE `my-project.replica.orders` (
order_id STRING PRIMARY KEY NOT ENFORCED,
customer_id STRING,
status STRING,
updated_at TIMESTAMP
)
PARTITION BY DATE(updated_at)
CLUSTER BY order_id
OPTIONS (max_staleness = INTERVAL 15 MINUTE);
With append-only mode, the current state is the latest change per key, minus keys whose latest change is a delete:
SELECT * EXCEPT (datastream_metadata)
FROM `my-project.raw_cdc.orders`
WHERE datastream_metadata.change_type != 'UPDATE-DELETE'
QUALIFY ROW_NUMBER() OVER (
PARTITION BY order_id
ORDER BY datastream_metadata.source_timestamp DESC) = 1
AND datastream_metadata.change_type != 'DELETE';
The WHERE drops the old images of updated rows. If changes to one key can share a source timestamp, add a tiebreaker from the metadata's sort keys.
Cost and freshness
Datastream is billed separately from BigQuery. On the BigQuery side, applying changes uses compute: without a BACKGROUND or BACKGROUND_CHANGE_DATA_CAPTURE reservation assignment, background apply jobs run on on-demand pricing, which Google notes can be costly for large, frequently changing tables. max_staleness is the main lever: a lower value means fresher data and more merge work. If a query arrives when the table is staler than max_staleness, BigQuery merges at query time and bypasses partition pruning, so the query scans the whole table. Google's guidance is to set it to the higher of your tolerated staleness and twice the typical apply time plus a buffer.
Common pitfalls
- Tables with active CDC block mutating DML (
UPDATE,DELETE,MERGE) while changes stream or wait to be merged. - Keys that aren't unique silently produce wrong merges, because keys aren't enforced.
- Schema changes at the source need a plan: Datastream handles many, but changing or adding a primary key on an already replicated table needs support.
- Deletes and retention. BigQuery keeps CDC deletes for a two-day window to apply out-of-order events; replicated personal data also needs a deletion plan downstream.
FAQ
Which sources does Datastream support?
Database sources include MySQL, PostgreSQL, AlloyDB for PostgreSQL, Oracle, SQL Server, MongoDB and Spanner, plus Salesforce; several other SaaS sources are in Preview. Destinations are BigQuery and Cloud Storage.
What does max_staleness do on a CDC table?
It sets how stale query results may be. Within that interval BigQuery reads the table as last merged by background apply jobs, with partition pruning. Without it, or past it, queries merge pending changes at run time, scan the whole table and cost more.
Is the primary key enforced on a BigQuery CDC table?
No. Primary keys are declared NOT ENFORCED, and BigQuery CDC ingestion doesn't check uniqueness, so the source key must really be unique. Composite keys of up to 16 columns are supported.
