Lab 10: CockroachDB — NewSQL

Time: 50 minutes | Level: Architect | DB: CockroachDB


🎯 Objective

Explore CockroachDB's distributed SQL architecture: Raft consensus, range distribution, geo-partitioning, and SQL compatibility with PostgreSQL. Run EXPLAIN for distributed query plans, SHOW RANGES, and zone configurations.


📚 Background

CockroachDB Architecture

SQL Layer (PostgreSQL-compatible)

  Transaction Layer (MVCC, Serializable)

  Distribution Layer (ranges, 512MB chunks)

  Replication Layer (Raft consensus per range)

    Storage Layer (Pebble LSM, RocksDB-based)

Key concepts:

  • Range: 512 MB data chunk, replicated 3x via Raft

  • Leaseholder: One replica that coordinates reads/writes for a range

  • Raft group: Each range has its own Raft group for consensus

  • Node: Physical server running CockroachDB process

Why CockroachDB?

Feature
PostgreSQL
CockroachDB

Distribution

Single node

Automatic sharding

Scaling

Vertical + manual replicas

Horizontal + automatic

Failover

Multi-AZ setup required

Built-in (Raft)

Transactions

ACID

Distributed ACID (Serializable)

SQL Compatibility

PostgreSQL

PostgreSQL-compatible

Geo-partitioning

No

Yes (Enterprise)

Use case

General purpose

Globally distributed OLTP


Step 1: Start CockroachDB (Single Node)

📸 Verified Output:


Step 2: Create Schema (PostgreSQL-compatible SQL)

📸 Verified Output:


Step 3: SHOW RANGES — View Data Distribution

📸 Verified Output:

💡 In production with multiple nodes, each table spans multiple ranges across nodes. CockroachDB automatically splits ranges when they exceed 512 MB.


Step 4: EXPLAIN — Distributed Query Plan

📸 Verified Output:


Step 5: Transactions — Serializable Isolation

📸 Verified Output:


Step 6: Zone Configurations (Geo-Distribution Concepts)

📸 Verified Output:


Step 7: SHOW JOBS & Schema Changes

📸 Verified Output:


Step 8: Capstone — CockroachDB vs PostgreSQL Decision

📸 Verified Output:


Summary

Concept
Key Takeaway

Range

512 MB data chunk replicated 3x via Raft across nodes

Leaseholder

Replica that coordinates reads/writes for a range

Raft consensus

Each range has independent Raft group for durability

SHOW RANGES

Displays data distribution and leaseholder info

EXPLAIN

Shows distributed query plan (including cross-node joins)

SHOW JOBS

Tracks background schema changes and statistics jobs

Online schema changes

ALTER TABLE never blocks reads/writes

REGIONAL BY ROW

Row-level geo-partitioning for data residency compliance

Serializable

Default isolation level (stricter than PostgreSQL default)

💡 Architect's insight: CockroachDB's "Postgres-compatible" isn't 100% — some extensions, functions, and behaviors differ. Always test your specific workload before migrating. The killer feature is surviving AZ/region failures with zero manual intervention.

Last updated