ChloeDB
Server and cluster

What a cluster does differently

Reference, rendered from docs/chloedb/cluster-limits.md.

These docs describe release 0.3.11. The docs of main describe the code as it is now.

On this page
  1. Refused on a cluster
  2. Different on a cluster
  3. Graphs and recommenders
  4. A table placed by a column (shard_by)

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 JOIN runs on the coordinator over every row of both sides, which it reads whole from the shards (a JOIN or LEFT JOIN reads 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 n and DELETE ... [ORDER BY ...] LIMIT n are two steps: the coordinator reads the keys of the rows the statement's WHERE, ORDER BY and LIMIT pick (over every shard), then runs the statement without its LIMIT over 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 the LIMIT is at most 10,000.
  • An UPDATE or DELETE whose WHERE has 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 its WHERE picks over every shard, then runs it over those keys alone (WHERE key IN (...), the subquery read once, in the first step). Without a LIMIT it writes at most 10,000 rows, and one that picks more is refused, with nothing written: narrow its WHERE, or run it in parts. One whose WHERE reads only its own row (a TEXT column compared with =, LIKE) runs on each shard, as any write does.
  • INSERT ... ON CONFLICT DO NOTHING goes as an insert_if_absent operation (protocol 21: a shard of an older version refuses it as an unknown operation). ON CONFLICT DO UPDATE runs as written on the one shard its rows go to.
  • INSERT ... SELECT and CREATE TABLE ... AS SELECT run their SELECT over every shard (one that does not answer fails the statement), then make the table on every shard as CREATE TABLE does, then write the rows as INSERTs of 500 rows each through the coordinator, one row each for ON CONFLICT ... DO UPDATE: each INSERT is 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 (query language). The table a CREATE TABLE ... AS SELECT makes needs a primary key the statement names (CREATE TABLE t (PRIMARY KEY (id)) AS SELECT ...) or OPTIONS(shard_by = ...), since the coordinator places each row on a shard by it: one without is refused before its SELECT runs, and POST /v1/sql/check says 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 with IF NOT EXISTS, IF EXISTS or OR REPLACE. ALTER TABLE ... ADD COLUMN c ... NOT NULL without 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 TABLE through the coordinator, as its plan (plan=true) says.
  • Exports: GET /v1/export?format=jsonl|csv|arrow|parquet&tables=T reads 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 are null. A strict write goes only to shards of protocol 21.

Graphs and recommenders#

  • Graph walks (REACHABLE, PATH, hops(), ppr()) over an EDGES column 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 both REFERENCES the table walked counts a row's edges first and reads them only to push it (graphs). REACHABLE and hops() take about one round per hop, PATH one 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 says walked by the coordinator in N rounds, and the answer is one database's, but for two things: PATH may pick another path of the same length, and ppr() scores may differ in the last digits, since it pushes rows in the order it met them (more when it adapts its own eps). A walk is refused past max_nodes rows, 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 RECOMMENDER runs 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) and sum(n) grouped by item, and every row of the user's), ranks as one database does, and runs the query with each call as ranked(...) of the keys; the plan says counts 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 RECOMMENDER counts 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 different shard_by values are not ordered by this check, and can both succeed: send the inserts of one key through one coordinator, or give the table a shard_by equal to the key's own placement.
  • INSERT OR REPLACE (upsert) replaces a row only on the shard of its shard_by value. 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 NOTHING passes over a key that another shard holds; DO UPDATE of 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". A set, add or delete whose routing places it on a shard that has no row with the key is refused (404, naming the routing and the column) rather than answered written: 0; "if_missing": "skip" passes over it as before. DELETE /v1/kv/T/K without routing on such a table is refused (400).
  • The column cannot change: UPDATE of it, and set or add on it, are refused. A row that moves is a delete and an insert.
  • Queries ask every shard, as for any table.