DEV Community

Alex Spinov
Alex Spinov

Posted on

CockroachDB Has a Free Distributed SQL Database — PostgreSQL That Survives Failures

Your PostgreSQL instance fails at 3 AM. CockroachDB doesn't — it automatically replicates, rebalances, and self-heals across nodes. And it speaks PostgreSQL.

What is CockroachDB?

CockroachDB is a distributed SQL database that's wire-compatible with PostgreSQL. It automatically distributes data across nodes, handles failover, and supports multi-region deployments — all while using standard SQL.

Why CockroachDB

1. PostgreSQL Compatible

import psycopg2

conn = psycopg2.connect(os.environ['DATABASE_URL'])
cur = conn.cursor()
cur.execute("""
    CREATE TABLE IF NOT EXISTS users (
        id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
        name TEXT NOT NULL,
        email TEXT UNIQUE,
        created_at TIMESTAMPTZ DEFAULT now()
    )
""")
cur.execute("INSERT INTO users (name, email) VALUES (%s, %s)", ('Alice', 'alice@example.com'))
conn.commit()
Enter fullscreen mode Exit fullscreen mode

Use psycopg2, pg, Prisma, Drizzle — any PostgreSQL client works.

2. Automatic Sharding

-- Data automatically distributed across nodes
-- No manual sharding configuration needed
CREATE TABLE orders (
    id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
    customer_id UUID REFERENCES customers(id),
    total DECIMAL(10,2),
    created_at TIMESTAMPTZ DEFAULT now()
);

-- CockroachDB automatically:
-- 1. Splits data into ranges
-- 2. Distributes ranges across nodes
-- 3. Rebalances when nodes are added/removed
Enter fullscreen mode Exit fullscreen mode

3. Multi-Region

-- Place data near your users
ALTER DATABASE myapp SET PRIMARY REGION "us-east1";
ALTER DATABASE myapp ADD REGION "eu-west1";
ALTER DATABASE myapp ADD REGION "ap-southeast1";

-- Table-level locality
ALTER TABLE users SET LOCALITY REGIONAL BY ROW;
-- Each row is stored in the user's closest region
Enter fullscreen mode Exit fullscreen mode

4. Serializable Transactions

-- Strongest isolation level by default
BEGIN;
SELECT balance FROM accounts WHERE id = 'alice';
-- Another transaction can't read stale data
UPDATE accounts SET balance = balance - 100 WHERE id = 'alice';
UPDATE accounts SET balance = balance + 100 WHERE id = 'bob';
COMMIT;
-- No phantom reads, no write skew, no lost updates
Enter fullscreen mode Exit fullscreen mode

5. Survivability

3-node cluster:
- 1 node fails → zero downtime, automatic failover
- Network partition → majority side continues serving

5-node cluster:
- 2 nodes fail → still fully operational
- Data automatically re-replicated to healthy nodes
Enter fullscreen mode Exit fullscreen mode

Free Tier (CockroachDB Cloud)

Feature Free
Storage 10 GiB
Request Units 50M/month
Regions 1
Nodes Serverless

CockroachDB vs PostgreSQL vs Cloud Spanner

CockroachDB PostgreSQL Cloud Spanner
Distributed Yes (automatic) No (single node) Yes
Multi-region Built-in Manual replication Built-in
PostgreSQL wire Yes Native No (custom)
Serializable Default Opt-in Default
Failover Automatic Manual/Patroni Automatic
License BSL / Free tier PostgreSQL License Proprietary

Getting Started

# Cloud (free)
# Sign up at cockroachlabs.com → Create cluster → Connect

# Self-hosted
cockroach start-single-node --insecure
cockroach sql --insecure
Enter fullscreen mode Exit fullscreen mode

The Bottom Line

CockroachDB is PostgreSQL that can't die. Automatic sharding, multi-region, serializable transactions, and self-healing — all with standard SQL syntax.


Need data tools? I build scraping solutions. Check my Apify actors or email spinov001@gmail.com.

Top comments (0)