Deploy CockroachDB or YugabyteDB on Linux dedicated servers across at least three independent bare-metal nodes synchronized with chrony NTP and low-latency private networking. Both distributed SQL engines use Raft consensus to replicate data synchronously and survive full server failures with zero RPO. Choose CockroachDB for PostgreSQL-wire compatibility with automatic rebalancing, or YugabyteDB for deeper PostgreSQL feature parity.
Quick Answer: Distributed SQL (CockroachDB & YugabyteDB) on Bare-Metal Linux
Distributed NewSQL databases such as CockroachDB and YugabyteDB rely on Raft consensus heartbeats and Hybrid Logical Clocks (HLC) across at least three independent nodes. Running them on bare-metal Linux dedicated servers with local PCIe Gen4 NVMe SSDs, chrony sub-millisecond NTP discipline, vm.swappiness=1, and private 10Gbps VLANs eliminates cloud EBS tail-latency spikes that otherwise trigger false Raft leader elections and query stalls.
Core Stack: 3x Ubuntu 24.04 LTS bare-metal nodes, CockroachDB or YugabyteDB, XFS/ext4 on local NVMe, chrony clock sync, mTLS node certificates, and HAProxy/MetalLB SQL routing.
Distributed SQL engines rely on Raft consensus heartbeats and tight clock synchronization that falter on noisy virtualized disks and shared networks. Bare-metal Linux with local NVMe drives delivers predictable quorum performance, provided chrony NTP offsets, mTLS certificates, and swap settings are tuned accurately. Follow this deployment and benchmarking guide.
Variable cloud storage fsync latency triggers false Raft leader elections and unpredictable distributed SQL query pauses.
Drifting system clocks or kernel memory swapping violates Hybrid Logical Clock bounds and crashes cluster nodes.
Lock vm.swappiness=1, audit sub-millisecond chrony offsets, and benchmark Raft write throughput across three NVMe nodes.
1. Prerequisites: Quorum Topology, NVMe Fsync Latency, and Clock Sync
Distributed SQL clusters enforce strict invariants around quorum majority, disk flush latency, and physical clock bounds. Violating any of these prerequisites on bare metal leads to split-brain rejections or degraded write throughput.
Three-Node Quorum Math, NVMe Capacity, and Fsync Latency Sizing
Always provision an odd number of nodes—at minimum three dedicated servers (RF=3) so the cluster tolerates one complete hardware failure while maintaining a 2-of-3 Raft majority, or five nodes to survive two simultaneous failures. Equip each node with at least 8–16 physical CPU cores, 32–64 GB ECC RAM, and dedicated PCIe Gen4 NVMe drives formatted as ext4 or xfs with noatime. Size per-node raw NVMe capacity using the formula: Per-Node Raw Storage = (Logical Dataset × RF 3 × 1.35 Compaction & MVCC Headroom) / (Node Count × 0.70 Max Utilization). For a 1 TB logical database on a 3-node cluster, each node requires roughly 1.93 TB of usable NVMe space so LSM compaction bursts never cross the 80% disk-full safety threshold.
Do not place distributed SQL WAL directories on shared network storage or slow SATA spinning disks; every committed SQL transaction requires a synchronous fsync() across a Raft quorum. Benchmark candidate NVMe drives with fio before cluster initialization to verify 4 KiB synchronous write p99 latency stays below 0.5 ms. Also plan Raft range density: CockroachDB defaults to 512 MiB ranges (with ~10,000–50,000 ranges comfortably managed per 16-core node). If you are designing storage tiers across multiple nodes, compare local NVMe against shared block arrays in our Ceph distributed NVMe storage cluster guide.
Hybrid Logical Clock (HLC) Synchronization and Private Networking
Both CockroachDB and YugabyteDB use Hybrid Logical Clocks to order distributed MVCC transactions without centralized lock managers. If any node’s clock drifts beyond max_offset (typically 250–500 ms), the node automatically shuts itself down to prevent stale reads or linearizability violations. Configure chrony against at least four low-stratum NTP sources (or a local GPS/PTP master) and connect nodes over a dedicated private VLAN or WireGuard/IPsec mesh with round-trip latency under 1–2 ms inside the same datacenter. For deeper insight into eliminating inter-node jitter, see our bare-metal Linux tail latency (p99) analysis.
2. CockroachDB vs. YugabyteDB: Storage Engine and PostgreSQL Parity
While both systems deliver ACID transactions, Raft replication, and PostgreSQL wire compatibility, their internal architectures differ significantly in how they handle PostgreSQL extensions, stored procedures, and storage engines.
| Technical Dimension | CockroachDB (Pebble Engine) | YugabyteDB (DocDB + PG Query Layer) |
|---|---|---|
| PostgreSQL Compatibility | Re-implemented PG wire protocol in Go; strong SQL parity, limited PL/pgSQL | Reuses actual PostgreSQL C query layer; supports triggers, PL/pgSQL, extensions |
| Storage Engine | Pebble (Go LSM-tree inspired by RocksDB) | DocDB (C++ RocksDB-derived distributed document store) |
| Process Architecture | Single symmetric binary per node | yb-master (metadata) + yb-tserver (data/query) |
| Multi-API Support | PostgreSQL SQL only | YSQL (PostgreSQL) + YCQL (Cassandra-compatible) |
| Operational Simplicity | Extremely clean single binary & Web Admin UI | Two daemons per node; YugabyteDB Anywhere or CLI |
Choose YugabyteDB if you are migrating an existing PostgreSQL monolith that relies heavily on triggers, foreign keys with complex PL/pgSQL functions, or extensions like pg_trgm. Choose CockroachDB for greenfield cloud-native Go/Rust/Java services that value single-binary operational simplicity and built-in geo-partitioning controls.
3. Step-by-Step Chrony, Kernel, Certificate, and Cluster Deployment
The following procedure builds a production three-node CockroachDB cluster across three bare-metal Linux servers (10.10.0.11, 10.10.0.12, 10.10.0.13) with mTLS node certificates, tuned kernel VM settings, and systemd supervision. The same OS, chrony, and NVMe tuning steps apply directly to YugabyteDB nodes.
Step 1: Tune Kernel Memory, File Limits, and Chrony on All Three Nodes
Disable swap (or minimize swappiness to 1), raise file descriptor and memory-map limits for Pebble/RocksDB SSTable compaction, and configure chrony to slew clocks smoothly rather than stepping backward.
sudo apt-get update && sudo apt-get install -y chrony curl jq haproxy
# Configure chrony for tight drift control
cat <<'EOF' | sudo tee /etc/chrony/chrony.conf
pool time.cloudflare.com iburst maxsources 4
pool time.google.com iburst maxsources 4
driftfile /var/lib/chrony/chrony.drift
makestep 0.1 3
rtcsync
EOF
sudo systemctl restart chrony
# Kernel tuning for distributed LSM storage engines
cat <<'EOF' | sudo tee /etc/sysctl.d/99-distributed-sql.conf
vm.swappiness = 1
vm.max_map_count = 262144
net.core.somaxconn = 4096
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 10
net.ipv4.tcp_keepalive_probes = 6
EOF
sudo sysctl --system
# Disable Transparent Huge Pages (THP) to prevent compaction latency spikes
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
Step 2: Install CockroachDB and Generate mTLS Cluster Certificates
Create a dedicated cockroach user, mount your dedicated NVMe data volume at /var/lib/cockroach, and generate a private CA plus per-node certificates on your bastion or Node 1 before distributing each node’s keypair securely.
curl -sSL https://binaries.cockroachdb.com/cockroach-v24.1.4.linux-amd64.tgz | tar -xz
sudo cp -i cockroach-v24.1.4.linux-amd64/cockroach /usr/local/bin/
sudo useradd -r -s /usr/sbin/nologin cockroach
sudo mkdir -p /var/lib/cockroach /etc/cockroach/certs /etc/cockroach/ca-key
sudo chown -R cockroach:cockroach /var/lib/cockroach /etc/cockroach
# Generate CA and node certificates (run on admin host, then copy certs to each node)
cockroach cert create-ca --certs-dir=/etc/cockroach/certs --ca-key=/etc/cockroach/ca-key/ca.key
cockroach cert create-node 10.10.0.11 localhost 127.0.0.1 --certs-dir=/etc/cockroach/certs --ca-key=/etc/cockroach/ca-key/ca.key
cockroach cert create-client root --certs-dir=/etc/cockroach/certs --ca-key=/etc/cockroach/ca-key/ca.key
sudo chown -R cockroach:cockroach /etc/cockroach/certs
sudo chmod 0600 /etc/cockroach/certs/*.key
Step 3: Create the Systemd Service and Initialize the Quorum
Deploy the systemd unit on all three nodes (substituting each node’s private IP in --advertise-addr), start all three daemons, and run cockroach init exactly once from Node 1 to bootstrap the Raft quorum.
cat <<'EOF' | sudo tee /etc/systemd/system/cockroachdb.service
[Unit]
Description=CockroachDB Distributed SQL Node
After=network-online.target chrony.service
Wants=network-online.target
[Service]
Type=notify
User=cockroach
Group=cockroach
ExecStart=/usr/local/bin/cockroach start \
--certs-dir=/etc/cockroach/certs \
--store=/var/lib/cockroach \
--listen-addr=10.10.0.11:26257 \
--http-addr=10.10.0.11:8080 \
--advertise-addr=10.10.0.11 \
--join=10.10.0.11,10.10.0.12,10.10.0.13 \
--cache=.25 \
--max-sql-memory=.25
LimitNOFILE=1048576
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now cockroachdb.service
# Run ONCE from Node 1 after all 3 services are up
sudo -u cockroach cockroach init --certs-dir=/etc/cockroach/certs --host=10.10.0.11:26257
4. Verifying Raft Consensus, Clock Offset, and TPC-C Throughput
After initializing the cluster, verify that all three nodes appear live, check that chrony clock offsets remain well under 5 milliseconds, and execute a SQL write/read smoke test plus CockroachDB’s built-in kv or tpcc workload generator.
# 1. Verify all 3 nodes are live and healthy
sudo -u cockroach cockroach node status --certs-dir=/etc/cockroach/certs --host=10.10.0.11:26257
# 2. Confirm NTP clock offset is sub-millisecond
chronyc tracking | grep -E "System time|Last offset|RMS offset"
# 3. Run a quick SQL check, verify zero under-replicated ranges, and benchmark KV latency
sudo -u cockroach cockroach sql --certs-dir=/etc/cockroach/certs --host=10.10.0.11:26257 -e "SHOW DATABASES; SELECT count(*) AS under_replicated FROM crdb_internal.ranges_no_leases WHERE array_length(replicas, 1) < 3;"
curl -sk https://10.10.0.11:8080/_status/vars | grep -E "ranges_underreplicated|clock_offset_meannanos|raft_ticks_behind"
cockroach workload init kv --drop "postgresql://[email protected]:26257?sslmode=verify-full&sslrootcert=/etc/cockroach/certs/ca.crt&sslcert=/etc/cockroach/certs/client.root.crt&sslkey=/etc/cockroach/certs/client.root.key"
cockroach workload run kv --duration=30s --concurrency=16 "postgresql://[email protected]:26257?sslmode=verify-full&sslrootcert=/etc/cockroach/certs/ca.crt&sslcert=/etc/cockroach/certs/client.root.crt&sslkey=/etc/cockroach/certs/client.root.key"
On bare-metal NVMe nodes connected over 10Gbps Ethernet, p99 KV write latency typically lands between 1.5 ms and 4 ms—dramatically tighter than virtualized cloud clusters. Confirm ranges_underreplicated stays at 0 and clock_offset_meannanos remains well below 5000000 (5 ms) across all nodes.
5. mTLS Node Identity, Connection Pooling, and Decommissioning
Distributed SQL clusters require careful firewalling and load-balancing so applications never connect directly to a single node IP that might later undergo maintenance.
Load-Balancing SQL Clients with HAProxy
Deploy HAProxy on your application tier (or as a pair of VIP-backed edge proxies) targeting port 26257 across all three database nodes with HTTP health checks against /health?ready=1 on port 8080. Because every CockroachDB node acts as a symmetric SQL gateway, HAProxy distributes client connections evenly without single-master bottlenecks:
cat <<'EOF' | sudo tee -a /etc/haproxy/haproxy.cfg
listen cockroach_sql
bind 10.10.0.10:26257
mode tcp
balance roundrobin
option httpchk GET /health?ready=1
http-check expect status 200
server crdb1 10.10.0.11:26257 check port 8080 check-ssl verify none inter 2s fall 2 rise 2
server crdb2 10.10.0.12:26257 check port 8080 check-ssl verify none inter 2s fall 2 rise 2
server crdb3 10.10.0.13:26257 check port 8080 check-ssl verify none inter 2s fall 2 rise 2
EOF
sudo systemctl reload haproxy
Strict VLAN Isolation for Raft and Admin Ports
Restrict TCP port 26257 (inter-node Raft + SQL) exclusively to your cluster nodes and application servers via nftables, and bind the Web Admin UI (8080) only to your private management subnet or an SSH/WireGuard bastion. Set OOMScoreAdjust=-900 in your cockroachdb.service (or yb-tserver.service) systemd unit so the Linux kernel protects database processes ahead of auxiliary monitoring agents.
6. Troubleshooting Clock Skew, Raft Stalls, and Version Rollbacks
Most operational incidents in distributed SQL stem from clock jumps, full disks or slow NVMe fsync stalls on a single replica, or premature node restarts during range rebalancing.
Diagnosing Common Distributed SQL Incidents
- Node Exits with “clock offset too large”: Check
chronyc sources -vto verify NTP reachability. Never runntpdateor force-step the clock backward on a live node; letchronyslew smoothly, verify RMS offset is under 5 ms, then restart the node service. - Raft Heartbeat Timeouts from NVMe Compaction Stalls: Verify disk health with
smartctl -a /dev/nvme0n1and run a non-destructivefio --name=fsync-test --rw=write --bs=4k --fsync=1 --size=100mcheck on an unmounted test path. If p99 fsync exceeds 2 ms, check for failing NVMe thermal throttling or accidental write-cache disablement. - Under-Replicated Ranges After Node Reboot: Wait 5 minutes (
server.time_until_store_dead) before assuming a node is permanently lost. If a disk actually failed, runcockroach node decommission <node-id>so Raft cleanly migrates replicas to surviving nodes. - High Transaction Retry Rates (SQLSTATE 40001): Hot rows updated concurrently by dozens of workers trigger MVCC serialization retries. Implement exponential backoff retry loops in your application driver and avoid monotonic integer primary keys in favor of
gen_random_uuid().
Safe Binary Upgrade and Rollback Window
Perform rolling upgrades one node at a time, draining active leases cleanly before stopping each daemon, and hold off on finalizing the cluster version (SET CLUSTER SETTING cluster.preserve_downgrade_option = '24.1') until you have verified application latency for 24–48 hours. While downgrade preservation is active, you can drain a newly upgraded node, swap back the previous binary, and restart it without rebuilding the cluster.
# 1. Preserve downgrade option before rolling binary upgrade
sudo -u cockroach cockroach sql --certs-dir=/etc/cockroach/certs --host=10.10.0.11:26257 \
-e "SET CLUSTER SETTING cluster.preserve_downgrade_option = '24.1';"
# 2. Drain Node 1 leases and roll back binary if an upgrade causes regressions
sudo -u cockroach cockroach node drain 1 --certs-dir=/etc/cockroach/certs --host=10.10.0.11:26257
sudo systemctl stop cockroachdb.service
sudo cp /usr/local/bin/cockroach.v24.1.4.bak /usr/local/bin/cockroach
sudo systemctl start cockroachdb.service
sudo -u cockroach cockroach node status --certs-dir=/etc/cockroach/certs --host=10.10.0.11:26257
Frequently Asked Questions
Why is local NVMe critical for CockroachDB and YugabyteDB?
Every committed write requires a Raft quorum fsync to disk. Local PCIe NVMe drives deliver consistent 50–150 microsecond fsync latency, whereas virtualized cloud block storage often suffers multi-millisecond tail spikes that stall Raft leaders.
Can I run a production distributed SQL cluster on just two servers?
No. Raft consensus requires a strict majority quorum ((N/2) + 1). With two nodes, losing either server leaves only 1 of 2 nodes online, halting all writes. Always deploy at least three independent nodes.
Should I choose CockroachDB or YugabyteDB for migrating a legacy PostgreSQL app?
YugabyteDB is usually easier for legacy PostgreSQL migrations because its YSQL layer reuses PostgreSQL’s actual C query planner and supports PL/pgSQL, triggers, and many native extensions.
How much RAM should I reserve for CockroachDB cache and SQL memory?
Allocate 25% of system RAM to --cache and 25% to --max-sql-memory (50% combined), leaving the remaining 50% for the Linux page cache, Go runtime overhead, and OS stability.
Why must swap be disabled or set to vm.swappiness=1 on distributed SQL nodes?
Swapping database memory pages to disk stalls Raft consensus heartbeats and causes nodes to miss leaseholder renewal deadlines, triggering unnecessary cluster-wide failovers. Setting vm.swappiness=1 or disabling swap entirely keeps latency predictable and prevents the Linux kernel from paging out active RocksDB or Pebble memtables under temporary memory pressure.
Build Fault-Tolerant Distributed SQL on Dedicated NVMe Nodes
Eliminate Raft leader stalls and noisy-neighbor storage jitter. Deploy three-node CockroachDB or YugabyteDB clusters on bare-metal Linux servers with private VLANs and enterprise NVMe.
