| name | cockroachdb-patterns |
| description | CockroachDB distributed SQL — PostgreSQL compatible, serializable isolation, geo-partitioning, multi-region. Use when working with cockroachdb patterns. |
| domain | development |
| author | oyi77 |
| license | Apache-2.0 |
| subdomain | software-development |
| tags | ["cockroachdb","coding","patterns","software-engineering","testing"] |
| version | 1.0.0 |
Overview
CockroachDB is a distributed SQL database that's wire-compatible with PostgreSQL. It provides serializable isolation, automatic sharding, geo-partitioning, and multi-region deployment out of the box.
Capabilities
- PostgreSQL wire protocol compatibility
- Serializable isolation by default
- Automatic data sharding and rebalancing
- Geo-partitioning for data locality
- Multi-region with zone configurations
- Changefeeds for CDC (Change Data Capture)
- Follower reads for low-latency reads
- Online schema changes
When to Use
Trigger phrases:
-
"cockroachdb patterns"
-
"CockroachDB distributed SQL — PostgreSQL compatible, serializable isolation, geo"
-
Need distributed SQL with PostgreSQL compatibility
-
Multi-region deployments with data locality requirements
-
Want serializable isolation without performance penalty
-
Migrating from PostgreSQL to distributed architecture
When NOT to Use
- Task is about deployment, not development (use deploy skills)
- Task is about code review, not writing (use review skills)
- You need to understand existing code first (use research skills)
- Task is about testing only (use test skills)
- Requirements are unclear (clarify first)
- Task is trivially simple (single line fix)
Pseudo Code
The cockroachdb-patterns workflow follows a standard pipeline pattern.
Core flow:
# cockroachdb-patterns primary flow
input = prepare(raw_data)
result = process(input, config={cockroachdb, compatible, distributed, isolation, multi})
validate(result)
deliver(result)
Error handling:
on error:
log(error_details)
retry_with_backoff(max=3)
if still_failing: alert_and_escalate()
Connection
import { Pool } from 'pg';
const pool = new Pool({
connectionString: 'postgresql://root@localhost:26257/defaultdb?sslmode=disable',
});
Geo-Partitioning
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name STRING,
region STRING NOT NULL
) PARTITION BY LIST (region);
ALTER TABLE users PARTITION BY LIST (region) (
PARTITION us VALUES IN ('us-east', 'us-west'),
PARTITION eu VALUES IN ('eu-west', 'eu-central'),
PARTITION apac VALUES IN ('ap-southeast', 'ap-northeast')
);
ALTER PARTITION us OF TABLE users CONFIGURE ZONE USING
constraints = '[+region=us-east1]';
Changefeeds
CREATE CHANGEFEED FOR users INTO 's3://bucket/changefeed'
WITH format = 'json', resolved = '10s';
CREATE CHANGEFEED FOR users INTO 'kafka://broker:9092'
WITH format = 'json', topic_name = 'users';
Follower Reads
SELECT * FROM users AS OF SYSTEM TIME follower_read_timestamp();
SELECT * FROM users AS OF SYSTEM TIME '-5s';
Multi-Region Setup
ALTER DATABASE mydb SET PRIMARY REGION "us-east1";
ALTER DATABASE mydb ADD REGION "eu-west1";
ALTER DATABASE mydb ADD REGION "ap-southeast1";
ALTER TABLE config SET LOCALITY GLOBAL;
ALTER TABLE users SET LOCALITY REGIONAL BY ROW;
Common Patterns
- Schema migrations: Use
flyway or migrate with CockroachDB dialect
- Connection pooling: Use PgBouncer or built-in connection handling
- Serializable retries: Wrap transactions in retry loops for serialization errors
- Performance: Use
EXPLAIN ANALYZE to optimize distributed queries
How to Use
- Understand the requirement and existing codebase patterns
- Design the solution with error handling and testability in mind
- Implement incrementally with tests for each change
- Verify against expected outcomes (manual and automated)
- Document usage, edge cases, and integration points
- Review with team before merging to shared branches
Red Flags
- Skipping tests to ship faster: Untested code breaks in production when you least expect it
- No error handling in production code: Unhandled errors crash services and lose user data
- Hardcoded configuration values: Hardcoded values prevent environment switching and leak secrets
- Ignoring security implications: Missing input validation, auth bypasses, and injection vulnerabilities
- Over-engineering simple solutions: Premature abstraction adds complexity without proportional benefit
Verification
Process
- Analyze the task requirements
- Apply domain expertise
- Verify output quality
Anti-Rationalization Table
| Rationalization | Reality |
|---|
| "Tests slow me down" | Bugs slow you down 10x more. Tests are speed, not overhead. |
| "I will refactor later" | Technical debt compounds. Refactor as you go. |
| "It works on my machine" | If it is not in CI, it does not work. Ship proof, not claims. |