Replication, Routing Consistency, Backup and Restore

Article · 8:00

Multi-cluster replication, routing profiles and their consistency implications, cluster failover, backups and restore, and verifying recovery.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

Replication is per instance and multi-primary. Add a second cluster - in another zone or another region - and Bigtable replicates between them asynchronously. Every cluster accepts both reads and writes, which makes it genuinely multi-primary and introduces the consistency question this module is about.

Conflict resolution is last-write-wins by cell timestamp. The same cell written in two clusters at nearly the same time resolves to the higher timestamp, and the other write is discarded with no record. So replication is right when writes to a given row are naturally confined to one cluster - region affinity, or a serving cluster and a batch cluster writing different things - and wrong when two clusters genuinely contend for the same cell. This is the same trade as DynamoDB global tables and Firestore, and the honest statement of it belongs in your design.

Routing is where the consistency decision lives. An app profile's routing policy determines what you get:

  • Single-cluster routing sends all a profile's requests to one named cluster. That gives read-your-writes consistency for that profile - you write to a cluster and read from the same one - and it gives up automatic failover, since the profile fails if that cluster is unavailable.
  • Multi-cluster routing sends requests to the nearest available cluster, which gives automatic failover and lower latency and gives up read-your-writes: you may write to one cluster and read from another that has not yet received it.
  • Conditional and read-modify-write operations require single-cluster routing, as Module 3 established.

So the design pattern is several profiles with different routing, chosen per operation: single-cluster for anything requiring read-your-writes or a conditional write, multi-cluster for reads that tolerate staleness and want availability.

Cluster failover with multi-cluster routing is automatic and fast. With single-cluster routing it is a configuration change - you update the app profile to point at the other cluster - which can be scripted and is the thing to have in a runbook rather than to work out during an incident.

Backups are per-table, per-cluster, created on demand or on a schedule, retained for a configurable period, and restored to a new table. The important properties: a backup is consistent for the table at a point in time; a restore creates a new table, so the runbook needs a cutover step; and the restored table's app profiles and replication are instance-level and so carry over, but anything table-specific must be checked. There is no point-in-time recovery to an arbitrary second as in Firestore - you restore a backup you took.

Verifying a recovery means assertions, and there is a Bigtable-specific wrinkle: because there is no COUNT(*), counting rows means scanning, which on a large table is a real operation. The practical approach is to verify a sample of known keys, verify the key range boundaries, and run a counted scan over a bounded prefix rather than the whole table.

Laboratory exercise 5 - break it, then get it back.

Add a second cluster in another zone and confirm replication. Measure the replication lag under write load from the metrics.

Demonstrate the routing consistency difference: with a single-cluster profile, write and immediately read and show it always present; with a multi-cluster profile, write and immediately read in a tight loop and record how often the read misses. That miss rate is the cost of availability, measured.

Demonstrate the conflict: write the same cell through two single-cluster profiles pointing at different clusters, at nearly the same time, and show one write discarded with no record of it.

Demonstrate failover both ways: make one cluster unavailable and show the multi-cluster profile continuing; then show the single-cluster profile failing, and perform the app-profile switch, timing it.

Create a backup, delete the table, and restore it. Record RPO and RTO, measured, and verify with assertions - sample keys, range boundaries and a counted prefix scan.

Finally, configure a backup schedule and verify it runs.

Deliverable: the replication topology with measured lag, the routing consistency measurements including the multi-cluster miss rate, the discarded-write demonstration, both failover procedures with the switch timed, the backup and restore with measured RPO and RTO, the verification assertions, and the scheduled backup evidence.

PY Example 1 158 lines
# ==================================================================
# REPLICATION: per instance, MULTI-PRIMARY.
# ==================================================================
#   gcloud bigtable clusters create prod-c2 \
#     --instance=telemetry --zone=europe-west2-b \
#     --num-nodes=3 --storage-type=SSD
#
# *** EVERY CLUSTER ACCEPTS BOTH READS AND WRITES. *** Replication is
# asynchronous, typically seconds.
#
# Measure the lag; do not assume it:
#   bigtable.googleapis.com/cluster/replication_latencies
#   bigtable.googleapis.com/table/replication_latency
#   gcloud bigtable instances describe telemetry

# *** CONFLICT RESOLUTION IS LAST-WRITE-WINS BY CELL TIMESTAMP. ***
#   the same cell written in two clusters at nearly the same time
#   -> the HIGHER TIMESTAMP wins
#   -> THE OTHER WRITE IS DISCARDED WITH NO RECORD
#
# So replication is RIGHT when writes to a given row are naturally
# confined to ONE cluster - region affinity, or a serving cluster and
# a batch cluster writing DIFFERENT things - and WRONG when two
# clusters genuinely contend for the same cell.
#
# The same trade as DynamoDB global tables and Firestore. Demonstrate
# the discard in the lab; the honest statement of it belongs in your
# design rather than in a footnote.
#
# ONE USEFUL DETAIL: because resolution is by CELL TIMESTAMP, writing
# with an EXPLICIT timestamp you control (rather than the server's)
# gives you a deterministic resolution rule. That is occasionally the
# right tool, and it is also how you can write a cell nothing can
# overwrite - so use it deliberately.

