ClickHouse small inserts and async_insert, measured: the 26.2 default that made one-row inserts 10x slower

ClickHouse turns on async_insert by default since 26.2. One client sending single rows got 16 rows/s instead of 167, still one part per row. Batching wins.

Every ClickHouse guide says the same thing about inserts: send big batches, never one row at a time, or you will drown in parts and hit Too many parts. Since version 26.2 there is a twist. ClickHouse now turns on async_insert by default: small inserts go into a server-side buffer that is flushed every so often as one part. So does the old advice still apply, or does the server batch for you now?

This is day 3 of a daily ClickHouse series built on experiments rather than docs. Yesterday’s post looked at the default compression codec; day 1 explained parts and granules, which you need for today. I sent the same kind of rows in batches of 1 to 100,000, with async inserts off, on (the default) and on without waiting, from one client and from 32. Everything ran on ClickHouse 26.10.3.7 in Docker with default settings.

The short version: the new default does not batch for a client that sends one row and waits for the answer. That client got 16 rows per second instead of 167, and still produced one part per row.

async_insert = 0every INSERT is written at once: 1 insert = 1 part, ~5 ms eachdefault, 1 client(sends, waits, sends)timer ~50 mstimer ~50 mstimer ~50 msthe buffer only ever holds this client's one row: 1 insert = 1 part, ~60 ms eachdefault, 32 clientstimer climbs to ~200 ms under load~25 inserts share one flush: 25 inserts = 1 part, but each client waits ~190 ms
Why the new default doesn't help a client that sends one row and waits: the buffer is flushed by a timer, and that client never has a second row in it.

What changed in 26.2

async_insert used to be opt-in. Since 26.2 its default is 1, together with wait_for_async_insert = 1, which means the client’s INSERT returns only after its data has been flushed into a part. You can check it on any server:

SELECT name, value FROM system.settings
WHERE name IN ('async_insert', 'wait_for_async_insert', 'async_insert_busy_timeout_min_ms',
               'async_insert_busy_timeout_max_ms', 'async_insert_max_data_size');

SELECT version, c.1 AS setting, c.2 AS old, c.3 AS new
FROM system.settings_changes ARRAY JOIN changes AS c
WHERE c.1 LIKE 'async_insert%';

On 26.10 that returns async_insert = 1, wait_for_async_insert = 1, a busy timeout between 50 and 200 ms, a 10 MiB buffer limit, and the change history: async_insert went from 0 to 1 in 26.2, and in 26.10 INSERT ... SELECT became async by default too. The official asynchronous inserts guide describes the buffer. What it does not show is what this costs a client, so I measured it.

The experiment

  • Table: MergeTree, four columns (DateTime64(3), UInt64, LowCardinality(String), Float64), ORDER BY (event, ts), no partitioning.
  • Client: a small Python program that sends TSV over HTTP on keep-alive connections, either from one thread (send, wait for the answer, send the next) or from 32 threads at once. The rows are generated before the clock starts.
  • Measured per run: throughput, time per INSERT, parts created and merges (system.part_log), bytes written by inserts and merges, peak active parts, and the server’s CPU from its container cgroup over the run plus 30 seconds of merging afterwards, minus the idle baseline (58 ms of CPU per second on this server).
  • Machine: a 24-core desktop shared with other jobs. Each case ran once; repeating three of them moved throughput by 5 to 17%, so read the ratios, not the last digit.

Batch size still decides everything

First the old behaviour, with async_insert = 0, from one client:

Rows per INSERT Rows sent Rows/s Parts created Bytes written per row Server CPU per row
1 5,000 167 5,000 10,236 12.2 ms
10 50,000 1,652 5,000 6,603 1.35 ms
100 200,000 6,679 2,000 986 243 µs
1,000 1,000,000 212,291 1,000 217 11.2 µs
10,000 1,000,000 924,304 100 56 3.3 µs
100,000 1,000,000 1,127,352 10 24 2.6 µs

Every INSERT became exactly one part, as expected. A one-row part is about 1.2 KB on disk for 4 columns of data, because every part carries its own metadata: checksums, column list, row count, primary index and marks. Then merges copy those rows again and again as small parts are merged into bigger ones. In total, each stored row cost 10 KB of writes at batch size 1 against 24 bytes at 100,000, for a table that ends up at about 15 bytes per row.

CPU tells the same story. A one-row insert took 5.3 ms of CPU in the query itself (system.query_log), each of the 1,000 merges took another 4.5 ms, and the rest is the server’s work around them. Per stored row, that is 4,700 times more than a 100,000-row batch.

