Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is a distributed SQL database (for example CockroachDB, YugabyteDB, Google Spanner or TiDB), and how does it differ from sharding PostgreSQL yourself?

A distributed SQL database such as CockroachDB, YugabyteDB, Spanner or TiDB presents one SQL database while automatically splitting data into ranges or tablets, replicating each with a consensus protocol (Raft or Paxos) and rebalancing them across nodes, and it supports transactions across ranges. Compared with sharding PostgreSQL ourselves, the application no longer handles routing, resharding or cross-shard commits, but each write pays consensus round trips, especially across regions, and compatibility with PostgreSQL or MySQL is partial. Self-managed sharding keeps single-node performance, but we own all the distribution logic.

Why this question is asked

For years, scaling a relational database beyond one primary meant sharding it by hand, with all the routing and cross-shard problems that brings. Distributed SQL databases promise to make that the database's job instead. Interviewers ask this question to see whether we understand what these systems actually do internally and what they cost, rather than treating them as a magic bigger PostgreSQL. In this answer, we will recap what we must build when we shard ourselves, see how a distributed SQL database splits, replicates and rebalances data, look at distributed transactions, and then compare the costs of both approaches.

1A Quick Recap: What Sharding Ourselves Means

When we shard PostgreSQL ourselves, we run several independent PostgreSQL servers, each holding part of the data, and our application or a proxy decides which server each query goes to. Each shard is an ordinary database, so queries that stay on one shard run at full single-node speed with all PostgreSQL features. But everything between shards is our problem: choosing and routing by a shard key, moving data when shards fill up, queries that span shards, transactions that must change rows on two shards, and keeping schemas in sync.

2How Distributed SQL Splits, Replicates and Rebalances

A distributed SQL database takes that work into the database. Each table, and each index, is stored as a sorted key-value space, split into contiguous chunks of keys. CockroachDB calls them ranges, YugabyteDB calls them tablets, TiDB's storage layer (TiKV) calls them regions, and Spanner calls them splits. When a chunk grows past a size limit, or gets too much load, the database splits it automatically.

Each chunk is replicated, typically three times, on different nodes, and the replicas form a consensus group using Raft (CockroachDB, YugabyteDB, TiDB) or Paxos (Spanner). A write to a chunk is committed when a majority of its replicas have it, so losing one node of three loses no committed data, and a new leader is elected automatically. A placement component moves replicas between nodes to balance data and load, and when we add a node, chunks migrate to it in the background. The SQL layer on top routes each read and write to the right chunk, so the application sees one database and connects to any node.

Step throughInside a distributed SQL database
  1. SQL layer (any node)parses SQL, plans, coordinates transactions
  2. Transaction layerdistributed commits across ranges, timestamps, isolation
  3. Ranges / tabletstable and index data split by key, split again as they grow
  4. Consensus replicationeach range has 3 replicas in a Raft or Paxos group
  5. Nodes and placementreplicas spread across nodes, zones, regions; rebalanced

5 layers, from the top down. Step through them one at a time.

The application talks only to the top layer; the rest is the database's job.

3Transactions Across Ranges

Because rows of one transaction may live in different ranges on different nodes, the database runs a distributed commit protocol internally. Simplified, the transaction writes provisional values, called intents in CockroachDB, in each range it touches, and then commits by atomically updating a single transaction record, after which the provisional values become visible. Timestamps decide which version each reader sees, using hybrid logical clocks or, in Spanner, tightly synchronised clocks. The application just writes BEGIN, its statements and COMMIT.

Notice what this removes from our code. A transfer between two accounts that happen to live in different ranges is just a normal transaction, with no saga, no two-phase commit managed by us, and no idea where the rows live. That is the main thing we are buying.

SQL
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1; -- range on node 2
UPDATE accounts SET balance = balance + 100 WHERE id = 9000; -- range on node 5
COMMIT; -- atomic across both ranges
-- with self-managed shards this would need 2PC or a saga in application code

4What It Costs

Every write pays for consensus: the leader of each range must hear from a majority of replicas before the write commits. Within one region this adds a few milliseconds, and a transaction touching several ranges adds coordination on top. Across regions, a write whose replicas span continents waits for at least one cross-region round trip, often 70 to 200 milliseconds. Single-node PostgreSQL, committing to a local disk, is often well under a millisecond for a small write. So a workload of many tiny, sequential transactions can be noticeably slower.

