At a national electronic-payments company, real-time change-data-capture (CDC) from four source systems was handled by a fleet of Python/Dagster consumers. They worked, but they were expensive and fragile: six consumer containers burned a combined ~463% CPU around the clock, bloated the orchestrator's Postgres log database by 20–30 GB every two days, and occasionally missed late-arriving records — in one May window, 74 transactions committed at 23:59 landed after the midnight report had already run.
I designed, benchmarked, and rolled out a replacement that removes Python from the hot path entirely.
The design
Source → Debezium → Kafka/Redpanda → ClickHouse KafkaEngine raw table → a fan-out of five Materialized Views (main upsert · delete-staging · batch-log · dead-letter · type-error) → target ReplacingMergeTree tables. Ingestion and transformation now run inside ClickHouse, with no external worker to schedule, monitor, or pay for.
Two source families needed different handling: MySQL (with epoch-millisecond timestamp conversion) and two MongoDB databases (whose Debezium delete tombstones carry the key in different shapes, resolved per-connector). MongoDB deletes — which KafkaEngine can't natively reconcile — are swept by a lightweight 5-minute reconciliation job against a pending-deletes table.
Proving it
- Resource: under real Debezium traffic, CDC CPU fell from 463% → ~18% (~96% reduction), with idle overhead only +0.4pp over baseline.
- Correctness: a 4-phase validation across 14 tables landed 820K+ rows with zero errors; a stress burst pushed 120K rows in 2m6s, peaking at 2.41 GiB (32% of the node limit).
- Throughput envelope (benchmark campaign, 3-node ReplicatedMergeTree cluster): scaled cleanly to 8,000 rows/sec (peak 1.5M inserts/min), roughly linear at ~26–37 CPU% per 1k rows/sec; the Redpanda broker never exceeded 1–2% CPU. I also quantified virtualization overhead — 244% CPU in VirtualBox vs 91% bare-metal at the same load — to inform the deployment target.
- Endurance: a 117-minute mixed-workload run (3 MongoDB + 1 MySQL) landed 3,097,441 rows with 0 CDC errors; every topic lag (MySQL peak 7,718 messages) drained back to zero by completion.
Reliability & operations
Beyond the migration I hardened the surrounding pipeline: a Debezium HA cutover routing the connector through HAProxy plus EDB pg_failover_slots across a 3-node Patroni cluster (so a Postgres leader switchover can't silently orphan the replication slot and lose WAL), freshness/staleness alerting to Teams, and an incident postmortem + backfill after a MongoDB CDC freeze. The design went to production across 30+ tables spanning MySQL, two MongoDB databases, and PostgreSQL.
Stack: ClickHouse (KafkaEngine, Materialized Views, ReplacingMergeTree) · Kafka / Redpanda · Debezium · MongoDB · MySQL · PostgreSQL (Patroni, pg_failover_slots) · HAProxy · Dagster.