CockroachDB: How SQL Works Across Multiple Servers
Outline
Transcript
0:00 CockroachDB spreads a SQL database across multiple servers. Debugging a write gets confusing when the server taking your connection holds data but sends the update elsewhere. We'll trace an update from its SQL connection to the server coordinating its storage, then change the row to see why the destination changes. We're using CockroachDB's architecture guides, its versioned source and a small local experiment. Links to the primary sources are in the description. For example, our accounts table has two rows: IDs 1 and 100, with balances 10 and 20.
0:32 The client connects to node one. Yes, node one is our SQL gateway: the request's entry point. It accepts SQL, plans the work and returns the answer to the client. And the diagram's three nodes are all inside one process for this demo, using memory. The routing is real; separate machines and disks aren't being tested. A common mistake is to treat the SQL endpoint as the one server responsible for every write. Our first update will go from node one to node two. Node one takes the connection, executes SQL, and holds a copy.
1:05 What's still missing before it can handle this write? It needs the coordinator role for that key. Holding those bytes alone doesn't grant it. To identify the coordinator, first locate the row among the stored keys. First, locate that row below the table layer. CockroachDB encodes data as ordered keys; the highlighted key includes table, index and row identifiers. Read the final row identifier against our table. It selects ID one. The storage layer can locate that key without treating the SQL table as one indivisible object.
1:38 A range is a contiguous interval in that ordered key space. For this example, we explicitly split the table's keys at ID 50. That puts our selected rows in different ranges. The run calls them 82 and 83. Those are observed IDs, not fixed CockroachDB partition numbers. A range can include other keys too, so the diagram only places the selected rows. Now the matrix shows a replica, a copy, on each node. Each row of the matrix is one range with three copies. The leaseholder is its current coordinator for the ordinary operations we're following. So the copy answers where data is stored, while the marker answers who coordinates.
2:17 For the first range, the marker is on node two. Consider an engineer debugging this: 'The connection and the copy are both on node one. Why am I investigating node two?' Because node two holds the lease for this range. The connection address can't tell us that. Let's follow the storage request and check the destination. Now take the first SQL update, adding one to the balance and returning it. Below that statement, the trace includes Scan and Put: read and store operations. Follow the Put for this row.
2:48 That's a storage operation within the SQL work, rather than a claim that the whole statement consists of one message. The distribution sender finds the key's range and routes the operation to a replica. It can use cached information about ranges and their leaseholders. My first thought was each update asking a central directory for directions. If the gateway already knows the range and coordinator, does it avoid that lookup? Yes, cached routing can supply that information. A missing or stale entry can require lookup or retry, but a fresh central consultation isn't part of every request.
3:23 Now look at the selected source branch. When the sender prioritizes a known leaseholder, the highlighted call moves that replica to the front of its choices. And our trace names node two as the Put destination. The arrow leaves gateway one, reaches coordinator two, and the statement returns balance 11. The trace's node and storage labels identify that destination. The trailing replica number identifies its copy; it isn't another server along the route. That gives us an observed destination matching the source decision.
3:55 Next we can change the target key while keeping the SQL connection in place. Next, consider the second key: it tests whether node 2 coordinates the entire database. That row belongs to the range whose leaseholder we moved onto node three. The client still uses node one. This separate update increments the balance for ID 100, and the captured Put goes to node three. The balance comes back as 21, and the client hasn't reconnected. Even its connection settings have nothing new to tell us. I think the arrows could mislead me into a transfer between accounts.
4:29 These separate updates don't show both balances changing as one transaction. Yeah, the arrows are two routing decisions. A fresh query through node two reads both balances, 11 and 21. Suppose the application instead opened its connection on node three. Would that make node three the coordinator for both ranges? No. The chosen gateway receives the SQL; leaseholding is assigned per range. The range containing the requested key still matters. That explains why the readback can enter through another node.
5:01 We're addressing one logical database, even though its storage work has different destinations. And we've tested that distinction without removing any local copies. But having three copies leaves a safety question: why can't each accept writes independently? A copy needs a coordinated order of changes. Otherwise gateways could treat different histories as the answer for one key. Which means routing alone isn't enough. Coordinating that order means the copies need agreement. Does every copy need to agree before the next command can proceed?
5:32 No. A quorum is the required majority. In this group, every copy has a vote, so two of three suffice. That's the source-backed model. Before proposing an ordinary write, the replica checks that the keys fit its range and that its lease permits the request. So the sender's choice is a routing hint. The receiver still checks whether it can serve that write. On the selected path, the code proposes a replicated command and waits for its result. That outcome can be a reply or an error. The gateway trace gives us the route and returned values.
6:06 The majority and application stages come from the source model; we haven't measured replica acknowledgements or physical disk writes. Choosing a coordinator tells us where the request goes. Who leads agreement among the copies? Raft is the range's agreement protocol. Its leader drives agreement on ordered commands. The leaseholder handles ordinary requests; the leader coordinates replication. In this version, the leaseholder is normally also the Raft leader. A handoff can briefly separate them; it's not a permanent two-boss arrangement.
6:37 One node can receive SQL, keep a copy, and coordinate a range. Those jobs can overlap, so a server address alone doesn't identify its job. Finally, reconnect those roles along the request path. Follow the gateway through the encoded key and range routing, to the coordinator and replicated result. Then the SQL response comes back through the gateway. The client used a table and an update statement; it didn't supply a range ID or a leaseholder address. There are other read modes and larger transactions with additional rules.
7:09 We've followed ordinary update routing, not shown that every query visits a remote coordinator. If we're debugging row 100, we can follow its key to its range and inspect that coordinator, rather than infer one from the connection. That puts our investigation on node three for that row. For row one, inspect node two. Node one still has copies of both. Thanks for listening to Learning Podcasts.