CockroachDB Cheat Sheet
CockroachDB distributed SQL database covering cluster setup, transactions, geo-partitioning, and PostgreSQL-compatible syntax.
2 PagesAdvancedMay 20, 2026
Starting a Local Cluster
Spin up a 3-node insecure cluster for local development.
bash
cockroach start --insecure --store=node1 --listen-addr=localhost:26257 \ --http-addr=localhost:8080 --join=localhost:26257,localhost:26258,localhost:26259 &cockroach start --insecure --store=node2 --listen-addr=localhost:26258 \ --http-addr=localhost:8081 --join=localhost:26257,localhost:26258,localhost:26259 &cockroach start --insecure --store=node3 --listen-addr=localhost:26259 \ --http-addr=localhost:8082 --join=localhost:26257,localhost:26258,localhost:26259 &cockroach init --insecure --host=localhost:26257cockroach sql --insecure --host=localhost:26257
Transaction Retry Handling
CockroachDB uses serializable isolation and may require client-side retries.
sql
BEGIN;SAVEPOINT cockroach_restart;UPDATE accounts SET balance = balance - 100 WHERE id = 1;UPDATE accounts SET balance = balance + 100 WHERE id = 2;RELEASE SAVEPOINT cockroach_restart;COMMIT;-- On a 40001 (serialization_failure) error, client should:-- ROLLBACK TO SAVEPOINT cockroach_restart; and retry the statements.-- Most drivers (pgx, node-postgres via cockroach adapters) do this automatically.
Geo-Partitioning
Pin rows to specific regions for data locality and compliance.
sql
ALTER TABLE users ADD COLUMN region STRING NOT NULL DEFAULT 'us-east';ALTER TABLE users PARTITION BY LIST (region) ( PARTITION us (VALUES IN ('us-east', 'us-west')), PARTITION eu (VALUES IN ('eu-west')));ALTER PARTITION us OF TABLE users CONFIGURE ZONE USING constraints = '[+region=us-east1]';ALTER PARTITION eu OF TABLE users CONFIGURE ZONE USING constraints = '[+region=eu-west1]';
Distributed SQL Concepts
What makes CockroachDB behave differently from single-node Postgres.
- Range- the unit of data distribution (~512MB by default), automatically split and rebalanced
- Raft consensus- each range is replicated (typically 3x) and agrees on writes via Raft
- Serializable isolation- the only isolation level; strongest consistency guarantee, may cause retryable errors under contention
- SQL_ERROR 40001- serialization failure code the client must catch and retry
- Follower reads / AS OF SYSTEM TIME- read slightly stale data from the nearest replica for lower latency
- Zone configs- control replica placement, count, and lease preferences per table/partition
Pro Tip
Use `AS OF SYSTEM TIME follower_read_timestamp()` for read-heavy, latency-sensitive queries that can tolerate a few seconds of staleness — it lets CockroachDB serve from the nearest replica instead of routing to the leaseholder, often cutting cross-region read latency dramatically.
Was this cheat sheet helpful?
Explore Topics
#CockroachDB#CockroachDBCheatSheet#Database#Advanced#StartingALocalCluster#TransactionRetryHandling#GeoPartitioning#DistributedSQLConcepts#Databases#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance