| name | clickhouse-reference-architecture |
| description | Production reference architecture for ClickHouse-backed applications โ
project layout, data flow, multi-tenant patterns, and operational topology.
Use when designing a new ClickHouse system, reviewing an existing analytics
architecture, or establishing standards for ClickHouse integrations.
Trigger with "clickhouse architecture", "clickhouse project structure",
"clickhouse design", "clickhouse multi-tenant", "clickhouse reference".
|
| allowed-tools | Read, Grep |
| version | 1.7.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","database","analytics","clickhouse","olap"] |
| compatibility | Designed for Claude Code |
ClickHouse Reference Architecture
Overview
Production-grade architecture for ClickHouse analytics platforms covering project
layout, data flow, multi-tenancy, and operational patterns. Work through the five
steps below to get the high-level shape, then drill into the linked reference files
for the full DDL, client code, and tenancy trade-offs.
Prerequisites
- Understanding of ClickHouse fundamentals โ table engines,
ORDER BY sort keys,
and partitioning.
- A TypeScript/Node.js project (the client examples use
@clickhouse/client).
- When reviewing an existing codebase,
Grep for createClient( to locate the
current client module and Read the SQL files under clickhouse/schemas/.
Instructions
Step 1: Project Structure
Keep SQL DDL as the source of truth under clickhouse/schemas/, named query
functions under clickhouse/queries/, and ingestion/API/jobs in sibling modules.
my-analytics-platform/
โโโ src/
โ โโโ clickhouse/
โ โ โโโ client.ts # Singleton client with health checks
โ โ โโโ schemas/ # SQL DDL files (source of truth)
โ โ โ โโโ 001-events.sql
โ โ โ โโโ 002-users.sql
โ โ โ โโโ 003-materialized-views.sql
โ โ โโโ queries/ # Named query functions
โ โ โโโ migrations/ # Schema migrations (runner.ts + *.sql)
โ โโโ ingestion/ # webhook-receiver, kafka-consumer, buffer
โ โโโ api/ # routes.ts, middleware.ts (auth, rate limit)
โ โโโ jobs/ # daily-rollup.ts, cleanup.ts (TTL enforcement)
โโโ tests/ # unit/ + integration/
โโโ docker-compose.yml # Local ClickHouse
โโโ init-db/ # Docker init scripts
โโโ config/ # development / staging / production .env
Step 2: Data Flow Architecture
Data moves in one direction: sources โ a batching ingestion layer โ ClickHouse
(raw MergeTree โ materialized views โ aggregate tables) โ an API that reads only
the aggregate tables โ dashboards.
Data Sources (Webhooks, API, Kafka, S3)
โ
Ingestion Layer (Buffer + batch, 10K+ rows/insert)
โ
ClickHouse Server
Raw Event Tables (MergeTree, append-only)
โ auto-aggregate on INSERT
Materialized Views (hourly, daily, tenant-level)
โ
Aggregate Tables (AggregatingMergeTree)
โ
API Layer (queries aggregate tables, never raw events)
โ
Dashboards / Client Apps
Step 3: Schema Design (3-Layer Pattern)
Three layers โ raw append-only events, hourly aggregation, and a daily rollup for
dashboards โ with materialized views auto-populating each aggregate on INSERT.
The essential raw-table skeleton: