Why Kafka Connect?
Every data engineering team eventually faces the same challenge: moving data between systems reliably. You might need to replicate PostgreSQL CDC events to Elasticsearch, stream application logs to S3 for archival, or push real-time metrics to InfluxDB. The naive approach, writing custom Kafka producers and consumers for each integration, leads to duplicated effort, inconsistent error handling, and maintenance nightmares.
Kafka Connect solves this with a framework-based approach. It provides a standard interface for connecting Kafka to external systems, with built-in support for fault tolerance, offset management, schema evolution, and parallelism. You configure connectors declaratively, and the framework handles the hard parts.
Core Concepts
- Connector: A plugin that defines how to move data to or from a specific system (e.g., PostgreSQL, S3, Elasticsearch).
- Task: The unit of parallelism. A connector can split work into multiple tasks.
- Worker: The JVM process that runs connectors and tasks. Workers form a cluster for distributed mode.
- Converter: Handles serialization/deserialization of data (JSON, Avro, Protobuf).
- Transform (SMT): Lightweight single-message transformations applied in-flight.
Source Connector: PostgreSQL CDC
The Debezium PostgreSQL connector uses logical replication to capture every INSERT, UPDATE, and DELETE in real-time with minimal impact on the source database:
{
"name": "postgres-cdc-source",
"config": {
"connector.class": "io.debezium.connector.postgresql.PostgresConnector",
"database.hostname": "prod-db.example.com",
"database.port": "5432",
"database.user": "kafka_connect",
"database.password": "${secrets:db-password}",
"database.dbname": "production",
"database.server.name": "prod",
"schema.include.list": "public",
"table.include.list": "public.orders,public.customers,public.products",
"plugin.name": "pgoutput",
"slot.name": "kafka_connect_slot",
"publication.name": "kafka_connect_pub",
"snapshot.mode": "initial",
"heartbeat.interval.ms": "10000",
"tombstones.on.delete": "true",
"transforms": "route,unwrap",
"transforms.route.type": "org.apache.kafka.connect.transforms.RegexRouter",
"transforms.route.regex": "([^.]+)\.([^.]+)\.([^.]+)",
"transforms.route.replacement": "cdc.$3",
"transforms.unwrap.type": "io.debezium.transforms.ExtractNewRecordState",
"key.converter": "org.apache.kafka.connect.json.JsonConverter",
"key.converter.schemas.enable": "true",
"value.converter": "io.confluent.connect.avro.AvroConverter",
"value.converter.schema.registry.url": "http://schema-registry:8081",
"errors.tolerance": "all",
"errors.deadletterqueue.topic.name": "dlq-postgres-cdc",
"errors.deadletterqueue.topic.replication.factor": "3"
}
}
Sink Connector: Elasticsearch
Push transformed events from Kafka into Elasticsearch for full-text search and real-time analytics:
{
"name": "elasticsearch-sink",
"config": {
"connector.class": "io.confluent.connect.elasticsearch.ElasticsearchSinkConnector",
"topics": "cdc.orders,cdc.customers",
"connection.url": "http://elasticsearch:9200",
"type.name": "_doc",
"key.ignore": "false",
"schema.ignore": "false",
"write.method": "upsert",
"behavior.on.null.values": "delete",
"behavior.on.malformed.documents": "warn",
"transforms": "extractKey",
"transforms.extractKey.type": "org.apache.kafka.connect.transforms.ExtractField$Key",
"transforms.extractKey.field": "id",
"batch.size": 500,
"max.buffered.records": 10000,
"linger.ms": 5,
"flush.timeout.ms": 10000,
"max.retries": 5,
"retry.backoff.ms": 500
}
}
Single Message Transforms (SMTs)
SMTs let you reshape records in-flight without writing custom code. Chain multiple transforms for powerful data shaping:
// Chain of transforms for field masking and routing
"transforms": "maskEmail,addTimestamp,route",
"transforms.maskEmail.type": "org.apache.kafka.connect.transforms.MaskField$Value",
"transforms.maskEmail.fields": "email,phone",
"transforms.maskEmail.replacement": "***REDACTED***",
"transforms.addTimestamp.type": "org.apache.kafka.connect.transforms.InsertField$Value",
"transforms.addTimestamp.timestamp.field": "_processed_at",
"transforms.route.type": "org.apache.kafka.connect.transforms.RegexRouter",
"transforms.route.regex": "(.+)",
"transforms.route.replacement": "enriched.${1}"
Exactly-Once Delivery
Combining Kafka Connect's offset management with idempotent sink operations achieves effectively exactly-once delivery:
- Enable
errors.tolerance: allfor fault tolerance - Use idempotent writes (upserts) in sink connectors
- Configure
commit.policyfor periodic offset commits - Leverage Kafka transactions with
producer.ackcks=all
Monitoring and Alerting
Monitor connector health through JMX metrics and set up alerts for production reliability:
# Key metrics to monitor
connect.connect-metrics:type=connector-metrics,connector=
→ connector-status (RUNNING / FAILED / PAUSED)
→ task-failed (count)
→ records-lag-max (consumer lag)
connect.connect-metrics:type=task-metrics,connector=,task=
→ source-record-poll-total (throughput)
→ source-record-write-total (write rate)
→ batch-size-avg (batch efficiency)
→ record-error-rate (error rate)
Conclusion
Kafka Connect eliminates the need to build and maintain custom integration code. With 200+ community connectors covering virtually every database, file system, and SaaS platform, it's the fastest path to building a reliable data integration layer. Combined with Schema Registry for data governance, SMTs for lightweight transformations, and distributed mode for fault tolerance, Kafka Connect is an essential tool in any modern data engineering stack.