Databases

CockroachDB

A distributed SQL database, PostgreSQL wire compatible, with horizontal scaling and fault tolerance.

What it is

CockroachDB combines the relational model and SQL with NoSQL-like scaling. Data is split into ranges that are replicated through Raft consensus, so losing a node or even a region does not stop work.

It offers strong consistency, serializable transactions by default and the PostgreSQL protocol, so familiar drivers and ORMs work. Data can be pinned to regions for locality requirements.

The cost: higher write latency because of consensus between nodes and harder operations than a single PostgreSQL server. The license has changed over time, so check current terms on the site.

Use it when

  • You need a SQL database with automatic failover across zones and regions.
  • Horizontal write scaling without manual sharding.
  • Data placement requirements in specific regions.

Avoid it when

  • A single PostgreSQL server is enough: distribution adds complexity and latency.
  • You need every PostgreSQL extension (PostGIS, pgvector): compatibility is partial.

Key concepts

Ranges
Chunks of data replicated separately.
Raft
Consensus between a range's replicas.
Serializable
The strictest isolation level by default.
Multi-region
Settings for placing data across regions.

Example

A money transfer in a distributed transaction.

Interview questions

  1. 1. What is NewSQL?

    Databases that combine the relational model with SQL and transactions and the horizontal scaling typical of NoSQL. CockroachDB, Spanner and TiDB belong to this class.

  2. 2. How does CockroachDB survive a node failure?

    Each range is replicated to several nodes and has a Raft leader. If a node disappears, the majority of replicas elect a new leader and data stays available.

  3. 3. Why avoid monotonic keys?

    Sequential keys send all writes to one range and create a hotspot. Prefer UUIDs or a hashed prefix.

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).