Schedule icon
Query icon
OutputValues icon
Set icon
Log icon
If icon
SlackIncomingWebhook icon
Fail icon

ClickHouse Keeper & ZooKeeper Replicated DDL Queue Stagnation Guard

Guard ClickHouse Keeper and ZooKeeper DDL queues by detecting stagnant queries, alerting on lagging replicas, and gating migrations with Kestra.

Categories
DataInfrastructure

ClickHouse relies on ClickHouse Keeper or Apache ZooKeeper to coordinate distributed DDL queries (ALTER TABLE ... ON CLUSTER, CREATE TABLE ... ON CLUSTER, DROP TABLE ... ON CLUSTER, etc.) across cluster replicas via the /clickhouse/task_queue/ddl coordination queue. Distributed DDL queries execute sequentially in strict FIFO order per cluster.

When a cluster node experiences network partitioning, hardware saturation, unhandled schema exceptions, or long-running conflicting mutations (ALTER TABLE ... UPDATE / DELETE), the affected replica stalls. Because the distributed DDL queue is strictly sequential, a single lagging or failed replica prevents all subsequent distributed DDL operations from executing across the entire cluster. Upstream database migration scripts and CI/CD deployment pipelines then hang indefinitely until the coordination queue is cleared.

This blueprint provides an automated watchdog guard that inspects system.distributed_ddl_queue, cross-references active table mutations in system.mutations and locking queries in system.processes, stores cluster queue health in Kestra's Key-Value store, alerts SRE and database teams via Slack with granular diagnostics, and optionally fails execution to safely gate upstream migration workflows.

How it works

  1. DDL Queue Inspection: check_ddl_queue executes a native JDBC query via io.kestra.plugin.jdbc.clickhouse.Query against system.distributed_ddl_queue (which reflects the underlying Keeper/ZooKeeper path /clickhouse/task_queue/ddl). It calculates the elapsed age of pending tasks using ClickHouse's dateDiff('minute', query_create_time, now()), isolates tasks where status != 'Finished' older than stale_ddl_age_minutes, and identifies lagging replica hostnames and exception messages.
  2. Conflicting Mutation Audit: check_conflicting_mutations queries system.mutations for uncompleted mutations (is_done = 0) that either have a non-empty latest_fail_reason or exceed stale_ddl_age_minutes. In ClickHouse ReplicatedMergeTree engines, active mutations acquire exclusive part locks that block concurrent DDL modifications.
  3. Blocking Process Detection: check_blocking_queries checks system.processes on the coordinator node for long-running statements (such as heavy OPTIMIZE TABLE or bulk operations) that may be holding table locks.
  4. Telemetry Aggregation: summarize_findings (io.kestra.plugin.core.output.OutputValues) computes overall health metrics, compiles comma-separated lists of stagnant queue entries, initiating users, lagging cluster replicas, and sets the cluster status (HEALTHY vs STAGNANT).
  5. KV Store Health Persistence: record_ddl_queue_status_kv (io.kestra.plugin.core.kv.Set) stores the structured queue health payload into Kestra's Key-Value store under the key clickhouse_ddl_queue_status_{cluster}. Downstream deployment pipelines can query this key before applying schema migrations.
  6. Log Output: log_status prints an operational breakdown of queue health, stagnant task IDs, initiating users, and lagging replica nodes.
  7. Slack Incident Escalation: If is_stagnant is true, alert_stagnant_ddl dispatches a rich Slack webhook alert containing the blocked query text, initiating user, stalled cluster nodes, and remediation guidance.
  8. Deployment Gating: When fail_on_stagnant is enabled, fail_on_stagnant_queue (io.kestra.plugin.core.execution.Fail) fails the execution, preventing automated migration runners or CI/CD pipelines from piling more DDL queries onto a wedged queue.

What you get

  • Native JDBC ClickHouse integration without external agents or python dependencies.
  • Early detection of wedged ON CLUSTER DDL operations before deployment pipelines fail.
  • Identification of the specific lagging replica nodes and initiating user causing the blockage.
  • Cross-table correlation between DDL queue stagnation and stuck background table mutations.
  • Built-in KV store snapshot for external pipeline validation and status checks.
  • Actionable Slack notifications with error details and recommended remediation steps.

Who it's for

  • ClickHouse Database Administrators (DBAs) managing replicated clusters.
  • Site Reliability Engineers (SREs) responsible for analytics cluster uptime and migration pipelines.
  • Data Platform Engineers running automated continuous integration schema migrations (Flyway, Liquibase, or custom migration scripts).

Why orchestrate this with Kestra

ClickHouse Keeper and ZooKeeper do not natively monitor queue dwell time or trigger incident alerts when a replica drops out of the DDL queue. Managing this with ad-hoc cron scripts creates brittle monitoring silos without audit logs or credential management.

Kestra centralizes ClickHouse cluster operations by providing:

  • Secret management for database credentials and notification webhooks.
  • Scheduled background sweeps with configurable operational thresholds.
  • Key-Value store persistence to decouple monitoring from deployment gates.
  • Flow-level error trapping and instant alert dispatch upon connectivity failures.

Prerequisites

  • A running ClickHouse cluster (v21.8 or higher) with ClickHouse Keeper or Apache ZooKeeper configured for distributed DDL.
  • A database user with SELECT privileges on system.distributed_ddl_queue, system.mutations, and system.processes.
  • A Slack Incoming Webhook URL for alert routing.

Secrets

  • CLICKHOUSE_USERNAME: ClickHouse database user (required when use_secret_auth is true).
  • CLICKHOUSE_PASSWORD: ClickHouse database password (required when use_secret_auth is true).
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook URL for incident notifications.

Quick start

  1. Configure namespace secrets CLICKHOUSE_USERNAME, CLICKHOUSE_PASSWORD, and SLACK_WEBHOOK_URL.
  2. Deploy this flow to your Kestra instance.
  3. Run a manual execution with cluster set to your ClickHouse cluster name (e.g., default or production) and fail_on_stagnant: false to verify connectivity.
  4. Enable the periodic_ddl_queue_check schedule trigger to run automated sweeps every 15 minutes.
  5. In your upstream schema deployment workflow, check the KV store key clickhouse_ddl_queue_status_{cluster} prior to running migrations.

How to extend

  • Automated Replica Restart: Add an approval gate and SYSTEM RESTART REPLICA command for replicas that have lost their Keeper session.
  • Deadlock Query Termination: Combine with KILL QUERY to terminate deadlocked queries that have blocked DDL operations past a critical SLA threshold.
  • Multi-Cluster Federation: Parameterize the flow with a list of clusters or wrap it in a parent flow using io.kestra.plugin.core.flow.Loop to audit all regional clusters in parallel.
  • PagerDuty Escalation: Replace or augment the Slack notification task with io.kestra.plugin.pagerduty.Incident for high-severity out-of-hours on-call paging.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.