NotesSystem

CockroachDB

Also: Cockroach · CRDB

A distributed SQL database (Postgres wire protocol) that defaults to serializable transactions across ranges replicated with Raft, using MVCC with hybrid-logical-clock timestamps.

Also in: Consistency Models › In the real world · Consensus › In the real world

Design choices

  • Serializability by default: every transaction runs SERIALIZABLE unless it opts into READ COMMITTED (available since 2024), which accepts some anomalies for fewer aborts.
  • MVCC with HLC timestamps: each version is stamped with a hybrid logical clock time; reads near the clock-skew bound trigger uncertainty restarts.
  • Write intents: provisional values that double as exclusive locks and point to a transaction record.
  • Timestamp cache + read refresh (Optimistic Concurrency Control): a write under a later read is pushed to a later timestamp; the transaction then re-validates its reads and returns a retryable 40001 error if they changed.
  • Parallel commits: halve commit latency to one round of consensus.

Notable trade-offs

  • Clients need a retry loop for 40001 errors; single statements and single-batch transactions are retried automatically while their results still fit in the results buffer (512 KiB by default since v24.1, 16 KiB before).
  • Contended keys cause pushes, refresh failures and retries rather than simple lock waits.
  • Correctness depends on bounded clock skew; a node whose clock drifts too far shuts itself down.