CockroachDB — Distributed SQL for the Global Cloud
CockroachDB is a cloud-native, distributed SQL database designed for high availability, effortless horizontal scale, and geographic data placement. PostgreSQL-compatible wire protocol with serializable transactions across regions.
What it is
CockroachDB is a distributed SQL database designed for the cloud. Inspired by Google Spanner, it offers serializable transactions, horizontal scale, automatic sharding, and survivability across node, zone, and region failures. It uses the PostgreSQL wire protocol, so most PostgreSQL drivers and ORMs work without modification.
CockroachDB targets teams building globally distributed applications that need strong consistency guarantees without the operational complexity of manual sharding or replication configuration.
How it saves time or tokens
CockroachDB eliminates the operational burden of manual sharding, replication setup, and failover configuration. You start a cluster, create tables with standard SQL, and CockroachDB handles data distribution, replication, and rebalancing automatically. Adding capacity means adding nodes; the cluster rebalances without downtime. The PostgreSQL-compatible interface means no new query language to learn and no application code changes when migrating from PostgreSQL.
How to use
- Install and start a single-node cluster for development:
curl https://binaries.cockroachdb.com/cockroach-v24.3.0.darwin-10.9-amd64.tgz | tar -xz
sudo cp cockroach-v24.3.0.darwin-10.9-amd64/cockroach /usr/local/bin
cockroach start-single-node --insecure --listen-addr=localhost:26257 \
--http-addr=localhost:8080 --store=cockroach-data
- Connect with the SQL shell:
cockroach sql --insecure --host=localhost:26257
- Create tables using standard PostgreSQL syntax:
CREATE DATABASE myapp;
USE myapp;
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
INSERT INTO users (email) VALUES ('user@example.com');
SELECT * FROM users;
Example
Multi-region configuration for geo-distributed applications:
-- Set up multi-region locality
ALTER DATABASE myapp SET PRIMARY REGION 'us-east';
ALTER DATABASE myapp ADD REGION 'eu-west';
ALTER DATABASE myapp ADD REGION 'ap-south';
-- Pin data to regions for compliance
ALTER TABLE users SET LOCALITY REGIONAL BY ROW;
INSERT INTO users (email, crdb_region)
VALUES ('eu-user@example.com', 'eu-west');
-- This row is stored and served from EU nodes
Related on TokRepo
- Database tools — More database tools and platforms on TokRepo.
- DevOps tools — Infrastructure and deployment tools.
Common pitfalls
- Running CockroachDB on a single node in production defeats its purpose. Deploy at least 3 nodes for fault tolerance.
- Not understanding CockroachDB's licensing: BSL 1.1 for some features means enterprise features require a license after evaluation.
- Using CockroachDB for workloads with very high single-row update rates. CockroachDB's distributed consensus adds latency to writes that single-node databases do not have.
Frequently Asked Questions
CockroachDB uses the PostgreSQL wire protocol and supports most PostgreSQL syntax. Most PostgreSQL drivers, ORMs (ActiveRecord, SQLAlchemy, GORM), and tools work without modification. Some PostgreSQL-specific features like extensions are not supported.
CockroachDB supports multi-region configurations where you define primary and secondary regions. Data can be pinned to specific regions for compliance, and reads are served from the closest region. Failover between regions is automatic.
CockroachDB provides serializable isolation, the strongest SQL isolation level. All transactions see a consistent snapshot of the database, even across distributed nodes and regions.
CockroachDB core is available under the BSL 1.1 license, which allows free use but restricts offering it as a managed service. Some enterprise features require a commercial license. The CCL (CockroachDB Community License) covers additional features.
Add nodes to the cluster and CockroachDB automatically rebalances data across all nodes. No manual sharding, no downtime, no configuration changes. The cluster adapts to the new capacity within minutes.
Citations (3)
- CockroachDB GitHub— CockroachDB distributed SQL database
- CockroachDB Docs— CockroachDB multi-region architecture
- Google Research— Google Spanner distributed database design
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.