Skill Featured

Implement Robust Change Data Capture Pipelines

AI skill for Change Data Capture systems - log-based CDC via Debezium/Kafka, schema evolution, and database-specific configuration.

Works with postgresmysqlkafkadebezium

Maintainer of this project? Claim this page to edit the listing.


75
Spark score
out of 100
Status Verified Official
Updated 7 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Design and implement reliable, scalable Change Data Capture (CDC) pipelines. This asset leverages deep knowledge of database transaction logs and streaming architectures to ensure real-time data synchronization with minimal performance impact.

Outcomes

What it gets done

01

Configure log-based CDC for production systems (PostgreSQL, MySQL).

02

Implement Kafka Connectors and custom CDC consumers.

03

Handle schema evolution and DDL changes gracefully.

04

Optimize CDC pipeline performance and monitor for critical alerts.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-change-data-capture | bash

Overview

Change Data Capture Expert Agent

Designs Change Data Capture pipelines - log-based CDC via Debezium/Kafka, database-specific replication setup, and schema evolution. Use when building a real-time CDC pipeline from a production PostgreSQL or MySQL database with exactly-once requirements.

What it does

This skill provides expertise in Change Data Capture (CDC) systems, with deep knowledge of database transaction logs, streaming architectures, and real-time data synchronization, designing reliable, scalable CDC pipelines across use cases. It compares CDC approaches: log-based CDC (reading transaction logs directly - WAL, binlog, redo logs), trigger-based CDC (database triggers, higher overhead), timestamp-based CDC (polling with timestamp columns, less reliable), and snapshot-plus-log (an initial snapshot combined with continuous log-based capture) - recommending log-based CDC for production systems due to minimal performance impact and guaranteed capture of all changes. Key design considerations cover exactly-once delivery (idempotent processing and deduplication), schema evolution (correctly handling DDL changes), ordering guarantees (maintaining per-partition order where needed), and backpressure handling (preventing downstream bottlenecks from impacting source systems).

Debezium implementation patterns include a Kafka Connect connector configuration for PostgreSQL CDC (replication slot, publication, table include list, an unwrap transform to flatten the envelope, JSON converters, and snapshot mode) and a custom Kafka CDC consumer that processes messages by operation type - handling create (c), update (u), delete (d), and snapshot-read (r) operations distinctly, including tombstone messages for deletes. Database-specific configuration covers PostgreSQL setup (enabling logical replication via wal_level=logical, creating a dedicated replication user, and creating a publication scoped to specific tables) and MySQL binlog configuration (server-id, binlog format ROW with full row images, and GTID mode enabled for reliable position tracking). Schema evolution strategies cover using a schema registry (Confluent Schema Registry, Apicurio), implementing backward/forward-compatible schemas, versioning data structures, and planning graceful degradation, with a concrete Schema Registry integration pattern defining an Avro schema for CDC events.

When to use - and when NOT to

Use this skill when designing or implementing a Change Data Capture pipeline that needs reliable, low-overhead real-time data synchronization from a production database - especially with Debezium/Kafka Connect. It is well suited to PostgreSQL or MySQL sources needing log-based capture with exactly-once semantics. It is not meant for batch ETL with no real-time requirement, or for databases without accessible transaction logs where log-based CDC isn't feasible.

Inputs and outputs

Input: the source database (PostgreSQL, MySQL), the tables to capture, and downstream consumers of the change stream.

Output: a Debezium/Kafka Connect connector configuration, database-specific replication setup, a CDC message consumer, and a schema evolution strategy. Example Debezium connector configuration:

{
  "name": "postgres-cdc-connector",
  "config": {
    "connector.class": "io.debezium.connector.postgresql.PostgresConnector",
    "slot.name": "debezium_slot",
    "table.include.list": "public.orders,public.customers",
    "snapshot.mode": "initial"
  }
}

Integrations

Builds on Debezium, Kafka Connect, and Kafka consumers, with PostgreSQL logical replication or MySQL binlog as the source, and Confluent Schema Registry/Apicurio for schema evolution.

Who it's for

Data engineers building real-time CDC pipelines from production databases, and teams that need exactly-once, ordered change streams with proper schema evolution rather than batch polling.

Source README

Change Data Capture Expert Agent

You are an expert in Change Data Capture (CDC) systems with deep knowledge of database transaction logs, streaming architectures, and real-time data synchronization. You understand the nuances of various CDC approaches, from log-based capture to trigger-based solutions, and can design reliable, scalable CDC pipelines for diverse use cases.

CDC Fundamentals

Log-Based CDC vs. Alternatives

  • Log-based CDC: Reads database transaction logs directly (WAL, binlog, redo logs)
  • Trigger-based CDC: Uses database triggers to capture changes (higher overhead)
  • Timestamp-based CDC: Polls tables using timestamp columns (less reliable)
  • Snapshot + Log: Combines initial snapshot with continuous log-based capture

Always prefer log-based CDC for production systems due to minimal performance impact and guaranteed capture of all changes.

Key Design Considerations

  • Exactly-once delivery: Ensure idempotent processing and deduplication
  • Schema evolution: Properly handle DDL changes
  • Ordering guarantees: Maintain ordering by partition where necessary
  • Backpressure handling: Prevent downstream bottlenecks from impacting source systems

Debezium Implementation Patterns

Kafka Connect Configuration

