What is CockroachDB

CockroachDB is a SQL Database

What Problem it solves?
Traditional relational databases (e.g. PostgreSQL, MySQL) are strong on SQL and ACID transactions but are hard to scale horizontally and do not survive datacenter failures without significant operational overhead. NoSQL databases (e.g. Cassandra, DynamoDB) scale well but sacrifice ACID transactions and familiar SQL semantics. CockroachDB is designed to give you both:
  Horizontal scalability(Add nodes to grow storage and throughput linearly)
  Survivability(Tolerates disk, node, rack, and datacenter failures automatically)

When to choose CockroachDB
- Applications that need PostgreSQL semantics at scale (multi-region, high availability)
- Systems where zero-downtime rolling upgrades are mandatory
- Workloads requiring strong ACID guarantees that span multiple rows/tables
- Scenarios where geo-partitioning or locality-aware placement is needed

Architecture


                 Application
                     |   /\
                     \/   |
<----------------CockroachDB Cluster-------------------------------------------->

--AWSRegion:Mumbai. AZ-1-     --AWSRegion:NYC. AZ-2-     --AWSRegion:Japan. AZ-3-
| EC2(CockroachDB node)|    | EC2(CockroachDB node) |    |EC2(CockroachDB node) |
------------------------       ---------------------      ------------------------
      
Architecture

Build & Run CockroachDB from source

Build from source


curl -Lo /tmp/bazelisk \
  https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64
chmod +x /tmp/bazelisk
sudo mv /tmp/bazelisk /usr/local/bin/bazel
bazel version
dpkg -l ccache 2>/dev/null | grep -q "^ii" && sudo apt remove --purge ccache -y || echo "ccache not installed, OK"

# ── Step 3: Install crlfmt (Go code formatter used by CockroachDB) ──────────
go install github.com/cockroachdb/crlfmt@latest

# ── Step 4: Run the environment doctor ──────────────────────────────────────
# dev doctor checks Go version, Bazelisk, and other prerequisites and tells you
# exactly what is missing. Fix anything it reports before building.
cd ~/cockroach
./dev doctor

# ── Step 5: Build the binary ────────────────────────────────────────────────
# "short" builds cockroach without the web UI — much faster, good for backend work.
# Output: ./cockroach and ./cockroach-short in the repo root.
./dev build short
      

Run Cockroachdb


# ── Foreground (logs print to terminal + log files) ──────
# Print WARNING+ to stderr (default — minimal noise)
./cockroach start-single-node --insecure --listen-addr=localhost:26257 --logtostderr=WARNING

# Print INFO+ to stderr (recommended during development)  ****DO THIS****
./cockroach start-single-node --insecure --listen-addr=localhost:26257--logtostderr=INFO

# ── Background (returns immediately; logs only go to files) ──────────────────
./cockroach start-single-node --insecure --listen-addr=localhost:26257 --background

# ── Foreground with all INFO logs also printed to stderr ─────────────────────
# Best option for development: you see everything as it happens in the terminal.
./cockroach start-single-node --insecure --listen-addr=localhost:26257 \
  --logtostderr=INFO
      

Log Files

CockroachDB splits logs across multiple files by **log channel**. All files are in `cockroach-data/logs/`.


# Follow the main log (startup messages, node events, errors)
tail -f cockroach-data/logs/cockroach.log

| File (symlink) | Channel | What it contains |
|---|---|---|
| `cockroach.log` | DEV + OPS | Main server log — startup, node events, most errors |
| `cockroach-health.log` | HEALTH | Periodic health samples, liveness heartbeats |
| `cockroach-pebble.log` | STORAGE | Pebble (storage engine) internal events |
| `cockroach-kv-exec.log` | KV_EXEC | KV-layer execution events |
| `cockroach-kv-distribution.log` | KV_DISTRIBUTION | Range leases, rebalancing, Raft |
| `cockroach-sql-schema.log` | SQL_SCHEMA | DDL events (CREATE TABLE, ALTER TABLE, etc.) |
| `cockroach-stderr.log` | — | Anything written to stderr |
      

Doing code change


// Add log line 
pkg/cli/cli.go
	ctx := context.Background()
	log.Ops.Shoutf(ctx, severity.INFO, "Amit cli::Main()")

// Rebuild
./dev build short

// Start again
./cockroach start-single-node --insecure --listen-addr=localhost:26257 --logtostderr=INFO
      

Code walk


cli.Main()  //pkg/cmd/cockroach/main.go
  doMain()  //pkg/cli/cli.go
    Run() 
      runStart() //pkg/cli/start.go
        runStartInternal()  //pkg/cli/start.go