1 µs10 µs100 µs1 ms10 ms100 ms1 row1 row per INSERT: 12.2 ms of server CPU per row12.2 ms10 rows10 rows per INSERT: 1.4 ms of server CPU per row1.4 ms100 rows100 rows per INSERT: 243 µs of server CPU per row243 µs1,000 rows1,000 rows per INSERT: 11.2 µs of server CPU per row11.2 µs10,000 rows10,000 rows per INSERT: 3.3 µs of server CPU per row3.3 µs100,000 rows100,000 rows per INSERT: 2.6 µs of server CPU per row2.6 µs
Server CPU per stored row (inserts plus the merges they cause, idle baseline subtracted), one client, async_insert=0. Note the log scale: 1-row inserts cost about 4,700 times more per row than 100,000-row inserts.

One number I could not pin down: the 100-row run took 14 ms per insert against 5.5 ms for 10 rows, while everything else scaled smoothly. That run overlapped with another heavy job on the same machine, so treat its throughput as a lower bound. Its part count, bytes and CPU per row don’t depend on the timing.

The new default, from one client

Now the same one-client tests with the 26.10 defaults (async_insert = 1, wait_for_async_insert = 1), next to the old behaviour:

Rows per INSERT Setting Rows/s Median per INSERT Parts per 1,000 inserts Server CPU per row
1 async_insert=0 167 5.5 ms 1,000 12.2 ms
1 default 16 60.6 ms 1,000 11.2 ms
1 wait_for_async_insert=0 2,643 0.34 ms 2.8 0.79 ms
100 async_insert=0 6,679 14.4 ms 1,000 243 µs
100 default 1,575 61.7 ms 1,000 317 µs
10,000 async_insert=0 924,304 10.1 ms 1,000 3.3 µs
10,000 default 144,030 69.1 ms 1,000 4.9 µs

The default gave this client the worst of both worlds: still one part per insert, and 10 times fewer rows per second. A loader that already sends 10,000-row batches lost 84% of its throughput to the same wait.

The reason is in how the buffer works. Each insert joins a buffer that is flushed when a timer runs out (or when the buffer reaches 10 MiB). With wait_for_async_insert = 1 the client waits for that flush. A client that sends one insert and waits never has a second insert in the buffer, so the timer just delays its single row, and the flush writes a one-row part anyway.

Where the 60 ms comes from

The timer is adaptive (async_insert_use_adaptive_busy_timeout = 1): it moves between async_insert_busy_timeout_min_ms (50) and ..._max_ms (200), rising when inserts arrive often and falling when they don’t. On a quiet server it sits at 50 ms. The flush thread also wakes on a 10 ms poll interval (async_insert_poll_timeout_ms), so a flush lands up to 10 ms after its deadline: 50 + 10 = the ~60 ms I measured. Changing the minimum moves it exactly as expected:

One client, 1 row per INSERT Mean per INSERT Rows/s
async_insert_busy_timeout_min_ms = 10 20.7 ms 48
default (min 50), quiet server 61.2 ms 16
default, after a burst of concurrent inserts 2 ms and 208 ms, alternating 9
async_insert_busy_timeout_min_ms = 200 109 ms (median 206 ms, about half flushed at once) 9
async_insert_use_adaptive_busy_timeout = 0 (always the max) 212 ms 5

The surprise: the same client got slower after a burst

When I repeated the one-client test later, the same client ran at 9 rows/s, with inserts taking alternately 2 ms and 208 ms. I read the source of AsynchronousInsertQueue and the server’s trace log to understand why. The timer state is kept per queue shard (16 of them, one per async_insert_threads), not per table or client, and a previous 32-client test had pushed that shard’s timer close to 200 ms. From there the rules lock into a loop:

  1. An insert arrives and waits for the ~198 ms timer, plus up to 10 ms of polling. That is more than the 200 ms maximum.
  2. The next insert arrives 1 ms later. More than 200 ms have passed since the previous insert and between the last two flushes, so the “maximum busy timeout exceeded” rule flushes it immediately (2 ms).
  3. The insert after that arrives 2 ms after the previous one, which counts as “frequent”, so the timer goes up again to 198 ms. Back to step 1.

The trace log shows exactly this pattern on every pair of inserts: Asynchronous timeout decreased from 198 to 165, an immediate flush, then Asynchronous timeout increased from 165 to 198. The timer never decays back to 50 ms while this client keeps sending. So the latency one client sees depends on what other inserts did to its shard earlier. Both states still produce one part per insert.

async_insert=0default1 client · 1 row1 client · 1 row, async_insert=0: 5.5 ms5.5 ms1 client · 1 row, default (async on): 60.6 ms60.6 ms1 client · 10k rows1 client · 10k rows, async_insert=0: 10.1 ms10.1 ms1 client · 10k rows, default (async on): 69.1 ms69.1 ms32 clients · 1 row32 clients · 1 row, async_insert=0: 16.9 ms16.9 ms32 clients · 1 row, default (async on): 183.4 ms183.4 ms
Median time per INSERT with wait_for_async_insert=1 (the default). The client waits for the flush timer whether it sent one row or 10,000.

