HTAP-Lite Bridge Design: OLTP → OLAP
As Sherlock, I'll design a lightweight HTAP-lite bridge that bridges an OLTP workload (e.g., PostgreSQL for high-throughput ACID transactions) to an OLAP system (e.g., ClickHouse or Snowflake for analytics). The goal is near-real-time analytics without full HTAP (like SingleStore or TiDB), minimizing complexity, cost, and dual-write pitfalls. Key principles:
- Single source of truth: OLTP is the canonical store; OLAP is derived.
- Decoupling: Async change propagation for scalability.
- Observability: Metrics for freshness SLAs.
- Fault tolerance: Idempotent processing, at-least-once delivery.
1. High-Level Architecture
[OLTP: PostgreSQL]
↓ WAL-based CDC (Debezium)
[Change Stream: Kafka + Schema Registry]
↓ Consumer + Transformer (e.g., Kafka Streams / ksqlDB)
[OLAP: ClickHouse / Snowflake]
↓ Materialized Views / Ingestion Engine
-
Components:
| Component | Tech | Role |
|---|
| OLTP | PostgreSQL 15+ | Transactional writes. |
| CDC | Debezium (embedded or Kafka Connect) | Captures INSERT/UPDATE/DELETE from WAL. |
| Stream | Apache Kafka (3 partitions/topic for scale) | Durable queue for changes. |
| Transformer | Kafka Streams / Flink | Upserts, aggregations, schema mapping. |
| OLAP | ClickHouse (MergeTree) | Fast analytics with TTLs. |
| Monitoring | Prometheus + Grafana | Lag, throughput, freshness SLAs. |
-
Data Flow:
- OLTP txns → WAL logs.
- Debezium tails WAL → Kafka topics (one per table, e.g.,
db.public.users+0).
- Consumer transforms (e.g., compact to upsert key-value) → OLAP sink.
- OLAP uses ReplacingMergeTree for dedup/upserts.
2. Change Capture
Primary: Log-based CDC (Debezium on WAL)
-
Why WAL? Zero-overhead (no triggers), captures all DML/DDL, schema-aware.
-
Config:
# Debezium Connector
connector.class: io.debezium.connector.postgresql.PostgresConnector
database.hostname: olap-oltp
slot.name: debezium_slot # Replication slot for reliability
publication.name: dbz_publication
snapshot.mode: initial # One-time bootstrap
transforms: unwrap # Flatten Debezium envelope to key-value
-
Envelope: { "before": {...}, "after": {...}, "op": "c/u/d" }.
-
Bootstrap: Initial snapshot for catch-up.
-
Fallbacks:
| Method | Pros | Cons | Use Case |
|---|
| WAL CDC | Low overhead, real-time | Postgres-only | Primary |
| Triggers (e.g., pglogical) | Works on any RDBMS | Overhead (5-10%) | MySQL/Vitess |
| Query-based polling | Simple | High CPU/lag | Legacy |
-
Throughput: Handles 10k+ TPS with horizontal Debezium scaling.
3. Read Replicas vs. Streams
| Dimension | Read Replicas (Physical, e.g., Postgres streaming rep) | Streams (Logical CDC, e.g., Kafka/Debezium) |
|---|
| Latency | Sub-second (P99 < 500ms) | 1-5s (P99 < 2s, tunable) |
| Consistency | Eventual (replica lag) | At-least-once (idempotent upserts) |
| Flexibility | Identical schema/queries | Transform/filter (e.g., anonymize PII, aggregate) |
| Scaling | Vertical (more replicas) | Horizontal (Kafka partitions) |
| Cost | Low (built-in Postgres) | Medium (Kafka infra ~$0.1/GB/mo) |
| Fault Tolerance | Replica failover | Exactly-once semantics (Kafka EOS) |
| OLAP Fit | Poor (OLTP schema not analytic-optimized) | Excellent (reshape to star schema) |
| When to Use | Simple reporting, same-engine OLAP (CockroachDB) | HTAP-lite: Decoupled, transformed analytics |
Recommendation: Streams for HTAP-lite
- Replicas good for "warm" reads but fail on schema divergence (e.g., OLAP needs columnar).
- Streams enable polyglot persistence (Postgres → ClickHouse).
Hybrid Option: Replicas for low-latency operational reads + streams for deep analytics.
4. Freshness SLAs
Define SLAs (tailored to use case):
| SLA Tier | Max Lag (P99) | Use Case | Monitoring |
|---|
| Hot | <1s | Dashboards | Debezium lag + Kafka consumer lag |
| Warm | <30s | Reports | OLAP watermark (e.g., ClickHouse max(tx_ts)) |
| Cold | <5min | Batch ML | Scheduled compaction |
5. Isolation & Consistency
Challenges: OLTP needs serializable isolation; OLAP eventual consistency OK for analytics.
| Level | Model | OLTP | OLAP Bridge |
|---|
| Isolation | Serializable (Postgres default) | Full ACID | Snapshot Isolation (MVCC in ClickHouse) |
| Consistency | Strong | Eventual (Causal via tx_id) | Read-your-writes via watermark queries |
| Durability | WAL | Kafka (replicated) | OLAP fsync + replicas |
-
Strategies:
- Causal Ordering: Enrich CDC with
commit_lsn or tx_id; OLAP queries filter WHERE olap_ts >= watermark.
- Idempotency: OLAP tables use
(pk, tx_ts) as sort key → ReplacingMergeTree dedups.
- No Dual Writes: App writes only to OLTP; analytics read OLAP.
- Backfill Safety: Pause CDC during schema changes; use Kafka compacted topics.
- Quarantine: Faulty changes → DLQ for manual replay.
-
Anomaly Handling:
| Scenario | Response |
|---|
| Lost Events | WAL retention (7d) + resnapshot. |
| Phantom Reads | OLAP bloom filters + tx_id bounding. |
| Write Skew | App-level checks pre-analytics. |
Implementation Roadmap
- Week 1: Setup Postgres + Debezium + Kafka (Terraform).
- Week 2: Sink to ClickHouse; test 1k TPS.
- Week 3: Transformers (e.g., join streams for denorm).
- Week 4: Monitoring + SLAs; chaos test (kill Debezium).
Cost Estimate (AWS, 1k TPS)**: ~$200/mo (m5.large Postgres + EKS Kafka + ClickHouse).
Tradeoffs: <5s freshness vs. full HTAP (sub-ms, but 10x cost/complexity). Scale to 10k TPS by partitioning Kafka.
Questions? Need code/configs or benchmarks?