Instances, Tables, Column Families and Cells

Article · 8:00

Instances, clusters and nodes; tablets; column families and qualifiers; cells with timestamps; the emulator; and the limits that bound a design.

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

The hierarchy, and what each level means. An instance is the logical database and holds one or more clusters; a cluster lives in one zone and has a node count, which is the unit of capacity and cost. Tables belong to the instance and exist in every cluster. Nodes serve data but do not store it - storage is in Colossus, Google's distributed file system - which is why adding a node is fast and why a node failure loses nothing.

Tablets are the unit of distribution. A table's key space is split into contiguous ranges called tablets, each assigned to one node. Bigtable splits and rebalances tablets automatically as data and traffic change. The consequence that drives the course: a row's traffic is served by exactly one node, so the distribution of your traffic across the key space is the distribution across your nodes.

The data model has four levels and one index.

  • A row key is an arbitrary byte string, up to 4 KB, and rows are stored in lexicographic order by it. That order is the only index.
  • A column family is a group of columns, declared in the schema, with its own garbage collection policy. Families are the only schema element; there are typically a handful.
  • A column qualifier is an arbitrary byte string within a family, not declared in advance - so a row can have millions of distinct qualifiers, and qualifiers are often used as data (a timestamp, an event id).
  • A cell is the value at a row, family, qualifier and timestamp. Bigtable keeps multiple timestamped versions of a cell, and how many it keeps is the garbage collection policy's job.

That last point is worth dwelling on, because it is unusual: a write does not replace a value, it adds a version. Reading gives you the latest by default, and old versions accumulate until garbage collection removes them. A table whose GC policy keeps unlimited versions and is written to every second will grow without bound.

The limits that bound a design:

  • Row key: 4 KB. And a long key is stored with every cell, so key length is a real storage cost.
  • A single row: 256 MB recommended, 100 MB for best performance. A row is never split across tablets, so a row that grows without bound is a tablet that cannot be split - which is the worst shape in Bigtable.
  • A cell value: 100 MB, and 10 MB per cell for good performance.
  • Column families: about 100 per table, and fewer is better.
  • Qualifiers: unlimited in practice.
  • Per node: roughly 10,000 reads or writes per second for small rows on SSD, and a storage limit per node that depends on the storage type.

Laboratory exercise 1 - provision and verify.

Get the emulator running and the cbt CLI configured against it. Then create a real single-node SSD development instance, confirm your code works against both by configuration alone, and write down the teardown procedure - this matters, because a forgotten instance is a bill.

Create a table with at least two column families with different GC policies. Load at least one million rows modelling the capstone's telemetry, with a hierarchical row key.

Then demonstrate the data model, each with evidence: write the same cell three times and read all versions; set a GC policy of one version and show the others disappear after compaction; create a row with 10,000 distinct qualifiers and read it; and read a range with a prefix and show the lexicographic ordering.

Then demonstrate the limits: build a row approaching 100 MB and measure the read latency against a small row; and write a key over 4 KB and capture the error.

Finally, record what the emulator cannot do, by trying it: check tablet splits, replication and backups and record the errors.

Deliverable: an idempotent table creation script, a loader, a README covering both emulator and instance with the teardown procedure, the four data model demonstrations, the two limit demonstrations with the large-row latency comparison, and the list of emulator gaps.

PY Example 1 149 lines
# ------------------------------------------------------------------
# THE EMULATOR for development. A REAL INSTANCE for Modules 4-6.
# ------------------------------------------------------------------
#   gcloud beta emulators bigtable start --host-port=localhost:8086
#   export BIGTABLE_EMULATOR_HOST=localhost:8086
#
#   cbt -project dev -instance dev createtable telemetry
#   cbt -project dev -instance dev ls
#
# THE REAL THING - and note the shape of the cheapest useful config:
#   gcloud bigtable instances create telemetry-dev \
#     --display-name="Telemetry dev" \
#     --cluster-config=id=dev-c1,zone=europe-west2-a,nodes=1 \
#     --cluster-storage-type=SSD
#
# *** A BIGTABLE INSTANCE IS ONE OF THE MORE EXPENSIVE THINGS IN THIS
# *** PROGRAMME - billed per NODE-HOUR with a minimum. WRITE THE
# *** TEARDOWN PROCEDURE DOWN AND USE IT: ***
#   gcloud bigtable instances delete telemetry-dev
# A forgotten instance is a bill, not a mistake you notice.

from google.cloud import bigtable
from google.cloud.bigtable import column_family, row_filters
from google.cloud.bigtable.row_set import RowSet
import datetime, os, struct

client = bigtable.Client(project="my-project", admin=True)
instance = client.instance("telemetry-dev")
# BIGTABLE_EMULATOR_HOST in the environment switches this to the
# emulator with no code change - which is the point.

