Data Systems
Spanner and CockroachDB
Google Spanner pioneered globally distributed SQL. CockroachDB brought those ideas to the open-source world. Master their architectures, consensus protocols, and the trade-offs between them.
- Global Transactions â Serializable ACID across regions
- Consensus â Paxos (Spanner) vs Raft (CockroachDB)
- Consistency â External consistency vs serializable
These databases prove you don't have to sacrifice consistency for global scale.
Google Spanner
TrueTime
Spanner Architecture
CockroachDB
Raft Consensus
| Feature | Paxos (Spanner) | Raft (CockroachDB) |
|---|---|---|
| Understood | Complex | Easier to reason about |
| Membership | Manual | Dynamic |
| Leader election | Implicit | Explicit |
| Implementation | Custom | Reference implementation |
Spanner vs CockroachDB
| Feature | Spanner | CockroachDB |
|---|---|---|
| Consensus | Paxos | Raft |
| Time Source | TrueTime (atomic clocks) | Hybrid Logical Clocks (HLC) |
| Deployment | GCP only | Any cloud, self-hosted |
| Consistency | External consistency | Serializable |
| Latency | Lower (TrueTime) | Higher (HLC uncertainty) |
| Cost | Higher (GCP pricing) | Lower (open source) |
| Maturity | 10+ years | 7+ years |
When to Use Each
| Use Case | Recommended |
|---|---|
| Global financial system on GCP | Spanner |
| Multi-cloud distributed SQL | CockroachDB |
| Need lowest latency globally | Spanner |
| Need to avoid vendor lock-in | CockroachDB |
| Already invested in GCP | Spanner |
| Need self-hosted option | CockroachDB |
Practice Exercises
-
Architecture Comparison: Compare the write paths of Spanner and CockroachDB. What happens when a write arrives in the US but the leader for that data is in Europe?
-
Transaction Design: Design a distributed transaction for transferring money between accounts in different regions. How do you handle the coordination and ensure consistency?
-
Consistency Analysis: Explain the difference between external consistency and serializability. Why does Spanner's TrueTime matter?
-
Migration Planning: Your team is migrating from PostgreSQL to CockroachDB. What are the main challenges, and how would you approach the migration?
What to Learn Next
-> NewSQL and Distributed SQL Overview of NewSQL databases and distributed SQL concepts.
-> PostgreSQL Deep Dive Advanced PostgreSQL features, extensions, and optimization.
-> SQL Deep Dive PostgreSQL, MySQL, indexing strategies, and query optimization.
-> Distributed Consensus Paxos, Raft, and consensus protocols.
-> Data Replication Sync vs async replication, leader election, and consistency.
-> CAP Theorem Consistency models, availability, and partition tolerance.