Where the default does help: many concurrent writers

The buffer is meant for many clients writing at once, for example thousands of devices or functions that each send a few rows. Here are 32 clients sending one row per insert:

32 clients, 1 row per INSERT Rows/s Median / p99 per INSERT Parts created (32,000 rows) Peak active parts Server CPU per row
async_insert=0 1,527 16.9 / 80.7 ms 32,000 505 6.1 ms
default 169 183 / 224 ms 1,255 6 2.7 ms
async_insert_busy_timeout_max_ms=50 528 61 / 74 ms — — —
wait_for_async_insert=0 4,551 5.9 / 21 ms 72 6 1.1 ms

This is the problem the default solves. Without it, 32 one-row writers created 32,000 parts in 21 seconds and reached 505 active parts, halfway to the point where ClickHouse starts delaying inserts (parts_to_delay_insert = 1000) and a sixth of the way to the Too many parts error at 3,000. With the default, about 25 inserts shared each flush: 25 times fewer parts and less than half the CPU per row.

But the cost moved to the clients. Under steady load the adaptive timer climbs to its maximum, every client waits about 190 ms per insert, and total throughput falls to almost exactly clients / timeout: 32 / 0.19 s = 168 rows/s, against 169 measured. Lowering async_insert_busy_timeout_max_ms to 50 ms tripled the throughput. Not waiting at all (wait_for_async_insert = 0) was the fastest by far, but the client is then told “OK” before the data is written: if the flush fails, the client never knows. The docs call this mode “very risky”, and I agree.

What to do with this

  • If your client already batches (1,000+ rows per insert), turn async inserts off for it. Set async_insert = 0 on that user, profile or connection. The default made a 10,000-row loader 6.4 times slower here and saved nothing, because each batch was already one part.
  • If one process sends rows one by one and waits, async inserts don’t help at all. Buffer on the client instead: batch every N rows or every second, whichever comes first.
  • If many writers send tiny inserts, the default is doing its job. Expect each insert to take up to the busy timeout (200 ms by default). Get more throughput from more concurrent writers or a lower async_insert_busy_timeout_max_ms, not from tighter loops.
  • Watch the part count, not just errors. SELECT count() FROM system.parts WHERE table = '...' AND active and the NewPart rate in system.part_log show trouble long before Too many parts does.
  • Upgrading from before 26.2? Measure your ingest latency before and after. If you need the old behaviour everywhere, compatibility = '26.1' or async_insert = 0 in the default profile restores it.

FAQ

Is async_insert on by default in ClickHouse? Yes, since 26.2 (async_insert = 1, wait_for_async_insert = 1). system.settings_changes lists the change. Earlier versions had it off.

Does async_insert fix “Too many parts”? Only when several inserts arrive within the same busy timeout. With 32 concurrent one-row writers it cut parts 25 times. With one client that sends a row and waits, it changed nothing: one part per insert.

Why does every INSERT take about 60 ms (or 200 ms) after upgrading? With wait_for_async_insert = 1 the client waits for the buffer’s flush timer: about 50 ms plus a 10 ms poll on a quiet server, up to 200 ms under load. Set async_insert = 0 for clients that already send batches.

How many rows per insert should I send? In this test the cost per row fell steeply up to 1,000 rows and was nearly flat after 10,000. The usual advice of 10,000 to 100,000 rows per insert, or at most about one insert per second per table, still holds.

Is wait_for_async_insert = 0 safe? It is fast (4,551 rows/s from 32 clients here), but the server acknowledges the insert before writing it, so failed flushes are invisible to the client. Use it only for data you can afford to lose.

Reproduce it

Start a throwaway server with docker run -d --rm --name ch-research -e CLICKHOUSE_PASSWORD=lab clickhouse/clickhouse-server:26.10, create the table, then send rows over HTTP in a loop with the settings as URL parameters (for example &async_insert=0 after the query parameter). After each run:

SYSTEM FLUSH LOGS;
SELECT countIf(event_type = 'NewPart') AS parts_created,
       countIf(event_type = 'MergeParts') AS merges,
       sumIf(size_in_bytes, event_type = 'MergeParts') AS merge_bytes_written
FROM system.part_log WHERE table = 't';

-- the adaptive timer at work (trace level is on in the Docker image)
SELECT event_time_microseconds, message FROM system.text_log
WHERE logger_name = 'AsynchronousInsertQueue' AND message LIKE 'Asynchronous timeout%'
ORDER BY event_time_microseconds DESC LIMIT 20;

Tomorrow: a codec shootout on realistic time series, with Delta, DoubleDelta, Gorilla, T64 and ZSTD levels measured for size and read speed.