# ------------------------------------------------------------------
# COLUMN FAMILIES are the only schema element. And the GC policy is
# per family, which is why you have more than one.
# ------------------------------------------------------------------
table = instance.table("telemetry")
table.create(column_families={
    # Current readings: ONE version. A write REPLACES, effectively.
    "cur": column_family.MaxVersionsGCRule(1),

    # History: keep 90 days, and at most 100 versions - whichever
    # comes first. An INTERSECTION means BOTH must be true to delete.
    "hist": column_family.GCRuleIntersection([
        column_family.MaxAgeGCRule(datetime.timedelta(days=90)),
        column_family.MaxVersionsGCRule(100),
    ]),

    # Metadata: one version, kept forever.
    "meta": column_family.MaxVersionsGCRule(1),
})
# About 100 families per table is the limit and FEWER IS BETTER -
# families are a schema-level cost. Qualifiers are unlimited, so put
# the variety in the QUALIFIER, not in the family.

# ------------------------------------------------------------------
# *** A WRITE DOES NOT REPLACE A VALUE. IT ADDS A VERSION. ***
# ------------------------------------------------------------------
row = table.direct_row(b"device#d-0042#2026-11-30T14:04:00Z")
row.set_cell("hist", b"temp_c", b"21.4",
             timestamp=datetime.datetime(2026, 11, 30, 14, 4, 0))
row.set_cell("hist", b"temp_c", b"21.6",
             timestamp=datetime.datetime(2026, 11, 30, 14, 4, 1))
row.set_cell("hist", b"temp_c", b"21.9",
             timestamp=datetime.datetime(2026, 11, 30, 14, 4, 2))
row.commit()

r = table.read_row(b"device#d-0042#2026-11-30T14:04:00Z")
cells = r.cells["hist"][b"temp_c"]
# -> THREE cells, newest first. The latest is cells[0].
#
# Old versions ACCUMULATE until GARBAGE COLLECTION removes them. A
# table whose GC policy keeps unlimited versions and is written every
# second GROWS WITHOUT BOUND - and GC runs during compaction, so the
# removal is not immediate.

# Reading just the latest, which is what you usually want:
r = table.read_row(b"device#d-0042#2026-11-30T14:04:00Z",
                   filter_=row_filters.CellsColumnLimitFilter(1))

# ------------------------------------------------------------------
# QUALIFIERS ARE NOT DECLARED, so they are often used AS DATA.
# ------------------------------------------------------------------
row = table.direct_row(b"device#d-0042#metrics")
for i in range(10000):
    # The qualifier IS the timestamp. A row with 10,000 columns, each
    # a reading. This is idiomatic Bigtable and looks very odd at
    # first.
    ts = f"2026-11-30T{i//3600:02d}:{(i//60)%60:02d}:{i%60:02d}Z"
    row.set_cell("hist", ts.encode(), struct.pack(">d", 20.0 + i*0.001))
row.commit()

# ------------------------------------------------------------------
# ROWS ARE SORTED LEXICOGRAPHICALLY. That order is THE ONLY INDEX.
# ------------------------------------------------------------------
rows = table.read_rows(
    start_key=b"device#d-0042#2026-11-30",
    end_key=b"device#d-0042#2026-12-01")
for r in rows:
    pass
# A CONTIGUOUS RANGE SCAN. This and a point lookup are the only
# efficient reads Bigtable has. Module 2 is about making your
# queries fit that shape.

# A prefix, which is the same thing expressed more clearly:
rows = table.read_rows(row_set=RowSet())   # or:
from google.cloud.bigtable.row_set import RowRange
rs = RowSet()
rs.add_row_range(RowRange(start_key=b"device#d-0042#",
                          end_key=b"device#d-0042$"))
#                                                 ^ '$' sorts just
#                                                 after '#': a prefix
#                                                 range, done right
rows = table.read_rows(row_set=rs)

# ------------------------------------------------------------------
# THE LIMITS. They bound every design.
# ------------------------------------------------------------------
# 1. ROW KEY: 4 KB. And THE KEY IS STORED WITH EVERY CELL, so key
#    length is a REAL STORAGE COST - a 200-byte key on a table with
#    10 billion cells is 2 TB of keys. Keep keys short:
#      BAD : "device_identifier#d-0042#reading_timestamp#2026-11-30T14:04:00.000Z"
#      GOOD: "d0042#20261130140400"

# 2. *** A SINGLE ROW: 256 MB max, 100 MB for good performance. AND
#    *** A ROW IS NEVER SPLIT ACROSS TABLETS. ***
#    So a row that grows without bound is a TABLET THAT CANNOT BE
#    SPLIT - the worst shape in Bigtable, because no amount of
#    rebalancing or extra nodes can help it. Demonstrate the latency:
#      small row (10 cells)    read p99  ___ ms
#      large row (1M cells)    read p99  ___ ms
#    The difference is why "bound the row" is the rule.

# 3. CELL VALUE: 100 MB, 10 MB for good performance.
# 4. COLUMN FAMILIES: ~100 per table, fewer is better.
# 5. PER NODE: roughly 10,000 reads OR writes/second for small rows
#    on SSD, plus a storage limit per node by storage type.
#    -> capacity planning is nodes = throughput / 10,000, and
#       Module 4 is about why you rarely get the full 10,000.

# ------------------------------------------------------------------
# WHAT THE EMULATOR CANNOT DO. Try each; record the error.
# ------------------------------------------------------------------
#   no tablet splitting or rebalancing
#   no node scaling
#   no replication or multi-cluster routing
#   no backups
#   no KEY VISUALIZER  <- the most important gap; Module 4 needs it
#   no realistic latency
# Modules 4, 5 and 6 need a real instance. Plan the spend.

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.