Asynchronous Data Inserts in ClickHouse

Tom Schreiber and Tony Bonuccelli
22 min readbeginner
--
View Original

Overview

This article discusses asynchronous data inserts in ClickHouse, highlighting their advantages over traditional synchronous inserts. It explains how asynchronous inserts can improve data ingestion performance by shifting the batching process from the client side to the server side, making it suitable for scenarios where client-side batching is not feasible.

What You'll Learn

1

How to implement asynchronous data inserts in ClickHouse

2

Why batching data is crucial for optimal performance in ClickHouse

3

When to use fire-and-forget mode for data inserts

Prerequisites & Requirements

  • Basic understanding of ClickHouse and data ingestion concepts

Key Questions Answered

What are the benefits of asynchronous inserts in ClickHouse?
Asynchronous inserts in ClickHouse allow for improved data ingestion performance by buffering data on the server side, which reduces the overhead associated with frequent small inserts. This method is particularly useful in scenarios where client-side batching is not feasible, enabling high throughput without overwhelming the database with too many active parts.
How does the fire-and-forget mode work in ClickHouse?
In fire-and-forget mode, when an insert query is received, the data is immediately written into an in-memory buffer, and the client receives an acknowledgment right away. However, this mode does not guarantee durability, as data may not be written to storage if an error occurs during the buffer flush.
What is the impact of client-side batching on ClickHouse performance?
Client-side batching is essential for optimal performance in ClickHouse, as it minimizes the creation of small parts that can lead to increased CPU and I/O usage. The article emphasizes that sending fewer but larger inserts can significantly enhance ingestion performance.
What are the risks associated with using asynchronous inserts?
Using asynchronous inserts can lead to potential data loss if the node crashes before the buffer is flushed. Additionally, errors during the buffer flush may not be communicated back to the client, making it challenging to identify failed data sets.

Key Statistics & Figures

Maximum active parts before error
300
ClickHouse returns a 'Too many parts' error when there are more than 300 active parts in a single partition.
Rows per part during inserts
1 million
By default, a single new part can contain up to approximately 1 million rows.
Logs ingested by Uber
millions of logs per second
Uber uses ClickHouse to ingest millions of logs per second, showcasing its high throughput capabilities.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Key Actionable Insights

1
Implement asynchronous inserts to enhance data ingestion performance in ClickHouse.
Asynchronous inserts allow for better resource management and can handle high throughput scenarios effectively, especially when client-side batching is not practical.
2
Utilize the fire-and-forget mode for non-critical data where speed is prioritized over durability.
This mode can significantly improve throughput but should only be used when the risk of data loss is acceptable.
3
Monitor the number of active parts in your ClickHouse tables to avoid performance degradation.
Keeping track of active parts helps prevent the 'Too many parts' error, which can hinder ingestion performance and resource utilization.

Common Pitfalls

1
Creating too many small inserts can degrade performance.
Frequent small inserts lead to increased overhead in file creation and merging, which can exhaust cluster resources and slow down ingestion.
2
Not monitoring active parts can lead to errors.
Failing to keep track of the number of active parts can result in hitting the maximum threshold, causing insert errors and performance issues.

Related Concepts

Data Ingestion Strategies
Batch Processing Vs. Stream Processing
Performance Optimization In Databases