Inside databases ClickHouse
ClickHouse
A column-oriented database: the values of each column are stored one after another, and a query reads from disk only the columns it needs. Its MergeTree tables keep data in sorted parts.
My work
An asynchronous ClickHouse cluster and ingestion into it from Kafka and RabbitMQ; part of the analytics moved here from Greenplum, from which data went to ClickHouse through external tables.
How it is built
An example of the mechanism: a generalised layout from the documentation of ClickHouse and of Arenadata DB. In my work data came into ClickHouse from Kafka and RabbitMQ, and from Greenplum through its external tables; the engines that read the brokers and the connector that sends Greenplum’s rows are the documentation’s example. The Distributed table over two shards of two replicas, with Keeper beside them, is set to write each row to one replica of its shard (internal_replication), and the replicated table copies it to the other.
While Keeper is unavailable
unavailable
- An INSERT into a replicated table while Keeper is unavailable throws an exception. A Distributed table writing in the background, as it does unless set otherwise, first accepts an insert into a local file: it is the sending on to the shard that fails.
- While a replica’s session with Keeper is being re-established, the replica is read-only. SELECT queries go on: they do not use Keeper. 1
4 7
Components
Press a component to read what it does.
Brokers Kafka, RabbitMQ
Kafka topics and RabbitMQ queues: streams that ClickHouse reads itself, as a consumer.
Consumers engine + view
A table with the Kafka or the RabbitMQ engine is a consumer, taken as a data stream; a materialized view attached to it converts what arrives and puts it into a table.
Greenplum external tables
The warehouse: Arenadata DB, based on Greenplum. Its writable external tables send data out, here into ClickHouse.
Distributed no own data
A table that stores no data of its own and allows distributed query processing on many servers: it sends queries to the shards and spreads inserted rows across them.
Replica 1 parts, shard 1
A server with a replicated MergeTree table: its data is a set of parts on its disk, each sorted by the table’s sorting key.
Replica 2 same shard
A second replica of the same shard. Data inserted on one replica is copied to it.
Keeper metadata
ClickHouse Keeper, where ClickHouse stores the replicas’ meta information. The data itself stays on the replicas.
Replica 1 parts, shard 2
A replica of the second shard, with the parts of its own share of the rows.
Replica 2 same shard
The second replica of the second shard.
-
From the brokers
A table with the Kafka or the RabbitMQ engine is a consumer. A materialized view attached to it collects data in the background and puts it into a table, here the Distributed one.
My part I built asynchronous ingestion into ClickHouse from Kafka and RabbitMQ.
In detail
SELECT is not particularly useful for reading such a table, except for debugging, because each message can be read only once: the documentation advises a materialized view that converts data from the engine and puts it into a previously created table. When the view joins the engine, it starts collecting data in the background. Kafka’s engine tracks the delivered messages, so each message in a consumer group is counted once; RabbitMQ’s sends basic.ack to the broker after a batch is pushed to the views, which lets the broker delete those messages. 8
9 Where it breaks Kafka’s offset is committed, and RabbitMQ’s messages acknowledged, once a batch is pushed to the views; the rows are durable only when their part is synced to disk, which by default does not happen at once. If the OS page cache is lost in between, the rows are lost with no error. fsync_after_insert on the target MergeTree tables narrows that window; a Distributed table on the way, writing in the background as it does by default, needs its own durability settings or synchronous insertion.
-
From Greenplum
Greenplum, here Arenadata DB, sends data to ClickHouse through a writable external table: rows inserted into it go to a table in ClickHouse.
My part Data went from Greenplum into ClickHouse through writable external tables of Greenplum.
In detail
Greenplum has readable external tables for loading data and writable ones for unloading it. In Arenadata DB, Tkhemali Connector 1.X sends data from ADB to ClickHouse through a writable external table whose LOCATION sets the PXF protocol with the TKH profile and the connection options for ClickHouse. With staging tables, the recommended way, the INSERT into that table is passed to the txn function: staging tables are an intermediate layer the connector creates in ClickHouse before inserting data into the target table, named with a number generated on the ADB master to emulate transaction numbers in ClickHouse. Without them, the INSERT runs against the external table directly. 10
11 Where it breaks With insert_distributed_sync on (an older name of distributed_foreground_insert), the connector’s default, an INSERT into a Distributed table counts as successful only after all data is added to all shards (with internal_replication, at least to one replica of each). And after an unexpected failure the staging tables may have to be deleted on all ClickHouse nodes, found from the ADB side by their number.
-
Sharding
A Distributed table stores no data of its own. SELECT queries are sent to all shards, and rows inserted into it are spread across the shards by a sharding expression.
In detail
Tables with the Distributed engine do not store any data of their own but allow distributed query processing on multiple servers, and reading is automatically parallelised. When querying a Distributed table, SELECT queries are sent to all shards. To choose the shard a row is sent to, the sharding expression is analysed and its remainder taken from dividing it by the total weight of the shards. 1
Where it breaks With internal_replication off, its default, the Distributed table itself writes each row to every replica of the shard; as drawn, it writes to one, and the replicated table copies it. And with distributed_foreground_insert off, inserted data is first stored on the node that accepted it and sent to the shards later, in the background.
-
Writing parts
On a shard, an INSERT into a MergeTree table creates one or more new parts, each sorted by the sorting key. In the Wide format each column of a part is a separate file.
In detail
A table consists of data parts sorted by primary key: when data is inserted, separate data parts are created, each lexicographically sorted by primary key, and data of different partitions goes into different parts. The sorting key is the table’s ORDER BY; where no PRIMARY KEY is given, it is the primary key as well. Parts are stored in the Wide or the Compact format: in Wide each column is stored in a separate file, in Compact all columns are in one file. 2
3 Where it breaks All parts are immutable: they are only created and deleted, not modified, so each insert adds parts. The Compact format can be used to speed up small and frequent inserts.
-
Merges
Background threads periodically select some parts and merge them into a single sorted part, to keep the number of parts low. Parts of different partitions are not merged.
In detail
There are background threads that periodically select some parts and merge them into a single sorted part to keep the number of parts relatively low: that is why the engine is called MergeTree. ClickHouse merges data parts in the background for more efficient storage. After merging, the old parts are kept for some time, to make recovery after a failure easier. 2
3 Where it breaks Merging leads to write amplification. And a merge does not guarantee that all rows with the same primary key end up in the same part.
-
Replication
Replication is asynchronous and multi-master: an INSERT can go to any replica that has a session with ClickHouse Keeper. An action to get the part is put in its replication log, and every other replica downloads the part.
My part An asynchronous ClickHouse cluster.
In detail
Replication is asynchronous and multi-master. INSERT queries can be sent to any available server: data is inserted on the server where the query is run, then copied to the other servers, so recently inserted data appears on the other replicas with some latency. ClickHouse uses ClickHouse Keeper for storing the replicas’ meta information, and there is a replication log that lists what actions to do: each replica copies it to its queue and executes the actions from it. On an insert, the action to get the part is created in the log, and every replica downloads that part. 3
4 Where it breaks Inserts are not acknowledged by a quorum by default: data just inserted can be lost if its node fails before the part is copied. The insert_quorum setting turns a quorum on.
-
Reading granules
The primary index is sparse: one entry per granule, up to 8192 rows by default. A query uses it to pick the granules that could match, and reads only the columns it needs.
My part Part of the analytics moved here from Greenplum.
In detail
Instead of locating single rows, the sparse primary index lets ClickHouse identify the groups of rows that could possibly match the query: by a binary search over its entries when the query filters on the first key column, by a generic exclusion search when it filters only on a later one. Those granules are then streamed into the engine in parallel. A granule holds from one row to index_granularity rows (8192 by default), depending on the size of the rows; it is the smallest indivisible set of data streamed into ClickHouse for processing, which always reads a whole granule, never single rows. 5
6 Where it breaks A selected granule can still hold rows that do not match: the index finds groups of rows that could match, and each granule it selects is read whole.
Sources
- ClickHouse: Distributed table engine
- ClickHouse: MergeTree table engine
- ClickHouse v24.8 LTS: Architecture Overview
- ClickHouse: Data Replication
- ClickHouse: A practical introduction to primary indexes
- ClickHouse: What is ClickHouse?
- ClickHouse: system.replicas
- ClickHouse: Kafka table engine
- ClickHouse: RabbitMQ table engine
- Greenplum 6: Defining External Tables
- Arenadata DB: Tkhemali Connector 1.X configuration
Next engine
ScyllaDB
A distributed database, a drop-in replacement for Apache Cassandra 3.11: each node is split into shards, one thread to a core, and tables are split into tablets placed across the nodes.