# ==================================================================
# *** ROUTING IS WHERE THE CONSISTENCY DECISION LIVES. ***
# ==================================================================
#   SINGLE-CLUSTER routing
#     all requests -> ONE named cluster
#     + READ-YOUR-WRITES for that profile
#     + conditional and read-modify-write are ALLOWED
#     - NO automatic failover: the profile FAILS if that cluster is
#       unavailable
#
#   MULTI-CLUSTER routing
#     requests -> the NEAREST AVAILABLE cluster
#     + AUTOMATIC FAILOVER, lower latency
#     - NO read-your-writes: you may write to one cluster and read
#       from another that has not received it yet
#     - conditional and read-modify-write are REFUSED
#
#   gcloud bigtable app-profiles create serving \
#     --instance=telemetry --route-to=prod-c1 --transactional-writes
#   gcloud bigtable app-profiles create reads \
#     --instance=telemetry --route-any
#   gcloud bigtable app-profiles create analytics \
#     --instance=telemetry --route-to=prod-c2

serving = instance.table("telemetry", app_profile_id="serving")
reads   = instance.table("telemetry", app_profile_id="reads")

# THE MEASUREMENT - the cost of availability, as a number:
#   single-cluster: write then immediately read, 10,000 times
#     -> 0 misses. Always.
#   multi-cluster:  write then immediately read, 10,000 times
#     -> ___ % miss. THAT PERCENTAGE IS THE COST OF AVAILABILITY,
#        in your deployment, under your load. Record it; it is the
#        number that makes the routing choice defensible.

# *** THE DESIGN PATTERN: SEVERAL PROFILES, CHOSEN PER OPERATION. ***
#   serving   single-cluster  writes, conditional writes, and any
#                             read that must see them
#   reads     multi-cluster   reads that tolerate staleness and want
#                             availability
#   analytics single-cluster  batch jobs, on the OTHER cluster, so
#                             they cannot touch serving latency

# ==================================================================
# FAILOVER: automatic one way, a runbook step the other.
# ==================================================================
# MULTI-CLUSTER: automatic and fast. Nothing to do.
#
# SINGLE-CLUSTER: a CONFIGURATION CHANGE. Script it, and have it in
# the runbook rather than working it out during the incident:
#   gcloud bigtable app-profiles update serving \
#     --instance=telemetry --route-to=prod-c2 \
#     --transactional-writes --force
#
#   TIME IT in the lab. It is the RTO for a cluster outage on your
#   serving path, and it is a number someone will ask for.
#   --force is required because changing routing can affect
#   consistency guarantees - which is the API telling you this is a
#   decision, not a tweak.

# ==================================================================
# BACKUP and RESTORE: per table, per cluster, to a NEW table.
# ==================================================================
#   gcloud bigtable backups create telemetry-20261130 \
#     --instance=telemetry --cluster=prod-c1 --table=telemetry \
#     --retention-period=30d
#   gcloud bigtable backups list --instance=telemetry --cluster=prod-c1
#   gcloud bigtable backups describe telemetry-20261130 \
#     --instance=telemetry --cluster=prod-c1
#
#   gcloud bigtable instances tables restore \
#     --source=projects/P/instances/telemetry/clusters/prod-c1/backups/telemetry-20261130 \
#     --async --destination=telemetry-restored \
#     --destination-instance=telemetry
#
# THE PROPERTIES THAT MATTER:
#   * consistent for the table at a point in time
#   * *** RESTORES TO A NEW TABLE *** - so the runbook needs a
#     CUTOVER step. There is no restore-in-place.
#   * app profiles and replication are INSTANCE-level, so they carry
#     over - but check anything table-specific
#   * *** THERE IS NO POINT-IN-TIME RECOVERY to an arbitrary second
#     as in Firestore. You restore A BACKUP YOU TOOK. *** So the
#     backup SCHEDULE is your RPO, and it is the number to state.
#
# Scheduled backups, which is what production uses:
#   gcloud bigtable backups schedules create daily \
#     --instance=telemetry --cluster=prod-c1 --table=telemetry \
#     --retention-period=30d --frequency=24h

# Also useful, and not a backup: export to Cloud Storage or BigQuery
# for analytics and for the queries no row key can serve (Module 2,
# queries 7 and 8).
#   gcloud dataflow jobs run export-telemetry \
#     --gcs-location gs://dataflow-templates/latest/Cloud_Bigtable_to_GCS_Avro

# ==================================================================
# *** VERIFYING A RECOVERY - with a Bigtable-specific wrinkle. ***
# ==================================================================
# There is NO COUNT(*). Counting rows means SCANNING, which on a
# large table is a real operation with a real cost. So:

restored = instance.table("telemetry-restored")

# 1. SAMPLE KNOWN KEYS - cheap, and catches a wrong or empty restore:
for k in known_sample_keys:                 # a few hundred
    assert restored.read_row(k) is not None, k

# 2. KEY RANGE BOUNDARIES - the first and last row of each prefix:
first = next(iter(restored.read_rows(row_set=_prefix("dev#"), limit=1)))
# ... and the expected first key for each device prefix

# 3. A COUNTED SCAN OVER A BOUNDED PREFIX, not the whole table:
from google.cloud.bigtable import row_filters as rf
n = sum(1 for _ in restored.read_rows(
    row_set=_prefix("dev#d-0042#20261130"),
    filter_=rf.StripValueTransformerFilter(True)))   # keys only: cheap
assert n == expected_for_that_day
#
# "The table exists and is READY" is NOT verification. And
# sample_row_keys is useful for understanding the split points but
# is an estimate, not a count:
list(restored.sample_row_keys())

Open this example in the editor and change it. Nothing you type is saved.

Discussion

No questions on this topic yet. Sign in to ask the first one.