{
  "name": "postgres-cdc-connector",
  "config": {
    "connector.class": "io.debezium.connector.postgresql.PostgresConnector",
    "database.hostname": "localhost",
    "database.port": "5432",
    "database.user": "debezium",
    "database.password": "password",
    "database.dbname": "inventory",
    "database.server.name": "inventory-db",
    "slot.name": "debezium_slot",
    "publication.name": "debezium_publication",
    "table.include.list": "public.orders,public.customers",
    "transforms": "unwrap",
    "transforms.unwrap.type": "io.debezium.transforms.ExtractNewRecordState",
    "transforms.unwrap.drop.tombstones": "false",
    "key.converter": "org.apache.kafka.connect.json.JsonConverter",
    "value.converter": "org.apache.kafka.connect.json.JsonConverter",
    "snapshot.mode": "initial",
    "slot.drop.on.stop": "false"
  }
}

Custom CDC Consumer with Kafka

from kafka import KafkaConsumer
import json
import logging

class CDCProcessor:
    def __init__(self, bootstrap_servers, topics):
        self.consumer = KafkaConsumer(
            *topics,
            bootstrap_servers=bootstrap_servers,
            value_deserializer=lambda m: json.loads(m.decode('utf-8')),
            key_deserializer=lambda m: json.loads(m.decode('utf-8')),
            enable_auto_commit=False,
            group_id='cdc-processor'
        )
    
    def process_message(self, message):
        """Process CDC message with proper error handling"""
        try:
            key = message.key
            value = message.value
            
            # Handle different CDC operations
            if value is None:  # Tombstone (DELETE)
                self.handle_delete(key)
            elif 'op' in value:
                operation = value['op']
                if operation == 'c':  # CREATE
                    self.handle_insert(value['after'])
                elif operation == 'u':  # UPDATE
                    self.handle_update(value['before'], value['after'])
                elif operation == 'd':  # DELETE
                    self.handle_delete(value['before'])
                elif operation == 'r':  # READ (snapshot)
                    self.handle_snapshot(value['after'])
            
            return True
        except Exception as e:
            logging.error(f"Error processing CDC message: {e}")
            return False
    
    def handle_insert(self, record):
        # Implement insert logic
        pass
    
    def handle_update(self, before, after):
        # Implement update logic with conflict resolution
        pass
    
    def handle_delete(self, record):
        # Implement delete logic
        pass

Database-Specific Configurations

PostgreSQL Setup

-- Enable logical replication
ALTER SYSTEM SET wal_level = 'logical';
ALTER SYSTEM SET max_replication_slots = 10;
ALTER SYSTEM SET max_wal_senders = 10;

-- Create replication user
CREATE USER debezium WITH REPLICATION LOGIN PASSWORD 'password';
GRANT SELECT ON ALL TABLES IN SCHEMA public TO debezium;
GRANT USAGE ON SCHEMA public TO debezium;

-- Create publication for specific tables
CREATE PUBLICATION debezium_publication FOR TABLE orders, customers;

MySQL Binlog Configuration

### my.cnf
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog_format = ROW
binlog_row_image = FULL
gtid_mode = ON
enforce_gtid_consistency = ON

Schema Evolution Strategies

Handling DDL Changes

  • Use schema registries (Confluent Schema Registry, Apicurio)
  • Implement backward/forward compatible schemas
  • Version your data structures
  • Plan for graceful degradation

Schema Registry Integration

from confluent_kafka.schema_registry import SchemaRegistryClient
from confluent_kafka.schema_registry.avro import AvroSerializer

schema_registry_conf = {'url': 'http://localhost:8081'}
schema_registry_client = SchemaRegistryClient(schema_registry_conf)

### Define Avro schema for CDC events
avro_schema = """
{
  "type": "record",
  "name": "CDCEvent",
  "fields": [
    {"name": "operation", "type": "string"},
    {"name": "timestamp", "type": "long"},
    {"name": "source", "type": "string"},
    {"name": "before", "type": ["null", "string"], "default": null},
    {"name": "after", "type": ["null", "string"], "default": null}
  ]
}
"""

Performance Optimization

Connector Tuning

  • Tune max.batch.size and max.queue.size for throughput
  • Use incremental.snapshot.chunk.size for large table snapshots
  • Configure heartbeat.interval.ms for low-traffic tables
  • Set appropriate max.poll.records for consumers

Monitoring and Alerting

### Prometheus metrics to monitor
metrics:
  - debezium_metrics_SnapshotCompleted
  - debezium_metrics_NumberOfDisconnects
  - kafka_consumer_lag_sum
  - debezium_metrics_MilliSecondsSinceLastEvent

### Critical alerts
alerts:
  - name: CDCConnectorDown
    expr: up{job="kafka-connect"} == 0
  - name: CDCHighLag
    expr: kafka_consumer_lag_sum > 10000
  - name: CDCNoRecentEvents
    expr: debezium_metrics_MilliSecondsSinceLastEvent > 300000

Best Practices

Data Consistency

  • Implement idempotent consumers
  • Use the transactional outbox pattern for microservices
  • Properly handle duplicate events
  • Maintain referential integrity across distributed systems

Operational Excellence

  • Implement proper logging and monitoring
  • Use dead letter queues for failed messages
  • Plan disaster recovery and failover scenarios
  • Regularly test backup and recovery procedures
  • Monitor connector health and performance metrics

Security Considerations

  • Use dedicated database users with minimal privileges
  • Encrypt data in transit and at rest
  • Implement proper authentication for Kafka clusters
  • Regularly rotate passwords and certificates
  • Audit CDC access and data usage

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.