Learn Labs
Ingestion & Other Engines

Async Inserts & Batching

Why tiny inserts hurt, how to fix it

Parts & Background Merges already covers why small, frequent inserts are dangerous — each one creates a new part, and enough of them outrun the background merge process. Batching client-side is the ideal fix. This page is for when you don't control the client: many independent application servers, each with a handful of rows to insert, with no good place to accumulate a batch before sending it.

Option one: the Buffer engine

A Buffer table sits in front of a real MergeTree table. Inserts land in memory, and the buffer flushes into the underlying table once row-count, byte-size, or time thresholds are crossed — turning many tiny inserts into far fewer, larger ones before they ever become a part.

CREATE TABLE events_buffer AS events
ENGINE = Buffer(
  currentDatabase(), events,   -- target database, target table
  16,                          -- number of buffer layers
  10, 100,                     -- min/max seconds before flush
  10000, 1000000,              -- min/max rows before flush
  10000000, 100000000          -- min/max bytes before flush
);

-- application inserts here, not into 'events' directly
INSERT INTO events_buffer VALUES (...);

It's the older mechanism, still used, but with a real cost: the buffer lives in server memory and is lost on restart or crash — anything not yet flushed is gone.

Option two: async_insert (the modern default choice)

Rather than a separate table, async_insert is a setting that changes how INSERT itself behaves: the server accepts the (possibly tiny) insert immediately, holds it in an internal buffer alongside inserts from other connections, and flushes the accumulated buffer as one real part once a size or time threshold is hit.

Many clientssmall inserts each
Server-side bufferasync_insert=1
One new partwritten to disk
Threshold hitsize or time
INSERT INTO events
SETTINGS async_insert = 1, wait_for_async_insert = 1
VALUES (102, 'click', now());

wait_for_async_insert is the trade you're actually making explicit:

  • = 1 (default) — the client's INSERT blocks until the buffered data is actually flushed to disk. Safer, but the client now waits for the flush timer too: 59 ms per insert in the lab below, against 0.9 ms for a plain synchronous insert.
  • = 0 — the server acknowledges the insert as soon as it's in the in-memory buffer, before it's durable. Much lower client-perceived latency, at the cost of a real window where an acknowledged insert can be lost if the server crashes before flushing.

Measured: async_insert is the default since 26.2

ClickHouse 26.2 turned async_insert on by default (SELECT * FROM system.settings_changes lists it: async_insert 0 -> 1 in version 26.2). The labs found out the hard way: a lab meant to show that 1,000 single-row inserts make 1,000 parts reported each insert taking 59 ms, and a trace log showed AsynchronousInsertQueue on every one. clickhouse/labs/parts_merges.py now measures all three modes:

time to write 1,000 rows1,000 single-row INSERTs, sync0.9 s, 1,000 parts1,000 single-row INSERTs, 26.x default59.4 s, 1,000 parts20 clients x 50, async_insert9.5 s, 69 partsone INSERT of 1,000 rows0.004 s, 1 part
  • One client, one row at a time, the new default: the worst of both. Every insert waits about 50 to 60 ms for the adaptive flush timer (async_insert_busy_timeout_min_ms = 50), then flushes alone, because a sequential client never has a second insert in the buffer. It still made 1,000 parts, and took 66 times longer than synchronous inserts.
  • 20 concurrent clients is what async_insert is for: their 1,000 rows landed in 69 parts instead of 1,000.
  • One batch is still the best by far: one part, 4 ms.

It also changed what errors look like. With Keeper's majority gone, the cluster labs' inserts failed with Wait for async insert timeout instead of the real cause, Table is in readonly mode, which only appeared once the labs turned async inserts off.

The labs set async_insert = 0 for batch writers, and so should an application that already batches. Leave it on for many small, independent writers, and make those inserts idempotent.

On this page