CockroachDB
CockroachDB is a distributed SQL database. It speaks the PostgreSQL wire protocol and most of its SQL, but underneath, every table is split into key ranges that are replicated with Raft and spread across a cluster of identical nodes. It was designed after Google's Spanner, with one big difference: it runs on ordinary servers in any cloud or data center, without atomic clocks or GPS receivers.
A few properties are worth separating:
- Postgres-compatible SQL: You connect with a Postgres driver, write normal SQL with joins, secondary indexes and foreign keys, and use transactions. Compatibility is broad but not complete, so not every Postgres extension or feature is there.
- Horizontally scalable: Data is divided into ranges of up to 512 MiB by default. Ranges split as they grow or get busy, and the cluster rebalances them across nodes automatically. Adding a node adds both storage and throughput for reads and writes.
- Consensus-replicated: Every range has (by default) three replicas on different nodes, kept in sync by Raft. A write commits once a majority has it, so a single node, or with the right setup a whole zone or region, can fail without losing committed data or availability.
- Serializable by default: Transactions run at
SERIALIZABLEisolation unless you choose otherwise. That's stronger than Postgres's default, and it comes with a cost you must design for: transactions sometimes have to be retried. - Multi-region aware: You can declare which regions a database lives in, whether it should survive a zone or a region failure, and where each table's or row's data should be placed, all in SQL.
In interviews, CockroachDB is a strong answer when a design needs relational data and transactions at more write volume or more availability than a single Postgres primary gives, especially across regions, and the company isn't on Google Cloud (where Spanner is the equivalent). It's also a great concrete example to use when an interviewer asks how distributed SQL works at all. We'll go through the layers, how reads and writes flow, how it handles clocks, and what to say about it.
The Data Model
To your application, CockroachDB looks like Postgres:
CREATE TABLE accounts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
owner STRING NOT NULL,
balance INT8 NOT NULL CHECK (balance >= 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE transfers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
from_id UUID NOT NULL REFERENCES accounts(id),
to_id UUID NOT NULL REFERENCES accounts(id),
amount INT8 NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
INDEX transfers_from_idx (from_id, created_at DESC)
);
But what is actually happening under the hood? Every table and index is mapped onto one big, sorted key-value space. A row in accounts becomes a key like /accounts/primary/<id> with the other columns as the value. An index entry in transfers_from_idx becomes /transfers/transfers_from_idx/<from_id>/<created_at>/<id>. That key space is sorted and cut into contiguous ranges, and each range is replicated and placed independently.
Two consequences follow, and both are worth saying out loud in an interview:
- The primary key decides where rows live. Rows with adjacent keys share a range. A sequential key (a timestamp, or an ever-increasing integer) sends all inserts to the last range, which becomes a hotspot on one node. That's why the example uses random UUIDs. For an index whose leading column is naturally sequential, such as
created_at, CockroachDB offers hash-sharded indexes (USING HASH), which prefix the key with a hidden bucket number so writes spread across ranges. - A secondary index lives in different ranges from its table. Inserting a transfer writes the row and its index entry, likely in different ranges on different nodes, which makes every such write a distributed transaction. CockroachDB handles that transparently, but it's why each extra index costs more than it does on a single-node database.
CockroachDB's integer
SERIALcolumns useunique_rowid(), which produces roughly time-ordered values. They're unique, but they hotspot under heavy inserts just like a timestamp. In a design, default to UUID primary keys and say why.