libch
libch runs ClickHouse's insert and merge code as explicit, short-lived operations on a tenant's disk, so one pool of workers serves a very large number of tenants. The disks are BigHouse inodedb disks: MergeTree tables whose blobs live in S3 and whose file tree lives in InodeDB. A disk libch writes is a stock BigHouse disk: read-only ClickHouse servers query it, and a stock writer can take it over, at any time.
A Rust crate drives a worker program built from the BigHouse fork of ClickHouse. Each operation starts from the tenant's key and the state in InodeDB, does one thing, commits and exits:
| Operation | What it does |
|---|---|
insert |
Commits a batch to a table and to every materialized view's target, once, however often it is sent under its token. |
plan |
Proposes merges with ClickHouse's own selector. |
merge |
Merges named parts into one. |
housekeep |
Removes what merges and dead workers leave behind. |
describe |
Lists a tenant's tables and parts as the worker's engine sees them. |
share |
Hands a disk back to operations once the tenant's exclusive writer has stopped. |
Why operations
Writing a tenant through stock ClickHouse means booting a process for each batch or keeping one running for every tenant. A stock process loads every part of every table it attaches, so its boot costs many times the insert and grows with the table. A libch operation's boot and insert cost about the same at any table size, because a worker loads none of a table's parts: 40 to 41 ms of CPU through a worker of its own, and 16 to 17 ms through a forking worker, from 10 parts a table to 10,000.
| Parts a table | Insert into a running writer | Stock server: boot, insert, stop | clickhouse local |
libch, a worker per operation | libch, a forking worker |
|---|---|---|---|---|---|
| 10 | 5.7 ms | 206 ms (36×) | 128 ms (22×) | 40 ms (7×) | 15.7 ms (2.8×) |
| 100 | 5.8 ms | 394 ms (68×) | 297 ms (51×) | 41 ms (7×) | 16.7 ms (2.9×) |
| 500 | 5.2 ms | 1,009 ms (194×) | 908 ms (175×) | 41 ms (8×) | 16.4 ms (3.2×) |
Each figure is the CPU for one 1,000-row insert through a projection and a materialized view, with its multiple of the first column: the same insert into a writer that is already running. The stock processes run with background merges off, as a process that serves one batch would. A forking worker starts the engine once and serves each operation in a child it forks. A stock server holds 142 to 200 MB and over 200 threads and answers in 240 to 341 ms; a worker holds 32 MB and 33 threads and answers in 23 to 28 ms.
A worker also builds only the tables its operation reaches. BENCHMARKS.md has the method and every operation's cost.
The recipe
let pool = new?;
let keys = new?;
let writer = pool.writer;
writer.insert.await?;
for planned in writer.plan.await?
writer.housekeep.await?;
A pool made with Pool::with_observer reports every operation as it ends: its outcome, its times, what the worker spent and what it wrote. The metrics feature records those reports through the metrics crate, for the exporter a service installs (OPERATING.md, section Reports and metrics).
demo/ runs the whole recipe for many tenants: batching, one insert in flight for each tenant, retries under a token, compaction from the catalog, housekeeping, and queries through read-only servers that belong to no tenant. OPERATING.md says what each call can answer and what a service around libch decides.
Releases
libch is published to crates.io as libch from every merge to main (RELEASING.md), with its documentation on docs.rs. Each release's notes name the commit of the fork's libch-worker branch it speaks to: build the fork's clickhouse at that commit for its workers.
Limits
- DDL and mutations run in a stock exclusive writer.
stock::Exclusive::runboots one for a tenant, runs statements and stops. From its start the tenant's operations endFenced, so none interrupts it, and they run again oncesharehands the disk back. - A view reads the rows it is fed. A worker refuses an insert whose view also reads stored rows, names a table function or reads a dictionary.
- Exactly-once needs tokens. Every table an insert reaches keeps deduplication tokens (
non_replicated_deduplication_window), or the writer waives exactly-once. - A source part and its views' parts commit one after another. A retry under the token completes an insert that died between them.
planloads every part of its table. That costs about half a millisecond of CPU a part on every call, where a stock server pays it once at boot, so a caller plans the tables the catalog names as crowded and keeps them compacted.- The sandbox needs Linux 6.12. It confines a worker by file, port, signal and system call; which hosts a worker reaches is for the network to say.
PLAN.md holds the goal, the phases, and the evidence for each.
What is here
| Path | Contents |
|---|---|
crates/libch |
The crate: catalog, part, disk, the Writer trait, stock servers, check, a small S3 client, worker (the client that runs each operation in a worker process, and the sandbox), pool (workers started ahead of demand), keys (tenants' keys, minted, cached and revoked), observe (a report of each operation as it ends) and, with the metrics feature, metrics (those reports as metrics). |
proto/libch.proto |
The protocol between the crate and a worker, which libch::proto compiles in as generated code. |
crates/libch-testkit |
What tests, benchmarks and the demo share: the dev stack, the preflight, a tenant fixture, the conformance scenarios, the differential harness and its workload generator, a proxy that fails calls to the store, process measurements, and a stand-in worker for protocol tests. |
crates/libch/tests |
Integration tests against the dev stack. |
crates/libch/benches |
The benchmarks that write BENCHMARKS.md. |
demo |
libch-demo, the high-tenant recipe end to end, and demo/run, which starts its large runs. |
scripts/dev-up, scripts/dev-down |
libch's own dev stack: InodeDB on port 7170 and s3proxy on port 11211. |
scripts/fork-setup, scripts/fork-build |
The worker's branch of the fork: a git worktree at fork/ClickHouse and its build at fork/build. |
scripts/soak, scripts/mutants |
The differential soak, and planted bugs that the tests must catch. |
bighouse.rev, fork.rev, inodedb.rev |
The BigHouse commit, the commit of the fork's libch-worker branch, and the InodeDB commit whose server the dev stack runs, that this code runs against. |
.github/workflows, scripts/ci-test |
CI, and a release to crates.io on every merge to main (RELEASING.md). |
Running the tests
The tests need:
- a BigHouse checkout beside this repository (or at
BIGHOUSE_DIR), at the commit inbighouse.revor later, with the fork's ClickHouse built (scripts/clickhouse-build) and InodeDB built (scripts/inodedb-build); - the worker's branch of the fork,
libch-worker, in that checkout'sClickHouserepository, built here; - Docker, for s3proxy;
protoc, whichlibch-testkit'stests/proto.rsruns.
The first scripts/fork-build compiles all of ClickHouse, about an hour on 24 cores. LIBCH_CLICKHOUSE points the tests at a binary built elsewhere.
scripts/ci-test runs the tests that need neither the stack nor the fork: the crate's unit and doc tests, and the tests against a stand-in worker. CI runs it.
Every test that needs the stack runs a preflight first and fails with what to start or build. Each test writes a tenant of its own and removes it, so tests run side by side and leave the stack empty.
| Command | What it runs |
|---|---|
cargo test |
The suite: about six minutes on a freshly wiped stack. |
LIBCH_SANDBOX=1 cargo test |
The suite with every worker in the sandbox. |
LIBCH_FORK=1 cargo test |
The suite with every worker forking a child for each operation. |
cargo test -p libch --test pool_tenants -- --ignored |
Sixty-four tenants at once through one pool. |
LIBCH_KILL_ROUNDS=1000 cargo test -p libch --test worker_kill_loop |
The kill loop at the plan's length. |
LIBCH_FORK_OPERATIONS=10000 cargo test -p libch --test worker_fork a_parent_serves |
Ten thousand operations through one forking worker. |
PARTS=10000 cargo test -p libch --test plan_beside_stock_boot -- --ignored --nocapture |
A plan beside a stock server's boot on a table of 10,000 parts. |
scripts/soak --minutes 60 |
The differential soak. |
scripts/mutants |
Each planted bug in turn, about an hour of builds. |
demo/run gate, demo/run kills, demo/run wide |
The demo's large runs, each from an empty stack: 1,000 tenants and 100,000,000 rows, the same with insert workers killed mid-operation, and 10,000 tenants. --tmpfs keeps the InodeDB store in memory. LIBCH_INODEDB_BIN runs another InodeDB server in place of BigHouse's. |
cargo bench --bench stock, cargo bench --bench operations |
The benchmarks. They refuse to record on a busy machine. |
License
libch is under the libch Development License. It permits developing libch or software that works with it, fixing its bugs, and reviewing its security, and grants no commercial rights. The license is provisional: it changes at libch's official release. The worker is under the fork's LICENSE-BIGHOUSE.