[CockroachDB Internals #5] CockroachDB is distributed SQL database that replicates the data to offer fault tolerance. Here are key details about it ⚡
Although CockroachDB is a SQL-like database, it actually stores data as a gigantic distributed KV store. Keys are ordered lexicographically and are divided into 64MB ranges.
Most traditional databases would replicate the data node-wise i.e. all the data of a node is clubbed and replicated to a subset of nodes.
i.e. data of node 1 is replicated to node 5 and node 13.
Instead of doing it at the node level, CockroachDB does it at the range level. Each range is replicated across the cluster by some replication factor (defaults to 3).
i.e. say range A, range B and range C are present on node 1, but
range A might be replicated on node 3, node 7, and node 13, while range B might be replicated on node 4, node 6, and node 7, while range C might be replicated on node 3, node 5, and node 12.
Keeping the replicated ranges in sync is the biggest challenge of any database and CockroachDB does it beautifully using Raft - a distributed consensus protocol. Each range is its own Raft group and runs its own consensus.
Discussing how Raft Consensus work is a 30 min lecture in itself, planning to cover it in gist sometime in the future.
In the next post, we will talk about how reads and writes happen in the distributed and replicated CockroachDB cluster. The approach is quite interesting and fascinating.
⚡ Recently, I started diving deep into CockroachDB internals and will share my learning in public. So, if you find it amusing, follow along.
System Design for Beginners: arpitbhayani.me/sys-design System Design for Experienced: arpitbhayani.me/course