Data Engineering
Change Data Capture (CDC) Architecture: Streaming Database Mutations to Kafka with Debezium
Traditional batch ETL pipelines that poll production relational databases via scheduled SQL queries (e.g., SELECT * WHERE updated_at > ?) introduce severe operational drawbacks: heavy lock contention on production OLTP tables, inability to capture hard deletions, and high latency. Change Data Capture (CDC) solves these challenges by directly tailing write-ahead logs (WAL) in PostgreSQL, MySQL, or Oracle and streaming byte-level row changes into Apache Kafka in near real time without affecting operational database performance.
Log-Based CDC vs Batch Query Polling
Evaluating the architectural advantages of log-based mutation streaming against traditional database polling:
| Dimension | Batch Query Polling | Log-Based CDC (Debezium) |
|---|---|---|
| Source Database Impact | High; repetitive table scans & locks | Zero query overhead; tails sequential WAL |
| Latency | Minutes to hours (Batch windows) | Sub-second (Real-time stream) |
| DELETE Operations | Missed unless using soft-deletes | Native tombstone records emitted |
| Intermediate State Changes | Lost (Only latest snapshot captured) | Full historical record of all state mutations |
Debezium PostgreSQL Connector Configuration
Below is a production JSON payload submitted to the Kafka Connect REST API to establish automated CDC replication from a PostgreSQL database using logical decoding via pgoutput:
{
"name": "postgres-inventory-cdc",
"config": {
"connector.class": "io.debezium.connector.postgresql.PostgresConnector",
"tasks.max": "1",
"plugin.name": "pgoutput",
"database.hostname": "postgres-primary.internal",
"database.port": "5432",
"database.user": "debezium_user",
"database.password": "${file:/secrets/db.properties:db_password}",
"database.dbname": "orders_db",
"database.server.name": "pg_cluster",
"table.include.list": "public.orders,public.order_items",
"tombstones.on.delete": "true",
"decimal.handling.mode": "double",
"transforms": "unwrap",
"transforms.unwrap.type": "io.debezium.transforms.ExtractNewRecordState",
"transforms.unwrap.drop.tombstones": "false"
}
}
The Transactional Outbox Pattern with Debezium
When dual-writing to both a local database and a message broker, process crashes can cause inconsistent state. The Transactional Outbox Pattern solves dual-write dilemmas by saving events to an outbox table within the same local database transaction, allowing Debezium to stream them reliably to Kafka:
-- Production PostgreSQL Transaction executing business mutation + Outbox record atomically
BEGIN;
INSERT INTO customer_orders (order_id, customer_id, total_amount, status)
VALUES ('ord-98214', 'cust-4410', 499.50, 'CREATED');
INSERT INTO outbox (id, aggregate_type, aggregate_id, event_type, payload)
VALUES (
gen_random_uuid(),
'Order',
'ord-98214',
'ORDER_CREATED',
'{"order_id": "ord-98214", "total_amount": 499.50, "status": "CREATED"}'::jsonb
);
COMMIT;
- Replication Slot Monitoring: Closely monitor the unconsumed WAL bytes in PostgreSQL (
pg_replication_slots); an inactive consumer can cause disk exhaustion if logs fail to prune. - Schema Evolution Handling: Integrate Debezium with Confluent or Apicurio Schema Registry to enforce backward and forward Avro/Protobuf schema compatibility.
- Exact Partitioning: Partition Kafka topics by the table’s primary key (e.g.,
order_id) to ensure in-order message processing per entity.
