Open Source Spanner
Google Spanner is proprietary technology available only through Google Cloud. CockroachDB was created by ex-Google engineers to bring a Spanner-like architecture to the open-source world, runnable on commodity hardware without atomic clocks.
Like Spanner, CockroachDB is a distributed SQL database built on top of a transactional, strongly-consistent key-value store. It shards data into "ranges" (analogous to Spanner's tablets) and replicates them using a consensus algorithm. It speaks the PostgreSQL wire protocol, meaning most PostgreSQL-compatible clients and tools work with it out of the box.
Key Differences from Spanner
1. Consensus: Raft instead of Paxos
CockroachDB uses the Raft consensus algorithm instead of Paxos. Both algorithms solve the same problem — getting a distributed group of nodes to agree on a sequence of values — but Raft was designed to be easier to understand and implement correctly. In Raft, one node is elected leader per range and coordinates all writes. Followers replicate the leader's log. If the leader fails, a new election occurs automatically.
2. Hybrid Logical Clocks instead of TrueTime
Most companies don't have atomic clocks. CockroachDB uses Hybrid Logical Clocks (HLC), a software algorithm that combines physical wall-clock time with a logical counter:
HLC timestamp = (physical_time, logical_counter)
When a node receives a message with a higher timestamp than its own clock, it advances its clock to match. This provides causal ordering — if event A caused event B, A's timestamp will always be less than B's — without requiring synchronized hardware. The trade-off is that HLC cannot provide the same strict global real-time ordering guarantees as TrueTime. In rare cases, CockroachDB may need to restart a transaction due to clock uncertainty, adding latency.
3. Survivability Model
CockroachDB is designed around a clear survivability model. With a replication factor of 3, it can survive the loss of 1 node. With a replication factor of 5, it can survive the loss of 2 nodes. You can configure locality labels on nodes so that CockroachDB places replicas across different availability zones or regions automatically — for example, pinning a table's data to US nodes for compliance is a single ALTER TABLE ... CONFIGURE ZONE USING statement naming the region constraint and replica count.
A common interview question is: "What consistency level does CockroachDB provide, and how does it compare to Spanner?" CockroachDB provides serializable isolation by default — the strongest standard SQL isolation level. Spanner provides external consistency (linearizability), which is strictly stronger than serializability. The difference: serializability guarantees transactions appear to execute in some serial order, but external consistency additionally guarantees that order matches real-world time. For most applications, serializability is sufficient.
Key Point: CockroachDB achieves Spanner-like guarantees using Raft consensus and Hybrid Logical Clocks instead of atomic hardware. This makes it deployable on commodity infrastructure at the cost of slightly weaker (but still very strong) consistency guarantees.