What a cluster does differently
Reference, rendered from docs/chloedb/cluster-limits.md.
These docs follow main, which can be ahead of the latest release, 0.3.11 (its docs): a change made after it says so where it is described.
On this page
A coordinator over shards (sharding) answers most requests as one server does: the tests run every query kind against three shards and one database and compare the answers. This page lists what it refuses, and what it does in another way, so that nobody finds out in production. A table placed by a column (OPTIONS(shard_by = '...'), tables on shards) has a few differences of its own, listed after.
POST /v1/sql/check with {"sql": ..., "params": ...} says whether a statement will run on the deployment it is sent to, without running it: {"runs": true, "deployment": "cluster", "kind": "write", "notes": [...]}, or runs: false with the error it would get. A pipeline's CI can send its statements to the cluster it deploys to, with a key of every table, a read key included (a key limited to tables is refused).
Refused on a cluster#
| what | on one server | on a cluster | instead |
|---|---|---|---|
Graph walks over a VECTOR column (REACHABLE(emb, ...), ppr(emb, ...) over the nearest-neighbour graph) |
run | refused: the graph is each shard's own index | walk an EDGES column or an edge table, or run it on a server that holds the whole table |
ALTER TABLE ... DROP COLUMN |
runs | refused: the coordinator's plans name columns by place | add a column and stop reading the old one |
INSERT ... ON CONFLICT DO UPDATE whose rows go to several shards |
runs | refused (400) | one statement per row or per shard, or INSERT OR REPLACE |
The relevance autopilot (POST /v1/relevance/autopilot) |
runs | refused: it replays queries on one snapshot | run it on a server with a copy of the tables |
/v1/export of the database as SQLite (format=sqlite) |
the admin's | refused: a coordinator holds no files | format=parquet, jsonl, csv or arrow export one table's rows from a cluster |
EXPLAIN through /v1/sql |
refused on both | POST /v1/explain |
|
CREATE RECOMMENDER over an events table that is not placed by its user column, on a coordinator of several shards |
runs | refused: each user's events are counted in order, on one shard | create the events table with OPTIONS(shard_by = '<user column>') and load its events again (below) |
Different on a cluster#
-
FULL JOINruns on the coordinator over every row of both sides, which it reads whole from the shards (aJOINorLEFT JOINreads the second side's rows by the first side's join values only); it answers as one database does. -
A write across shards is one transaction a shard, not one over all. The coordinator checks each shard's part first and writes nothing when one refuses; what can still fail partway, and how to finish it with the idempotency key, is in when a write fails.
UPDATE ... LIMIT nandDELETE ... [ORDER BY ...] LIMIT nare two steps: the coordinator reads the keys of the rows the statement'sWHERE,ORDER BYandLIMITpick (over every shard), then runs the statement without itsLIMITover those keys alone (WHERE (...) AND key IN (...)). A row changed in between so that it no longer matches is left alone, so fewer than n rows may change. The table needs a primary key, and theLIMITis at most 10,000.- An
UPDATEorDELETEwhoseWHEREhas a subquery (WHERE id IN (SELECT ...),NOT EXISTS (...)), which reads rows of other shards, is two steps the same way: the coordinator reads the keys of the rows itsWHEREpicks over every shard, then runs it over those keys alone (WHERE key IN (...), the subquery read once, in the first step). Without aLIMITit writes at most 10,000 rows, and one that picks more is refused, with nothing written: narrow itsWHERE, or run it in parts. One whoseWHEREreads only its own row (aTEXTcolumn compared with=,LIKE) runs on each shard, as any write does. INSERT ... ON CONFLICT DO NOTHINGgoes as aninsert_if_absentoperation (protocol 21: a shard of an older version refuses it as an unknown operation).ON CONFLICT DO UPDATEruns as written on the one shard its rows go to.INSERT ... SELECTandCREATE TABLE ... AS SELECTrun theirSELECTover every shard (one that does not answer fails the statement), then make the table on every shard asCREATE TABLEdoes, then write the rows asINSERTs of 500 rows each through the coordinator, one row each forON CONFLICT ... DO UPDATE: eachINSERTis a write of its own, so a failure part way leaves the rows written before it, and a table made for rows that failed is dropped. When every shard speaks protocol 22 and theSELECTreads one table's rows each by itself (columns and aWHERE, with noGROUP BY, aggregate,ORDER BY,LIMIT, ranking or join), into a table placed by its key, the rows skip the coordinator: each shard's primary runs theSELECTover its own rows and writes each row to the shard its slot is on, in writes of 1,000 rows (RunTask), and only the counts come back. AGROUP BYof one KEYWORD or INT column, with counts and aggregates (andHAVING, but noORDER BYorLIMIT), into a table whose key is that column runs so too, in two stages when every shard speaks protocol 23: each shard sums its own rows into partial groups and sends each to the shard of its group's key (Exchange), then each shard merges the groups it was sent and writes them, so neither the rows nor the groups pass through the coordinator. The rows written are the same, and so is what a failure leaves; aCREATE TABLE ... AS SELECTgoes this way only when its columns' types are known before its rows are (each a column of a table it reads, aCAST, a count or another expression of a number, BOOL or TIMESTAMP, or defined by the statement), as the table is made first.CHLOEDB_TASKS=offon the coordinator sends every such statement through it as before (query language). The table aCREATE TABLE ... AS SELECTmakes needs a primary key the statement names (CREATE TABLE t (PRIMARY KEY (id)) AS SELECT ...) orOPTIONS(shard_by = ...), since the coordinator places each row on a shard by it: one without is refused before itsSELECTruns, andPOST /v1/sql/checksays so. On one server the rows are one transaction, and a table needs no key.- DDL (
CREATE TABLE,ALTER TABLE,DROP TABLE, rank profiles, functions) runs on every shard, one change a shard: a shard that fails leaves the others changed, and the statement is sent again withIF NOT EXISTS,IF EXISTSorOR REPLACE.ALTER TABLE ... ADD COLUMN c ... NOT NULLwithout a default is refused by each shard that has rows, and the error counts every such shard's rows. - A SQLite or Parquet import that creates its table creates it on every shard by
CREATE TABLEthrough the coordinator, as its plan (plan=true) says. - Exports:
GET /v1/export?format=jsonl|csv|arrow|parquet&tables=Treads the table by a scroll over every shard, with read on the table. Each page is read from the shards as they are then, not one snapshot of all of them: a write during the export may or may not be in it. Arrow is one batch built in memory (at most 1,000,000 rows); Parquet is written a page at a time and keeps the table's schema. - Rank-log export and interleaving credit (
/v1/rank_log/export,/v1/rank_log/interleaving) run their queries over every shard, each read whole by a scroll, rather than from one snapshot: a request logged during the export may be left out or come with part of its events. - Conditional writes (
if_seq_no,seq_no()) need every shard to number its writes alike (one replica, or a primary its replicas follow), and answers carry each shard's number (seq_nos), not one. - Write receipts count per shard (
receipt.per_shard); a shard from before protocol 21 does not count, and its counts arenull. A strict write goes only to shards of protocol 21.
Graphs and recommenders#
- Graph walks (
REACHABLE,PATH,hops(),ppr()) over anEDGEScolumn or an edge table run on the coordinator: it runs the same walk one database runs, over what it has read so far, and reads what the walk is missing in one round of reads by key over every shard (keys in batches of 2,000, four reads at a time), then walks again, until nothing is missing.ppr()over an edge table whose ends bothREFERENCESthe table walked counts a row's edges first and reads them only to push it (graphs).REACHABLEandhops()take about one round per hop,PATHone per hop from either end,ppr()one each time its pushes reach rows it has not read. Each shard is then given the walk's answer (hops, place on the path, score) for its own rows, and the rest of the query (filters,bm25,GROUP BY, joins) runs as for any query. The plan sayswalked by the coordinator in N rounds, and the answer is one database's, but for two things:PATHmay pick another path of the same length, andppr()scores may differ in the last digits, since it pushes rows in the order it met them (more when it adapts its owneps). A walk is refused pastmax_nodesrows, as on one database, and after 256 rounds. - A coordinator of one shard that holds every slot (no move in progress) sends graph queries to that shard, which walks them itself.
- Joins along links (
JOIN items b ON b.cooc = a.id, rows that point at a key) run on a cluster too: each shard answers one hop in by key from its own rows' edges, whatever shard the key's row is on. - Recommenders run when the events table is placed by its user column (
OPTIONS(shard_by = 'usr')).CREATE RECOMMENDERruns on every shard, and each shard counts the events it holds, as they are written, in its own rows of the counts table (keys that end with its slot map's epoch and shard).recommend(...)reads the counts summed over the shards (sum(next),sum(co)andsum(n)grouped by item, and every row of the user's), ranks as one database does, and runs the query with each call asranked(...)of the keys; the plan sayscounts summed over the shards. So a counts table read directly on a cluster holds each shard's part, not the totals. After a move of slots, the shards that take a user's events count them in new rows of their own, and a user's recent events are those of all their rows;CREATE OR REPLACE RECOMMENDERcounts everything again. - Kept whole on every shard: not yet. An edge table is placed by its key like any table, so a walk over it reads by key each round. Keeping a small edge table whole on every shard would let each shard walk it alone; that is next.
A table placed by a column (shard_by)#
A row goes to the shard of its shard_by value, not of its key. So:
- A primary key is unique across the shards for
INSERT. Before it writes, the coordinator asks every shard's primary whether it holds the keys being inserted (one read by key a shard, for the whole request), and refuses a key another shard holds (409, naming the shard and the row's value of the column). Two inserts of one key through one coordinator wait for each other. Two coordinators inserting one key at the same moment with differentshard_byvalues are not ordered by this check, and can both succeed: send the inserts of one key through one coordinator, or give the table ashard_byequal to the key's own placement. INSERT OR REPLACE(upsert) replaces a row only on the shard of itsshard_byvalue. An upsert that gives an existing key another value of the column writes a second row there and leaves the old one (as Elasticsearch's custom routing does): change the column by deleting the row first.ON CONFLICT DO NOTHINGpasses over a key that another shard holds;DO UPDATEof a key that another shard holds under another value of the column is refused (409), since it would insert a second row.- Writes by key give the column's value as
"routing". Aset,addordeletewhose routing places it on a shard that has no row with the key is refused (404, naming the routing and the column) rather than answeredwritten: 0;"if_missing": "skip"passes over it as before.DELETE /v1/kv/T/Kwithoutroutingon such a table is refused (400). - The column cannot change:
UPDATEof it, andsetoraddon it, are refused. A row that moves is a delete and an insert. - Queries ask every shard, as for any table.