This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: clickhouse-adaptive-async-insert-batching-invariant

ClickHouse Adaptive Asynchronous Insert Batching Invariant

Enforces server-side asynchronous batching (`async_insert=1`, `wait_for_async_insert=1`) and client-side micro-batch queues for high-throughput ingestion into ClickHouse MergeTree tables. Prevents catastrophic part-count explosion ('Too many parts in all data parts in table' exception) and zookeeper metadata exhaustion caused by synchronous micro-commits. Mandates bounded flush windows, idempotency deduplication tokens, and explicit backpressure telemetry.

Prohibited Anti-Pattern: High-Frequency Synchronous Micro-Inserts Triggering 'Too Many Parts' MergeTree Cluster Degradation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: ClickHouse Adaptive Asynchronous Insert Batching Invariant - Enforces server-side asynchronous batching (`async_insert=1`, `wait_for_async_insert=1`) and client-side micro-batch queues for high-throughput ingestion into ClickHouse MergeTree tables. Prevents catastrophic part-count explosion ('Too many parts in all data parts in table' exception) and zookeeper metadata exhaustion caused by synchronous micro-commits. Mandates bounded flush windows, idempotency deduplication tokens, and explicit backpressure telemetry.
globs: src/**/repositories/**/*.ts, src/**/services/**/*.ts, src/**/clickhouse/**/*.ts, src/**/ingestion/**/*.ts, src/**/infrastructure/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/clickhouse-adaptive-async-insert-batching-invariant"
---

# ClickHouse Adaptive Asynchronous Insert Batching Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: High-Frequency Synchronous Micro-Inserts Triggering 'Too Many Parts' MergeTree Cluster Degradation

## System Constraints
You are a Principal Backend and Data Infrastructure Architect enforcing ClickHouse Adaptive Asynchronous Insert Batching Invariants. Never emit single-record or unbatched synchronous SQL insert calls into MergeTree tables. Mandate server-side async inserts or resilient client-side batching pipelines to eliminate part thrashing.

## Strict Guidelines
- Engineers MUST configure ClickHouse sessions or insert queries with `async_insert = 1` and `wait_for_async_insert = 1` (or `0` when fire-and-forget guarantees are acceptable with upstream dead-letter tracking) when issuing streaming inserts.
- Engineers MUST NOT issue raw `INSERT INTO table VALUES (...)` statements for single telemetry events directly in HTTP request lifecycles without an intermediate queue or asynchronous batching parameters enabled.
- Engineers MUST specify explicit thresholds for `async_insert_max_data_size` (e.g., 10-100 MiB) and `async_insert_busy_timeout_ms` (e.g., 200-1000ms) on client sessions or ClickHouse user profiles.
- Services MUST implement resilient client-side buffering using bounded memory queues with active backpressure rejection (e.g., HTTP 429 / Circuit Breaker) rather than unbound in-memory buffering when ClickHouse ingestion latency spikes.
- Engineers MUST define explicit idempotency keys or utilize `insert_deduplication_token` query settings for retried batch payloads to prevent duplicate record proliferation across distributed clusters.

## Bad vs Good Implementations
### Unbatched Synchronous Telemetry Ingestion vs. Parameterized Async Batching with Deduplication Token

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing single-row synchronous inserts generates an unmerged file part on disk for every incoming request. Under high concurrency, ClickHouse exhausts CPU running background merges, encounters part explosion errors, and throttles ingestion cluster-wide.
```typescript
import { createClient } from '@clickhouse/client';

const client = createClient({ url: process.env.CLICKHOUSE_URL! });

// ANTI-PATTERN: Firing immediate synchronous single-event inserts per request.
// Each insert creates an isolated data part on disk, triggering 'Too many parts' (code 252)
// and crashing background merge queues under normal ingestion spikes.
export async function ingestTelemetryBad(event: { tenantId: string; metric: string; value: number }) {
  await client.insert({
    table: 'system_metrics',
    values: [
      {
        tenant_id: event.tenantId,
        metric_name: event.metric,
        metric_value: event.value,
        timestamp: new Date().toISOString(),
      },
    ],
    format: 'JSONEachRow',
  });
}
```

