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: PostgreSQL 的 drop-in? A: 大部分 PostgreSQL 功能兼容。不兼容的有:存储过程(支持有限)、某些类型、一些 PG 扩展。迁移前测试。
Q: 性能开销? A: 分布式事务比单机慢(延迟≈网络 RTT 2-3 次)。但横向扩展能力远超单机。小应用单机 Postgres 更快更便宜。
Q: BUSL 限制? A: 源码可读、可自用,但不能做商用托管服务与 Cockroach Cloud 竞争。4 年后自动转 Apache 2.0。
来源与致谢 Sources
- Docs: https://www.cockroachlabs.com/docs
- GitHub: https://github.com/cockroachdb/cockroach
- License: BUSL 1.1