Query icon
IonToCsv icon
If icon
Log icon
SlackIncomingWebhook icon
Pause icon
Set icon
Schedule icon

ClickHouse Asynchronous Insert Buffer Queue and Backpressure Sentinel

Monitor ClickHouse async insert buffer memory and flush lag using system.asynchronous_inserts, gate operator queue flushes via Pause, and alert on ingestion backpressure.

Categories
Data

ClickHouse's asynchronous insert engine (async_insert = 1) optimizes high-frequency ingestion by grouping small, real-time client inserts into in-memory buffers before writing consolidated columnar data parts to disk. However, when upstream ingestion rates surge or storage I/O throttles, unwritten data accumulates in RAM. Once memory limits or timeout boundaries (wait_for_async_insert_timeout) are exceeded, client queries fail with timeout exceptions, creating cascading data ingestion backpressure across message queues like Kafka and RabbitMQ. This workflow continuously queries ClickHouse's system.asynchronous_inserts catalog table, monitors memory saturation and flush latencies against configurable SLAs, exports a downloadable CSV diagnostic report, provides an operator-in-the-loop approval gate to flush saturated queues to disk, verifies queue draining, persists telemetry history in Kestra's Key-Value store, and dispatches incident alerts to Slack when ingestion congestion is detected.

How it works

  1. The Schedule trigger initiates the workflow every 15 minutes, or engineers can trigger executions on-demand from the UI.
  2. evaluate_buffer_saturation aggregates active buffer metrics from system.asynchronous_inserts, computing total buffered megabytes, pending batch counts, and the age in milliseconds of the oldest unwritten batch.
  3. fetch_saturated_tables_telemetry streams detailed table-level buffer metrics (database, table, pending rows, memory, format, and open batch age) into internal storage (STORE fetch type).
  4. export_diagnostic_csv transforms raw buffer telemetry into a downloadable CSV artifact using IonToCsv.
  5. check_backpressure_and_alert (If condition) verifies whether buffered memory or flush age exceeds configured SLA boundaries.
  6. If backpressure is detected, notify_slack_alert posts an alert with buffer saturation metrics and the direct CSV report link for fast triage.
  7. When auto_flush_queue is enabled, execute_queue_remediation pauses execution (io.kestra.plugin.core.flow.Pause, pauseDuration: PT10M, behavior: CANCEL) for operator review before executing SYSTEM FLUSH ASYNC INSERT QUEUE; to safely force unwritten data to disk, canceling automatically on timeout to prevent unapproved flushes, followed by verify_queue_drained confirming the remaining batch count and memory dropped to healthy levels.
  8. update_telemetry_watermark updates the clickhouse_async_insert_audit_<database> key in Kestra's KV store for historical capacity trending.
  9. The errors block intercepts connection or query failures and immediately alerts the on-call engineering channel.

What you get

  • Early detection of ClickHouse ingestion bottlenecks before client applications experience timeout failures.
  • Real-time visibility into unwritten in-memory data accumulation across all MergeTree tables.
  • Automated export of congested tables and pending batch ages into downloadable CSV artifacts.
  • Operator-in-the-loop remediation gate pausing for confirmation before flushing the asynchronous insert queue.
  • Post-remediation verification ensuring unwritten buffers drained completely.
  • Persistent state tracking via Kestra KV store without third-party monitoring infrastructure.
  • Actionable incident alerts delivered directly to Slack with report links.

Who it's for

  • Data Platform Engineers operating high-throughput ClickHouse real-time ingestion pipelines.
  • Site Reliability Engineers (SREs) monitoring analytical database SLAs and memory limits.
  • Backend Engineers seeking visibility into asynchronous write buffer latencies.

Why orchestrate this with Kestra

Monitoring asynchronous insert buffers directly through declarative Kestra workflows eliminates the need for heavyweight proprietary APM agents. Kestra natively handles database retries, credential isolation via secrets, conditional branching, human approval gates (io.kestra.plugin.core.flow.Pause), artifact generation, and key-value state persistence in a single audited YAML pipeline.

Prerequisites

  • ClickHouse 21.11 or newer with asynchronous inserts enabled (async_insert = 1).
  • Database user with SELECT permissions on system.asynchronous_inserts and SYSTEM FLUSH ASYNC INSERT QUEUE privileges.
  • Incoming Slack Webhook URL for alerting notifications.

Secrets

  • CLICKHOUSE_JDBC_URL: JDBC connection string (e.g., jdbc:clickhouse://clickhouse.company.internal:8123/default).
  • CLICKHOUSE_USERNAME: Database username with read and flush permissions.
  • CLICKHOUSE_PASSWORD: Database user password.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint for alerting.

Quick start

  1. Configure the CLICKHOUSE_JDBC_URL, CLICKHOUSE_USERNAME, CLICKHOUSE_PASSWORD, and SLACK_WEBHOOK_URL secrets in your Kestra namespace.
  2. Import this blueprint into your Kestra workspace.
  3. Click Execute with default inputs to perform an initial audit.
  4. Verify the generated output logs, download the generated CSV report from the Outputs tab, and inspect the stored record in the KV Store tab.
  5. Enable the 15-minute schedule trigger for continuous monitoring.

How to extend

  • Configure automated writer throttling or backpressure webhooks to upstream microservices when buffer saturation persists post-flush.
  • Archive periodic buffer congestion reports to Amazon S3 or Google Cloud Storage using io.kestra.plugin.aws.s3.Upload.
  • Integrate with PagerDuty or Opsgenie using Kestra's notification plugins for high-severity memory spikes.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.