Distributed SQL has moved from a niche research topic to the backbone of today’s global, data‑driven applications. Whether you’re powering a real‑time trading platform, an online multiplayer game, or a worldwide network of sensors tracking bee colonies, the need for a database that can scale horizontally, remain highly available, and preserve strong consistency is no longer optional—it’s mandatory.
In this pillar article we dive deep into three of the most influential distributed SQL engines that claim to deliver global consistency: CockroachDB, YugabyteDB, and TiDB. We’ll unpack their architectural choices, examine how they solve the classic CAP trade‑offs, and surface concrete performance numbers that matter when you’re deciding which engine to trust with mission‑critical data. Along the way we’ll sprinkle in real‑world analogies—from the hive mind of a bee colony to the cooperative behavior of self‑governing AI agents—so the technical details feel grounded and relatable.
1. What is Distributed SQL? distributed-sql-basics
Distributed SQL is the marriage of SQL’s expressive, declarative query model with a distributed, fault‑tolerant storage layer that can span data centers, clouds, and even edge locations. At its core, a distributed SQL system must answer three questions:
| Question | Traditional RDBMS | Distributed SQL |
|---|---|---|
| Scalability | Scale up (bigger machines). | Scale out (more machines). |
| Availability | Single point of failure unless replicated. | Automatic failover, multi‑region replication. |
| Consistency | Strong ACID guarantees on a single node. | Strong (or tunable) ACID across nodes. |
The holy grail is to achieve Consistency, Availability, and Partition tolerance simultaneously—a feat that the classic CAP theorem says is impossible in the presence of network partitions. Distributed SQL engines sidestep the impossibility by leveraging consensus protocols (Raft, Paxos, or variants) and carefully designing data placement so that the system can appear strongly consistent most of the time while still providing high availability.
2. The Challenge of Global Consistency
When you replicate data across continents, latency isn’t a footnote—it’s a defining constraint. A single write must travel to a quorum of replicas, survive network jitter, and still return to the client within an acceptable SLA. The latency budget for many modern apps is under 50 ms for a write and under 10 ms for a read, even when the data lives in three separate regions.
2.1 Consensus Protocols in a Nutshell
Raft has become the de‑facto consensus algorithm for many distributed SQL engines because of its simplicity and strong leader‑centric model. In a typical Raft group:
- Leader election – one node becomes the leader; the rest are followers.
- Log replication – the leader appends client commands to its log, then replicates them to followers.
- Commit – once a majority (⌈N/2⌉ + 1) acknowledge, the entry is committed and applied.
Because a majority is required, the system tolerates up to ⌊(N‑1)/2⌋ simultaneous node failures without losing consistency. The trade‑off is write latency: a client must wait for the round‑trip to the leader and the acknowledgment from a majority of replicas.
2.2 Multi‑Region Placement
Global consistency also hinges on where data lives. Most engines let you define replication zones or placement policies:
- CockroachDB uses ranges (≈ 64 MiB) that are automatically split and re‑balanced across zones.
- YugabyteDB stores tablet shards in placement groups.
- TiDB separates TiKV stores into regions managed by a Placement Driver.
By co‑locating a quorum of replicas in the same region, you can keep latency low for local traffic while still maintaining a remote replica for disaster recovery. The engineering challenge is to keep those replicas in sync without sacrificing the global ACID guarantees.
3. CockroachDB Architecture cockroachdb-architecture
CockroachDB brands itself as “the database that scales like a NoSQL store, but gives you the relational semantics of PostgreSQL.” Its architecture is a textbook implementation of Raft‑based replication, but with several clever twists to make it production‑ready at global scale.
3.1 Ranges, Replicas, and Zones
- Ranges: The smallest unit of data distribution, defaulting to 64 MiB. A range is analogous to a shard in other systems.
- Replicas: Each range has three replicas (configurable) stored on distinct nodes. The default replication factor of 3 yields a theoretical 99.999% availability (five‑nines) when nodes are independently failing.
- Zones: You can assign zone configurations that dictate where replicas live. For example, a table storing user profiles might have two replicas in the US‑East zone and one in US‑West, guaranteeing low‑latency reads for American users while still surviving a whole‑region outage.
3.2 Raft Leaders per Range
Every range elects its own Raft leader. This leader per range design avoids a single bottleneck: writes to different ranges can proceed in parallel, each coordinated by its own leader. In practice, a busy OLTP workload that touches many tables will distribute its write load across thousands of leaders.
3.3 Transaction Layer: Optimistic Concurrency + Timestamp Ordering
CockroachDB implements serializable isolation using Hybrid Logical Clocks (HLC). Each transaction receives a read timestamp and a write timestamp:
- Read phase – The transaction reads at its read timestamp, using MVCC (multi‑version concurrency control) to see a consistent snapshot.
- Write phase – Writes are staged with a provisional timestamp.
- Commit – The coordinator performs a write intent resolution and atomically bumps the commit timestamp to the maximum of all participants’ timestamps plus one.
If a conflict is detected (e.g., two concurrent transactions try to write the same key), one transaction is restarted automatically. This approach eliminates deadlocks and guarantees strict serializability—the strongest consistency model available.
3.4 Real‑World Numbers
| Metric | Value (as of v23.2) |
|---|---|
| Max nodes per cluster | 1,000+ (tested in production) |
| Typical write latency (single‑region) | 5–10 ms |
| Global write latency (3‑region) | 30–45 ms (majority quorum) |
| Throughput on 30‑node cluster (YCSB A) | ~150 k ops/s |
| Storage overhead (replication factor = 3) | ~2× raw data (due to MVCC) |
3.5 Bee Analogy
Think of a CockroachDB range as a cell in a beehive. Each cell (range) has three worker bees (replicas) that tend to the honey (data). The queen of the cell (Raft leader) decides when new honey is added. If one worker bee disappears, the other two still keep the cell alive, and the hive continues producing honey without missing a beat.
4. YugabyteDB Architecture yugabytedb-architecture
YugabyteDB positions itself as a PostgreSQL‑compatible (YSQL) and Cassandra‑compatible (YCQL) distributed database, built on a shared‑nothing architecture that blends Raft with a log‑structured merge‑tree (LSM) storage engine called DocDB.
4.1 Tablet Servers and Shards
- Tablets: The primary data unit, each covering a contiguous key range (default 256 MiB). A tablet is analogous to a range in CockroachDB.
- Tablet Replicas: By default three replicas per tablet, each hosted on a different tserver (tablet server). Replication factor is configurable up to 5.
- Placement Policies: YugabyteDB uses a placement cloud abstraction (e.g.,
cloud=aws, region=us-east-1, zone=us-east-1a). You can assign placement blocks that guarantee at least one replica in each desired zone.
4.2 Raft per Tablet + DocDB
Each tablet runs its own Raft group, mirroring CockroachDB’s per‑range leader model. However, YugabyteDB couples Raft with DocDB, an LSM‑based engine optimized for write‑heavy workloads:
- Write Path: Incoming writes are appended to an in‑memory memtable, then flushed to immutable SSTables on disk.
- Read Path: Reads consult a Bloom filter and a key‑value index to locate the correct SSTable, achieving sub‑millisecond latency for point reads.
The combination yields high write throughput while retaining strong consistency because Raft ensures the log is replicated before a write becomes visible.
4.3 Transaction Layer: Two‑Phase Commit (2PC) + MVCC
YugabyteDB’s YSQL API implements serializable isolation using a two‑phase commit (2PC) protocol across tablets:
- Prepare Phase – The coordinator sends a prepare request to each tablet’s Raft leader, which writes a prepare record to its log.
- Commit Phase – Once a majority of tablets acknowledge, a commit message is sent, and each tablet finalizes the transaction.
Because each tablet’s Raft log is already ordered, the 2PC can be optimistically fast: the prepare and commit steps often complete within a single network round‑trip when all tablets are co‑located.
4.4 Performance Benchmarks
| Benchmark | Configuration | Latency (p99) | Throughput |
|---|---|---|---|
| YCSB A (write‑heavy) | 9‑node cluster, RF = 3 | 6 ms (single‑region) | 210 k ops/s |
| TPCC (OLTP) | 12‑node cluster, RF = 3 | 15 ms (new‑order) | 12 k tpmC |
| Global write (US‑East, EU‑West, AP‑South) | 3‑region, RF = 3 | 38 ms | 70 k ops/s |
YugabyteDB’s YCQL API (Cassandra‑compatible) can achieve even lower latencies for eventual‑consistent workloads, but the YSQL path remains fully ACID.
4.5 Bee Analogy
Imagine each tablet as a flower patch in a meadow. Three bees (replicas) pollinate each patch, and one bee is the leader that decides when a new pollen grain (write) is added. Even if one bee disappears, the patch still produces honey, and the meadow continues thriving.
5. TiDB Architecture tidb-architecture
TiDB is a MySQL‑compatible distributed SQL engine that separates compute and storage into three layers: TiDB servers, TiKV stores, and the Placement Driver (PD). Its design draws heavily from Google’s Spanner and F1, but with an open‑source implementation.
5.1 The Three‑Tier Stack
| Layer | Role |
|---|---|
| TiDB Server | Stateless SQL layer; parses queries, generates execution plans, and coordinates distributed transactions. |
| TiKV Store | Distributed key‑value storage engine based on RocksDB (LSM). Stores data as regions (≈ 96 MiB). |
| Placement Driver (PD) | Central metadata service; manages region placement, leader election, and cluster topology. |
5.2 Regions, Raft Groups, and the Percolator Model
- Regions: Each region is a contiguous key range stored on multiple TiKV nodes. By default, a region has three Raft replicas.
- Raft Leaders: Like CockroachDB and YugabyteDB, each region elects a leader to serialize writes.
- Percolator Transaction Model: TiDB adopts a two‑phase commit similar to Google’s Percolator. The transaction coordinator (a TiDB server) writes prewrite records to all involved regions, then commits them atomically.
The key difference is TiDB’s timestamp oracle: PD generates monotonically increasing timestamps using a Hybrid Logical Clock that is globally unique across the entire cluster. This eliminates the need for each region to negotiate timestamps locally, simplifying global ordering.
5.3 Strong Consistency Guarantees
TiDB offers snapshot isolation by default, but you can enable strict serializable mode with a simple session variable (SET @@transaction_isolation='SERIALIZABLE'). In this mode, TiDB enforces write‑conflict detection during the prewrite phase and aborts conflicting transactions.
5.4 Real‑World Performance
| Metric | Value |
|---|---|
| Max nodes in a production TiDB cluster | 1,200 (TiKV stores) |
| Write latency (single‑region, RF = 3) | 4–9 ms |
| Global write latency (3‑region) | 28–40 ms |
| YCSB A throughput (96‑node cluster) | 180 k ops/s |
| OLTP (Sysbench) – transactions per second | 8 k tps (4‑node TiDB + 12 TiKV) |
TiDB’s separation of compute and storage enables elastic scaling: you can add more TiDB servers for query parallelism without touching the underlying TiKV stores.
5.5 Bee Analogy
Picture the Placement Driver as the queen bee that decides where each cell (region) lives within the hive. The TiKV stores are the worker bees that tend the honey (data) in each cell. The TiDB servers are the foragers that bring in nectar (queries) and distribute it across the hive, always ensuring that the honey remains fresh and consistent.
6. Comparative Analysis: Consistency, Performance, and Ops
Below we line up the three engines across the dimensions that matter most for global, ACID‑critical workloads.
| Dimension | CockroachDB | YugabyteDB | TiDB |
|---|---|---|---|
| SQL Compatibility | PostgreSQL 13‑level (full feature set) | PostgreSQL 12 (YSQL) + Cassandra (YCQL) | MySQL 5.7 / 8.0 compatible |
| Consensus Protocol | Raft per range | Raft per tablet | Raft per region (via TiKV) |
| Timestamp Generation | Hybrid Logical Clock per node | Hybrid Logical Clock per tablet | Global HLC from PD (centralized) |
| Default Replication Factor | 3 (configurable) | 3 (up to 5) | 3 (configurable) |
| Strong Consistency Guarantees | Serializable (default) | Serializable (YSQL) | Snapshot Isolation (default) / Serializable optional |
| Write Latency (single‑region) | 5–10 ms | 6–9 ms | 4–9 ms |
| Global Write Latency (3‑region) | 30–45 ms | 35–48 ms | 28–40 ms |
| Throughput (YCSB A, 30‑node) | ~150 k ops/s | ~210 k ops/s | ~180 k ops/s |
| Storage Engine | MVCC on RocksDB (embedded) | LSM DocDB (RocksDB‑like) | RocksDB (LSM) |
| Compute‑Storage Separation | No (TiDB‑like) – nodes run both | No (integrated) | Yes (TiDB stateless) |
| Operational Complexity | Moderate (single binary) | Moderate (tserver + yb‑master) | Higher (three services) |
| Multi‑Region Placement | Zone configs, survivable across regions | Placement blocks, automatic rebalancing | PD‑controlled region placement |
| Open‑Source License | Apache 2.0 (Enterprise tier optional) | Apache 2.0 (Enterprise tier optional) | Apache 2.0 (Enterprise tier optional) |
| Ecosystem | Built‑in UI, Prometheus, Grafana dashboards | Yugabyte Platform, YSQL tools, YCQL CLI | TiDB Dashboard, TiDB Cloud, TiDB Operator |
6.1 Consistency vs. Latency Trade‑offs
- CockroachDB emphasizes global serializability with a per‑range Raft leader, which yields predictable latency but can suffer when a leader sits far from a client.
- YugabyteDB gains a slight edge in raw throughput thanks to its LSM‑optimized DocDB, yet its 2PC across tablets adds a small overhead for cross‑shard transactions.
- TiDB benefits from a central timestamp oracle that removes per‑region timestamp negotiation, giving it the lowest global write latency among the three, but it introduces a single point of failure (mitigated by PD replication).
6.2 Operational Considerations
- Node Count & Resource Footprint – CockroachDB and YugabyteDB run a single binary that handles both compute and storage, simplifying deployment on Kubernetes. TiDB’s three‑tier design requires careful sizing of PD, TiDB, and TiKV pods, but it allows you to scale query processing independently of storage.
- Backup & Restore – All three support point‑in‑time recovery (PITR) via incremental snapshots. CockroachDB’s incremental backup can achieve < 5 GB/h on a 10 TB dataset. YugabyteDB’s Yugabyte Platform automates multi‑region backups to S3. TiDB’s BR (Backup & Restore) tool can stream backups at 1 TB/h with parallelism.
- Observability – Each engine ships with Prometheus metrics and Grafana dashboards. CockroachDB’s UI shows range health and replica distribution; YugabyteDB’s console visualizes tablet hotspots; TiDB’s Dashboard displays region split/merge activity.
7. Real‑World Use Cases
7.1 Financial Services: Global Trade Matching
A multinational exchange platform processes 10 M trades per day across New York, London, and Singapore. The system requires strict serializability to avoid double‑spending and must survive a whole‑region outage.
- CockroachDB: Deployed with three‑zone replication (US‑East, EU‑West, AP‑South). The per‑range Raft leaders are co‑located with the majority of traders, delivering < 30 ms write latency even under peak load.
- YugabyteDB: Chosen for its high write throughput; the exchange runs a YSQL schema with stored procedures for order matching. 2PC across tablets guarantees atomicity of multi‑account transfers.
7.2 Online Gaming: Real‑Time Leaderboards
A global multiplayer game tracks player scores in real time, updating leaderboards for millions of concurrent users. Consistency is essential—no player should see a stale rank after a win.
- TiDB: Its compute‑storage separation lets the game spin up additional TiDB servers during events, while TiKV stores keep the leaderboard data strongly consistent across regions.
- CockroachDB: Also viable; its range splits automatically balance hot keys (top‑10 players) across nodes, preventing hotspot throttling.
7.3 E‑Commerce: Multi‑Region Inventory
A retailer with warehouses in the US, EU, and Asia needs to keep stock levels consistent to avoid overselling.
- YugabyteDB: The YCQL API powers a low‑latency cache layer for inventory reads, while YSQL handles transactional order placement. The 2PC ensures that a purchase atomically decrements inventory across all warehouses.
7.4 Bee‑Colony Monitoring Platform (Apiary)
Apiary’s own platform collects sensor data from thousands of beehives worldwide—temperature, humidity, hive weight, and acoustic signatures. The data is stored for real‑time analytics and long‑term research.
- Why a distributed SQL engine? Sensors send a few kilobytes every minute; the platform must aggregate this data globally while preserving exact timestamps for scientific reproducibility.
- Implementation: We run CockroachDB in a three‑region cluster (AWS us‑east‑1, eu‑central‑1, ap‑south‑1). The zone config places a replica in each region, guaranteeing that a researcher in any continent can query the latest hive status with < 20 ms latency.
- AI agents: Self‑governing analysis agents read from the same database, compute anomaly scores, and write back alerts—all within a single transaction to avoid race conditions.
8. Operational Best Practices
8.1 Capacity Planning
| Metric | Recommended Baseline |
|---|---|
| **CPU per |