CockroachDB is a distributed SQL database that survives datacenter failures without manual intervention. It's PostgreSQL-compatible and offers a free Serverless tier with 10 GiB storage and 50M Request Units/month.
Why CockroachDB?
- Distributed by default — data automatically replicated across nodes
- PostgreSQL compatible — use any Postgres driver or ORM
- Survives failures — automatic failover, zero downtime
- Free Serverless tier — 10 GiB, 50M RU/month, no credit card
Quick Start: CockroachDB Serverless
# Sign up at cockroachlabs.cloud (free tier)
# Get your connection string, then:
# Connect with psql
psql 'postgresql://user:pass@free-tier.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full'
# Or use cockroach CLI
curl https://binaries.cockroachdb.com/cockroach-latest.linux-amd64.tgz | tar xz
./cockroach sql --url 'postgresql://...'
SQL API (It's Just Postgres!)
-- Create a multi-region table
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email STRING UNIQUE NOT NULL,
name STRING NOT NULL,
region STRING NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
-- CockroachDB-specific: locality-aware index
CREATE INDEX idx_users_region ON users (region, email);
-- Insert data
INSERT INTO users (email, name, region) VALUES
('alice@example.com', 'Alice', 'us-east'),
('bob@example.com', 'Bob', 'eu-west');
-- Distributed joins work seamlessly
SELECT u.name, COUNT(o.id) as order_count
FROM users u JOIN orders o ON u.id = o.user_id
GROUP BY u.name;
REST API via CockroachDB Cloud
# Cluster info
curl -H "Authorization: Bearer $COCKROACH_API_KEY" \
https://cockroachlabs.cloud/api/v1/clusters
# SQL over HTTP (Serverless)
curl -X POST 'https://your-cluster.cockroachlabs.cloud:8080/api/v2/sql' \
-H 'Authorization: Bearer $API_KEY' \
-H 'Content-Type: application/json' \
-d '{"statements": [{"sql": "SELECT count(*) FROM users"}]}'
Python Example
import psycopg2
conn = psycopg2.connect(
'postgresql://user:pass@free-tier.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full'
)
cur = conn.cursor()
# Works like Postgres — with distributed magic underneath
cur.execute("SELECT * FROM users WHERE region = %s", ('us-east',))
for row in cur.fetchall():
print(row)
conn.close()
Key Features
| Feature | Details |
|---|---|
| Compatibility | PostgreSQL wire protocol |
| Distribution | Automatic sharding + replication |
| Consistency | Serializable isolation (strongest) |
| Survival | Node, zone, region failure |
| Free tier | 10 GiB, 50M RU/month |
| Scaling | Automatic on Serverless |
When to Choose CockroachDB
- Multi-region apps needing low-latency reads globally
- Financial systems requiring serializable consistency
- Apps that cannot tolerate any downtime
- Teams that want Postgres compatibility without managing replication
Resources
Building data-intensive applications? I create custom data pipelines, scrapers, and integrations. Check out my tools on Apify or reach out at spinov001@gmail.com.
Top comments (0)