What CockroachDB Does
- Serializable transactions — strongest ACID guarantee
- PostgreSQL compatibility — wire protocol + most SQL features
- Auto-sharding — data split into ranges, auto-rebalanced
- Multi-region — place data near users via zone configs
- Raft replication — configurable replica count per table
- Online schema changes — non-blocking DDL
- Time travel queries — AS OF SYSTEM TIME
- Follower reads — read from closest replica for low latency
- CDC — change data capture stream
Architecture
Each node is identical (no primary/secondary). Data is split into 64MB ranges, each replicated via Raft to typically 3 nodes. SQL layer sits on top of the distributed KV layer. DistSQL executes queries across nodes in parallel.
Self-Hosting
# Secure 3-node cluster
cockroach start --certs-dir=certs --listen-addr=node1:26257 \
--advertise-addr=node1 \
--join=node1,node2,node3
# Init cluster
cockroach init --certs-dir=certs --host=node1Key Features
- Distributed SQL with serializable transactions
- PostgreSQL wire compatibility
- Multi-region with geo-partitioning
- Automatic sharding and rebalancing
- Zero-downtime upgrades
- Online schema changes
- Backup and restore
- Row-level TTL
- Change data capture
- BACKUP to cloud storage
Comparison
| Database | Inspired By | Compatibility | License |
|---|---|---|---|
| CockroachDB | Spanner | PostgreSQL | BUSL + CCL |
| YugabyteDB | Spanner | PostgreSQL | Apache 2.0 |
| TiDB | Spanner + HTAP | MySQL | Apache 2.0 |
| Spanner | Own | Managed only | Proprietary |
| Citus (Postgres ext) | Postgres | PostgreSQL | Apache 2.0 |
FAQ
Q: A PostgreSQL drop-in? A: Most PostgreSQL features are compatible. Incompatibilities include: stored procedures (limited support), certain types, and some PG extensions. Test before migrating.
Q: Performance overhead? A: Distributed transactions are slower than single-machine (latency ≈ 2-3 network RTTs). But the horizontal scaling capability far surpasses a single machine. For small apps, single-node Postgres is faster and cheaper.
Q: BUSL restrictions? A: Source is readable and self-usable, but you cannot run a commercial managed service that competes with Cockroach Cloud. Automatically converts to Apache 2.0 after 4 years.
Sources
- Docs: https://www.cockroachlabs.com/docs
- GitHub: https://github.com/cockroachdb/cockroach
- License: BUSL 1.1