---
url: https://docs.tailor.tech/guides/tailordb/replication.md
description: >-
  Keep TailorDB tables copied into a BigQuery dataset you own: how replication
  works, what lands in BigQuery, access, limits and recovery.
---

# Replication to BigQuery&#x20;

TailorDB replication keeps a copy of the tables you choose in a BigQuery dataset you own. It copies the existing records first, then applies every create, update and delete as it happens. Analytics, reporting and data pipelines can query BigQuery instead of TailorDB.

::: info Preview
Replication is in [Preview](/reference/platform/service-lifecycle-policy#preview) and is enabled per workspace. Contact Tailor support to enable it. Until it is enabled, creating a destination or a replication fails with `permission_denied: TailorDB replication isn't enabled for this workspace`.
:::

To set it up with the SDK, see [TailorDB Replication](/sdk/services/tailordb/replication).

## Concepts

* **Destination:** a BigQuery dataset in your Google Cloud project that replications write into.
* **Replication:** copies chosen tables of one TailorDB namespace into one destination. Each TailorDB table becomes one BigQuery table.
* **Service account:** each workspace has its own Google service account. Replication writes to BigQuery as this account and nothing else, so it can only reach what you grant it.

## How it works

1. **Copy:** when a replication starts, or a table is added to it, TailorDB copies the table's current records into BigQuery.
2. **Stream:** after that, every change to a replicated record is written to BigQuery as it happens, in the order it happened.
3. **Repair:** if the BigQuery table falls out of step with TailorDB, for example after the table is deleted in BigQuery, TailorDB rewrites it from its records.

Reading from BigQuery adds no load to TailorDB. Queries of a replicated table can return results up to its [max staleness](https://cloud.google.com/bigquery/docs/change-data-capture#manage_table_staleness) behind (15 minutes by default) while BigQuery merges recent changes in the background.

## Granting access

Grant the workspace's service account:

* **BigQuery Data Editor** (`roles/bigquery.dataEditor`) on the dataset
* **BigQuery Job User** (`roles/bigquery.jobUser`) on the project

`tailor deploy` prints the account's email when it creates a destination. Until the grant works, the destination stays `PENDING_PERMISSION` and nothing is written. If the grant is removed later, the destination moves to `PERMISSION_ERROR` and replication stops writing until access is restored.

If your organization restricts IAM members to its own domains (the `iam.allowedPolicyMemberDomains` organization policy), the policy must also allow Tailor's Google Workspace customer ID. Contact Tailor support for the ID.

The dataset can be in any BigQuery location. Choose one that meets your data residency needs: records are written from your workspace's region to the dataset's location.

## What lands in BigQuery

Each replicated table is written to `<tablePrefix><TableName>` in the destination dataset. TailorDB creates the table, with `id` as its primary key, and clusters it by `id`. It never writes to a table it didn't create.

| Column               | Type      | Meaning                                             |
| -------------------- | --------- | --------------------------------------------------- |
| `id`                 | STRING    | The record's ID                                     |
| *replicated fields*  | see below | One column per replicated field                     |
| `_tailor_seq`        | STRING    | Ordering key of the change that wrote the row       |
| `_tailor_deleted_at` | TIMESTAMP | When the record was deleted; `NULL` while it exists |
| `_tailor_source_ts`  | TIMESTAMP | When the change happened in TailorDB                |

### Field types

| TailorDB type (SDK method)                                   | BigQuery column                                        |
| ------------------------------------------------------------ | ------------------------------------------------------ |
| String, UUID, Enum (`db.string()`, `db.uuid()`, `db.enum()`) | STRING                                                 |
| Integer (`db.int()`)                                         | INT64                                                  |
| Float (`db.float()`)                                         | FLOAT64                                                |
| Boolean (`db.bool()`)                                        | BOOL                                                   |
| Decimal (`db.decimal()`)                                     | NUMERIC, or BIGNUMERIC with more than 9 decimal places |
| Date (`db.date()`)                                           | DATE                                                   |
| Time (`db.time()`)                                           | TIME                                                   |
| DateTime (`db.datetime()`)                                   | TIMESTAMP                                              |
| Nested (`db.object()`)                                       | RECORD                                                 |
| Array fields                                                 | REPEATED column of the item type                       |

File and vector fields are not replicated.

### Deleted records

A deleted record stays in BigQuery as a row with `_tailor_deleted_at` set and its other fields `NULL`. To query live records only:

```sql
SELECT * FROM `acme-analytics.tailor.shop_Order`
WHERE _tailor_deleted_at IS NULL
```

## Table states

Each table of a replication has a state. Check them with `tailor tailordb replication status <name>`.

| State         | Meaning                                                                                                                 |
| ------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `PENDING`     | Waiting to be copied                                                                                                    |
| `BACKFILLING` | Copying the table's records                                                                                             |
| `STREAMING`   | Up to date; changes are written as they happen                                                                          |
| `ERROR`       | Can't be written, for example the BigQuery table was deleted or its schema no longer fits. The state's message says why |

A table in `ERROR` stays there until you fix the cause and run `tailor tailordb replication repair <name>`, which rewrites it from TailorDB.

## Changing replicated tables

* Adding a table to a replication copies its records first.
* Adding a field to the replicated fields rewrites the table.
* Removing a table from a replication, or deleting a replication or destination, stops writing; the data already in BigQuery is kept.
* Changing a field's type to one its BigQuery column can't hold puts the table in `ERROR`.

## Limits

| Resource                              | Limit per workspace |
| ------------------------------------- | ------------------- |
| Replications                          | 5                   |
| Destinations                          | 5                   |
| Tables replicated across replications | 100                 |

## Cost

BigQuery storage, the background merges of replicated changes, and your queries are billed to your Google Cloud project. A copy or repair rewrites the whole table, so it costs more than everyday changes on large tables.