Performance characteristics also differ. Sequential keys concentrate inserts on one range, so these databases recommend UUIDs or hash-sharded indexes. Many default to SERIALIZABLE isolation (CockroachDB does; YugabyteDB and TiDB have different defaults, so check the product), which means contended transactions fail with retryable errors and the application must retry. Query plans for joins across nodes behave differently from a single machine.

Compatibility is partial. CockroachDB and YugabyteDB speak the PostgreSQL wire protocol and TiDB speaks MySQL's, so JDBC drivers, JPA and Spring work. But PostgreSQL extensions, some functions, some DDL, triggers or stored procedure details, and isolation behaviour can differ. YugabyteDB reuses PostgreSQL's query layer and is closer in features; CockroachDB is its own implementation. We should check the specific product's compatibility notes against what our application uses.

5Choosing Between Them

Both approaches can be right. Self-managed sharding fits when most work stays on one shard, single-node latency matters, we need full PostgreSQL features, and we have the team to own routing and resharding. Distributed SQL fits when we want horizontal scale and automatic failover without building a distribution layer, need transactions across any rows, or need multi-region placement, and can accept higher write latency and some compatibility work.

Distributed SQL versus sharding PostgreSQL ourselves
ConcernDistributed SQLSelf-managed sharding
RoutingDatabase routes to rangesApplication or proxy routes by shard key
ReshardingAutomatic splits and rebalancingOur tooling and projects
Cross-shard transactionsBuilt in2PC, sagas or redesign
Write latencyConsensus round trips; more across regionsSingle node, local commit
Feature compatibilityPartial; check the productFull PostgreSQL
FailoverAutomatic per rangePer shard, with our HA tooling

6Follow-up questions

Is Citus a distributed SQL database?

Citus is an extension that turns PostgreSQL into a sharded cluster: a coordinator routes queries to worker PostgreSQL nodes holding shards, and it supports distributed transactions and a shard rebalancer. It sits between the two worlds: it keeps real PostgreSQL on each node, but data placement is based on a distribution column we choose, and replication and failover per node rely on ordinary PostgreSQL HA rather than per-range consensus.

Why are three replicas the default?

Because consensus needs a majority. With three replicas, two must agree, so one can fail without losing availability or data. Five replicas survive two failures at the cost of more storage and slightly slower writes.

Does a distributed SQL database remove the need for a good key design?

No. Keys still decide how data is split into ranges, so sequential keys create hot ranges, and keeping related rows close together, for example with a shared key prefix, still matters for performance.

7Conclusion

In this answer, we saw that a distributed SQL database presents one SQL database while splitting tables into ranges or tablets, replicating each range with Raft or Paxos, rebalancing automatically and running transactions across ranges. That removes routing, resharding and cross-shard commits from our application. The price is consensus latency on every write, especially across regions, different performance behaviour such as hot ranges and retryable serialization errors, and partial compatibility with PostgreSQL or MySQL. Self-managed sharding keeps single-node speed and full features, but we own all the distribution logic.

What to say in the interviewAbout 90 seconds aloud

Open with

A distributed SQL database, like CockroachDB, YugabyteDB, Spanner or TiDB, looks like one SQL database to the application, but underneath it splits every table and index into ranges or tablets by key, replicates each one, usually three times, with a consensus protocol, Raft or Paxos, and rebalances them across nodes automatically as data grows or nodes are added.

Then, if they want more

It also runs distributed transactions across ranges, so an update touching rows on different nodes is still just BEGIN and COMMIT. Compared with sharding PostgreSQL ourselves, the big difference is who owns the distribution.

With our own shards, each shard is a normal PostgreSQL with full features and single-node performance, but the application or a proxy has to route by shard key, and we own resharding, cross-shard queries and cross-shard transactions, which means sagas or two-phase commit. With distributed SQL, the database does all of that.

The cost is latency, because each write waits for a majority of replicas, a few milliseconds in one region and often 70 to 200 milliseconds when replicas span regions. Performance also behaves differently: sequential keys create hot ranges, and many of these databases default to serializable isolation, so the app must retry on 40001 errors.

And compatibility is partial: they speak the PostgreSQL or MySQL protocol, but extensions, some SQL features and isolation details differ, so I would check the product against what we use.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a distributed SQL database (for example CockroachDB, YugabyteDB, Google Spanner...": https://scrivio-interview-library.netlify.app/databases/partitioning-and-sharding/bfc3252b08/