#### Production Standard (Correct)
> **Standard**: Configures server-side `async_insert: 1` and `wait_for_async_insert: 1`, allowing ClickHouse to coalesce incoming writes into optimized 10 MiB or 500ms data parts while maintaining client backpressure. Introduces `insert_deduplication_token` to guarantee idempotent retries.
```typescript
import { createClient } from '@clickhouse/client';
import { createHash } from 'node:crypto';

const client = createClient({
  url: process.env.CLICKHOUSE_URL!,
  clickhouse_settings: {
    // Invariant: Delegate part-aggregation to ClickHouse server buffers
    async_insert: 1,
    wait_for_async_insert: 1, // Await until batch is flushed to disk for durability
    async_insert_busy_timeout_ms: 500,
    async_insert_max_data_size: 10485760, // 10 MiB limit before force flush
  },
});

export interface TelemetryPayload {
  tenant_id: string;
  metric_name: string;
  metric_value: number;
  timestamp: string;
}

// PRODUCTION FIX: Batch processing with async_insert server invariants
// and deduplication hashing to safeguard cluster file-part limits
export async function ingestTelemetryBatchGood(events: TelemetryPayload[]): Promise<void> {
  if (events.length === 0) return;

  // Generate deterministic token for ClickHouse ReplicatedMergeTree block deduplication
  const deduplicationToken = createHash('sha256')
    .update(`${events[0]?.tenant_id}:${events[0]?.timestamp}:${events.length}`)
    .digest('hex');

  await client.insert({
    table: 'system_metrics',
    values: events,
    format: 'JSONEachRow',
    clickhouse_settings: {
      insert_deduplication_token: deduplicationToken,
    },
  });
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: ClickHouse Adaptive Asynchronous Insert Batching Invariant

ClickHouse Adaptive Asynchronous Insert Batching Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces server-side asynchronous batching (`async_insert=1`, `wait_for_async_insert=1`) and client-side micro-batch queues for high-throughput ingestion into ClickHouse MergeTree tables. Prevents catastrophic part-count explosion ('Too many parts in all data parts in table' exception) and zookeeper metadata exhaustion caused by synchronous micro-commits. Mandates bounded flush windows, idempotency deduplication tokens, and explicit backpressure telemetry. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: High-Frequency Synchronous Micro-Inserts Triggering 'Too Many Parts' MergeTree Cluster Degradation Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftHigh-Frequency Synchronous Micro-Inserts Triggering 'Too Many Parts' MergeTree Cluster DegradationCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST configure ClickHouse sessions or insert queries with `async_insert = 1` and `wait_for_async_insert = 1` (or `0` when fire-and-forget guarantees are acceptable with upstream dead-letter tracking) when issuing streaming inserts.

Engineers MUST configure ClickHouse sessions or insert queries with `async_insert = 1` and `wait_for_async_insert = 1` (or `0` when fire-and-forget guarantees are acceptable with upstream dead-letter tracking) when issuing streaming inserts.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT issue raw `INSERT INTO table VALUES (...)` statements for single telemetry events directly in HTTP request lifecycles without an intermediate queue or asynchronous batching parameters enabled.

Engineers MUST NOT issue raw `INSERT INTO table VALUES (...)` statements for single telemetry events directly in HTTP request lifecycles without an intermediate queue or asynchronous batching parameters enabled.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST specify explicit thresholds for `async_insert_max_data_size` (e.g., 10-100 MiB) and `async_insert_busy_timeout_ms` (e.g., 200-1000ms) on client sessions or ClickHouse user profiles.

Engineers MUST specify explicit thresholds for `async_insert_max_data_size` (e.g., 10-100 MiB) and `async_insert_busy_timeout_ms` (e.g., 200-1000ms) on client sessions or ClickHouse user profiles.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST implement resilient client-side buffering using bounded memory queues with active backpressure rejection (e.g., HTTP 429 / Circuit Breaker) rather than unbound in-memory buffering when ClickHouse ingestion latency spikes.

Services MUST implement resilient client-side buffering using bounded memory queues with active backpressure rejection (e.g., HTTP 429 / Circuit Breaker) rather than unbound in-memory buffering when ClickHouse ingestion latency spikes.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Engineers MUST define explicit idempotency keys or utilize `insert_deduplication_token` query settings for retried batch payloads to prevent duplicate record proliferation across distributed clusters.

Engineers MUST define explicit idempotency keys or utilize `insert_deduplication_token` query settings for retried batch payloads to prevent duplicate record proliferation across distributed clusters.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is ClickHouse Adaptive Asynchronous Insert Batching Invariant critical for AI coding assistants?

Enforces server-side asynchronous batching (`async_insert=1`, `wait_for_async_insert=1`) and client-side micro-batch queues for high-throughput ingestion into ClickHouse MergeTree tables. Prevents catastrophic part-count explosion ('Too many parts in all data parts in table' exception) and zookeeper metadata exhaustion caused by synchronous micro-commits. Mandates bounded flush windows, idempotency deduplication tokens, and explicit backpressure telemetry. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (High-Frequency Synchronous Micro-Inserts Triggering 'Too Many Parts' MergeTree Cluster Degradation) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

ClickHouse