xiaohongshu
xiaohongshu
clickhouse-cdc
Use when syncing data FROM relational databases (PostgreSQL, MySQL, MongoDB) TO ClickHouse. Covers change data capture using Debezium, Airbyte, or custom triggers. Includes handling schema evolution, DELETE operations, and maintaining consistency. NOT for message queues (see clickhouse-streaming)...
Full skill instructions
Change Data Capture (CDC) replicates database changes to ClickHouse for analytics. Core principle: Capture changes at source, transform for column-storage, handle deletes gracefully.
Key challenge: ClickHouse isn't designed for updates/deletes. Use ReplacingMergeTree or append-only patterns.
Symptoms:
When NOT to use:
clickhouse-streamingclickhouse-patternsclickhouse-patternsdigraph cdc_methods {
rankdir=TD;
node [shape=box, style=rounded];
start [label="Choose CDC Method", shape=ellipse];
volume [label="High volume?\n(>10k rows/sec)", shape=diamond];
complexity [label="Complex schema?\n(many tables)", shape=diamond];
debezium [label="Debezium\n(production-grade)"];
airbyte [label="Airbyte\n(managed service)"];
custom [label="Custom Triggers\n(simple setup)"];
start -> volume;
volume -> debezium [label="yes"];
volume -> complexity [label="no"];
complexity -> airbyte [label="yes"];
complexity -> custom [label="no"];
}
| Method | Best For | Pros | Cons |
|---|---|---|---|
| Debezium | High volume, production | Log-based, no DB impact | Complex setup |
| Airbyte | Multi-source, managed | UI, pre-built connectors | Cost, less control |
| Custom Triggers | Simple, low volume | Easy to understand | DB overhead |
| Pattern | Use Case | Key Technique |
|---|---|---|
| Soft Delete | Handle DELETEs | Add is_deleted flag |
| ReplacingMergeTree | Handle UPDATEs | Use version/timestamp column |
| Schema Evolution | Add columns | Nullable with defaults |
| Backfill | Initial sync | Snapshot then CDC |
PostgreSQL (WAL) → Debezium → Kafka → ClickHouse Kafka Engine
-- 1. Kafka staging table
CREATE TABLE users_kafka (
id UInt64,
name String,
email String,
updated_at DateTime,
_operation String -- INSERT, UPDATE, DELETE
) ENGINE = Kafka()
SETTINGS
kafka_broker_list = 'kafka:9092',
kafka_topic_list = 'dbserver1.public.users',
kafka_group_name = 'clickhouse_consumer',
kafka_format = 'JSONEachRow';
-- 2. Target table with dedup
CREATE TABLE users (
id UInt64,
name String,
email String,
updated_at DateTime,
is_deleted UInt8 DEFAULT 0
) ENGINE = ReplacingMergeTree(updated_at)
ORDER BY id;
-- 3. Transform with materialized view
CREATE MATERIALIZED VIEW users_mv TO users AS
SELECT
id, name, email, updated_at,
if(_operation = 'DELETE', 1, 0) AS is_deleted
FROM users_kafka;
-- 4. Query active records
SELECT * FROM users FINAL WHERE is_deleted = 0;
{
"name": "postgres-connector",
"config": {
"connector.class": "io.debezium.connector.postgresql.PostgresConnector",
"database.hostname": "postgres",
"database.port": "5432",
"database.user": "replicator",
"database.dbname": "mydb",
"database.server.name": "dbserver1",
"table.include.list": "public.users,public.orders",
"plugin.name": "pgoutput"
}
}
CREATE OR REPLACE FUNCTION notify_changes()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
PERFORM pg_notify('table_changes', json_build_object(
'table', TG_TABLE_NAME,
'operation', TG_OP,
'data', row_to_json(NEW)
)::text);
RETURN NEW;
ELSIF TG_OP = 'DELETE' THEN
PERFORM pg_notify('table_changes', json_build_object(
'table', TG_TABLE_NAME,
'operation', 'DELETE',
'id', OLD.id
)::text);
RETURN OLD;
END IF;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER users_notify_trigger
AFTER INSERT OR UPDATE OR DELETE ON users
FOR EACH ROW EXECUTE FUNCTION notify_changes();
import { Client } from 'pg';
import { ClickHouse } from 'clickhouse';
const pgClient = new Client({ connectionString: process.env.PG_URL });
const clickhouse = new ClickHouse({ url: process.env.CH_URL });
async function startCDC() {
await pgClient.connect();
await pgClient.query('LISTEN table_changes');
pgClient.on('notification', async (msg) => {
const { table, operation, data, id } = JSON.parse(msg.payload);
if (operation === 'INSERT' || operation === 'UPDATE') {
await clickhouse.insert(table, [{ ...data, is_deleted: 0 }]);
} else if (operation === 'DELETE') {
await clickhouse.query(`
ALTER TABLE ${table} UPDATE is_deleted = 1 WHERE id = ${id}
`).toPromise();
}
});
}
{
"name": "mysql-connector",
"config": {
"connector.class": "io.debezium.connector.mysql.MySqlConnector",
"database.hostname": "mysql",
"database.user": "debezium",
"database.server.id": "184054",
"database.server.name": "mysql-server",
"table.include.list": "mydb.orders,mydb.customers"
}
}
Key differences: Uses binlog (not WAL), requires server.id, schema changes tracked separately.
CREATE TABLE orders (
id UInt64,
user_id UInt64,
amount Decimal(10, 2),
is_deleted UInt8 DEFAULT 0
) ENGINE = ReplacingMergeTree(updated_at)
ORDER BY id;
SELECT * FROM orders FINAL WHERE is_deleted = 0;
CREATE TABLE orders (
id UInt64,
amount Decimal(10, 2),
sign Int8 -- 1 = active, -1 = deleted
) ENGINE = CollapsingMergeTree(sign)
ORDER BY id;
-- Insert
INSERT INTO orders VALUES (1, 50.00, 1);
-- Delete (insert negative)
INSERT INTO orders VALUES (1, 50.00, -1);
-- Query
SELECT * FROM orders FINAL WHERE sign = 1;
ALTER TABLE users ADD COLUMN phone String DEFAULT '';
ALTER TABLE users ADD COLUMN country LowCardinality(String) DEFAULT 'UNKNOWN';
const schemaRegistry = {
'users': { version: 2, columns: ['id', 'name', 'email', 'phone'] }
};
function transformRow(table: string, row: any) {
return {
...row,
phone: row.phone || '',
country: row.country || 'UNKNOWN'
};
}
// 1. Snapshot existing data
async function backfillTable(tableName: string) {
const stream = pgClient.query(`COPY (SELECT * FROM ${tableName}) TO STDOUT`);
await pipeline(stream, csvParser(), clickhouse.insert(tableName).stream());
}
// 2. Start CDC from snapshot position
const lsn = await pgClient.query(`SELECT pg_current_wal_lsn()`);
| Mistake | Why It Fails | Fix |
|---|---|---|
| No deduplication | Duplicate events | Use ReplacingMergeTree |
| Hard deletes | Not supported well | Soft delete with flag |
| Ignoring order | Out-of-order updates | Use version/timestamp |
| No backfill | Missing historical data | Snapshot before CDC |
-- Batch Kafka consumption
SETTINGS kafka_max_block_size = 65536, kafka_poll_timeout_ms = 1000;
-- Async inserts
SET async_insert = 1, wait_for_async_insert = 0;
-- Replication lag
SELECT table, max(synced_at) AS last_sync,
now() - max(synced_at) AS lag_seconds
FROM system.parts WHERE active GROUP BY table;
-- Kafka consumer errors
SELECT database, table, exceptions, last_exception_time
FROM system.kafka_consumers;
Design:
Operations:
Performance:
Resources: Debezium docs (https://debezium.io), ClickHouse Kafka engine docs
clickhouse-patternsclickhouse-streamingRemember: CDC is about consistency. Always handle deletes, monitor lag, and test schema changes thoroughly.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience