Lesson 6 of 9 · 24m
Ingestion Patterns and Backpressure
Batching, Kafka pipelines, exactly-once myths, idempotent retries, file loading, and what to monitor before producers bury the cluster.
ClickHouse can ingest astonishing volumes, but only if you feed it like a batch system rather than a row-at-a-time transaction log. Most ingestion incidents are not caused by ClickHouse being fragile. They are caused by producers sending a million tiny inserts, retrying without idempotency, or treating a queue backlog as somebody else's problem.
The unit of ingestion is the block
A good insert is a block: thousands to millions of rows in one request, preferably in a compressed binary or efficient textual format. JSONEachRow is convenient for pipelines and humans, but native formats such as Native, Parquet, or CSV can be more efficient depending on the source.
INSERT INTO events FORMAT JSONEachRow
{"event_time":"2026-11-29 12:00:01.123","tenant_id":42,"user_id":99,"event_name":"login","bytes":512}
{"event_time":"2026-11-29 12:00:01.128","tenant_id":42,"user_id":100,"event_name":"logout","bytes":256}Batching is not only a throughput optimization. It controls how many parts are created. Insert concurrency matters too: a moderate number of large parallel inserts is usually better than hundreds of tiny ones fighting for merge capacity.
Kafka is a buffer, not a correctness spell
A mature pipeline often looks like this:
producers -> Kafka -> ClickHouse Kafka engine -> materialized view -> MergeTreeKafka smooths bursts and gives you replay. The ClickHouse Kafka engine consumes in blocks, which matches the storage model. But consumers can fail after writing and before committing offsets. That means duplicates are possible, even if the marketing phrase "exactly once" is floating around the design doc.
Make the pipeline idempotent. Include a stable event ID in the payload, deduplicate in a downstream aggregate where correctness matters, or accept that analytics are approximate by design and say so explicitly. The worst answer is the unstated one.
Files and backfills are first-class citizens
ClickHouse is excellent at ingesting files:
clickhouse-client --query "INSERT INTO events FORMAT Parquet" < events-2026-11.parquetYou can also query object storage directly with the s3 table function for inspection or one-off loads. For serious backfills, load into a staging table, validate counts and distributions, then insert into production in bounded chunks. Do not point a year's worth of data at the live table and hope merge capacity will forgive you.
Backpressure must exist somewhere
If ClickHouse slows down, producers need a signal. Kafka lag is a natural one. For direct inserts, monitor insert latency, errors, memory usage, and the parts count. A pipeline that retries forever with no jitter can turn a slowdown into a distributed denial-of-service attack authored by your own infrastructure.
Useful checks include:
SELECT count(), sum(rows) FROM system.parts WHERE table = 'events' AND active;
SELECT * FROM system.merges ORDER BY elapsed DESC LIMIT 5;
SELECT * FROM system.errors ORDER BY last_error_time DESC LIMIT 10;Field notes
- Prefer fewer, larger inserts over maximum connection concurrency. The merge system pays the bill for every part.
- Put event IDs in the payload even if the current design does not deduplicate. Future you will want the option.
- Keep a replay path. If data cannot be re-ingested, every schema or parsing mistake becomes permanent damage.
- Watch asynchronous insert and distributed insert queues if you enable those features. Convenience queues still fill disks.
- Backfills should be throttled even when the cluster looks idle. Merges lag behind visible insert success.
Was this lesson helpful?