Skip to content
Navigation

Type at least two characters. Search covers page titles, headings, tags and database names.

↑ ↓ to navigateEnter to openEsc to close0 pages
Distributed SQLadvanced

CockroachDB Architecture

Ranges, replicas, leaseholders and the layered design that turns SQL into distributed key-value operations.

2 min readAdvancedUpdated Edit this page

CockroachDB presents a PostgreSQL-compatible SQL interface over a distributed, transactional key-value store.

Raft replication of a single rangeA client writes to the Raft leader, which appends the entry to its log and replicates it to two followers. Once a majority has persisted the entry it is committed and applied to the state machine, then acknowledged to the client.writeAppendEntriesAppendEntriescommit ackClientLeaderterm 7, log index 42Follower 1log index 42Follower 2log index 41
Raft replication of a single range

Layers

  1. SQL — parses, plans and executes, translating rows into key-value operations.
  2. Transactional KV — provides serializable transactions across arbitrary keys.
  3. Distribution — maps keys to ranges and routes requests.
  4. Replication — Raft consensus per range.
  5. Storage — Pebble, an embedded LSM engine derived from RocksDB.

Ranges and replicas

The keyspace is divided into ranges — contiguous key spans, split when they exceed a size threshold. Each range is replicated to (by default) three nodes, forming a Raft group.

One replica per range holds the leaseholder role. It serves reads without consensus, because it knows it holds a valid lease, and coordinates writes through Raft. Separating the leaseholder from the Raft leader is an optimisation — placing the leaseholder near the traffic reduces read latency without moving the whole group.

SHOW RANGES FROM TABLE orders;
SELECT range_id, lease_holder, replicas FROM crdb_internal.ranges LIMIT 10;

What this means for queries

  • A read served by a leaseholder near the client is fast — no consensus needed.
  • A write always requires a Raft majority.
  • A query touching one range is a single-node operation; one touching many fans out.
  • Ranges split automatically as data grows, and rebalance across nodes.

The hotspot problem

Node roles

All nodes are equal — every one runs all layers and can serve any query. There is no coordinator node to lose. Cluster metadata is itself stored in ranges, replicated the same way.

Clock dependency

CockroachDB uses hybrid logical clocks and requires bounded clock skew between nodes (--max-offset, 500 ms by default).