Kafka Connector Integration
Complete the full lesson to earn 25 points — 50 with Pro
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks, the wait, and see fewer ads — read each lesson on a single page with Pro
Module: Maintaining Azure Cosmos DB Solutions
Lesson: Kafka Connector Integration for Data Movement
Introduction: Bridging Streaming Data and NoSQL Storage
In modern distributed systems, data rarely remains static. It flows from sensors, user interactions, and microservices into various storage engines. Apache Kafka has established itself as the industry standard for distributed event streaming, acting as a high-throughput backbone for asynchronous data processing. However, storing this streaming data in a format that allows for low-latency queries and global distribution is a separate challenge. This is where Azure Cosmos DB enters the picture.
The Kafka Connector for Azure Cosmos DB provides a critical bridge between these two worlds. By integrating Kafka with Cosmos DB, you enable a architecture where events flowing through topics are automatically persisted into your NoSQL database, or conversely, changes within your database are streamed back out to Kafka for downstream consumption. Understanding how to configure, maintain, and troubleshoot this connector is essential for any engineer tasked with building data pipelines that require high availability and massive scale.
This lesson explores the mechanics of the Kafka Connector, the configuration patterns required to optimize performance, and the operational best practices to ensure your data movement remains reliable under heavy load.
Understanding the Architecture of the Kafka Connector
The Kafka Connect framework is a tool for streaming data between Apache Kafka and other systems. It consists of two primary types of components: Source Connectors and Sink Connectors. When working with Cosmos DB, you will primarily interact with these two patterns to facilitate bi-directional data flow.
The Sink Connector
The Sink Connector is the most common use case. It consumes data from one or more Kafka topics and writes that data into an Azure Cosmos DB container. This is ideal for scenarios where you need to archive event logs, materialize views of event-sourced data, or power dashboards that require real-time updates. The connector handles the heavy lifting of mapping Kafka messages to JSON documents, managing batch sizes, and retrying failed writes.
The Source Connector
The Source Connector works in the opposite direction. It monitors the Change Feed of an Azure Cosmos DB container and publishes these changes as messages onto a Kafka topic. This is incredibly powerful for event-driven architectures where you want to trigger downstream processes—such as sending an email, updating a search index, or invalidating a cache—whenever a record in your database is created or modified.
Callout: Change Feed vs. Sink Connector It is important to distinguish between the two directions. The Sink Connector is essentially a "Kafka-to-Cosmos" pipeline, while the Source Connector is a "Cosmos-to-Kafka" pipeline. When using the Source Connector, you are essentially exposing your database's internal state changes to the rest of your ecosystem, turning your database into a producer of events rather than just a passive store.
Setting Up the Environment
Before you can move data, you must ensure that your environment is prepared for the connector. The Kafka Connector for Azure Cosmos DB is typically deployed as a plugin within a Kafka Connect cluster.
Prerequisites
- Azure Cosmos DB Account: You must have an active SQL API (Core) account.
- Kafka Cluster: A running Kafka cluster (this can be on-premises, managed Kafka like Confluent Cloud, or Azure Event Hubs with Kafka support).
- Kafka Connect Worker: A set of workers that run the connector tasks.
- Connector JAR: You must download the latest version of the Azure Cosmos DB Kafka Connector JAR file from the official repository or Maven Central.
Step-by-Step Installation
- Locate the Plugin Directory: On your Kafka Connect worker nodes, identify the
plugin.pathdirectory defined in yourconnect-distributed.propertiesfile. - Copy the JAR: Move the downloaded Cosmos DB connector JAR file into this directory. If there are dependencies, ensure they are present as well.
- Restart Workers: You must restart the Kafka Connect workers so that they can discover the new plugin classes.
- Verify Discovery: You can verify that the plugin is loaded by sending a GET request to the Kafka Connect REST API:
GET /connector-plugins. You should seecom.azure.cosmos.kafka.connect.sink.CosmosSinkConnectorand the corresponding source connector in the response list.
Configuring the Sink Connector
The Sink Connector requires a JSON configuration file that defines how to map Kafka topics to your Cosmos DB collections. Below is a detailed breakdown of the essential configuration properties.
Essential Configuration Properties
connector.class: This identifies the class to run. For the sink, it iscom.azure.cosmos.kafka.connect.sink.CosmosSinkConnector.tasks.max: Controls the parallelism. Set this based on the number of partitions in your Kafka topic and the throughput requirements of your Cosmos DB container.topics: A comma-separated list of Kafka topics that the connector should subscribe to.connect.cosmos.connection.endpoint: The URI of your Cosmos DB account.connect.cosmos.connection.key: The primary or secondary key for authentication.connect.cosmos.database.name: The target database name.connect.cosmos.container.name: The target container name.
Example Configuration Snippet
{
"name": "cosmos-sink-connector",
"config": {
"connector.class": "com.azure.cosmos.kafka.connect.sink.CosmosSinkConnector",
"tasks.max": "3",
"topics": "user-activity-topic",
"connect.cosmos.connection.endpoint": "https://your-account.documents.azure.com:443/",
"connect.cosmos.connection.key": "your-secret-key",
"connect.cosmos.database.name": "analytics-db",
"connect.cosmos.container.name": "user-events",
"connect.cosmos.sink.batch.size": "100",
"connect.cosmos.sink.bulk.enabled": "true"
}
}
Note: Always use Azure Key Vault or a similar secret management system to store your
connect.cosmos.connection.key. Never hardcode keys in configuration files that are checked into version control.
Optimizing Performance: Bulk Operations and Throughput
One of the most common mistakes when using the Kafka Connector is failing to tune it for the specific throughput requirements of the workload. By default, the connector might not be optimized for high-volume ingestion, leading to bottlenecks in the Kafka Connect cluster.
Enabling Bulk Support
Cosmos DB supports a bulk execution mode that significantly improves throughput by reducing the number of round-trips to the server. When configuring your sink connector, you should always set connect.cosmos.sink.bulk.enabled to true. This allows the connector to group multiple operations into a single request, which is much more efficient for the underlying Cosmos DB SDK.
Batch Size Tuning
The connect.cosmos.sink.batch.size property determines how many messages the connector accumulates before flushing them to Cosmos DB. If you set this too low, you will have high network overhead. If you set it too high, you might hit the request size limit (which is 2MB per request in Cosmos DB). A good starting point is 100 to 500 documents per batch, but you should perform load testing to find the "sweet spot" for your specific document size.
Partitioning Strategy
Your Cosmos DB partition key choice is critical for performance. Ensure that the messages coming from Kafka contain a property that maps to your Cosmos DB partition key. If your partition key is not present in the Kafka message, the connector will fail to write the record, or it will use a default that might lead to "hot partitions" in your database.
Implementing the Source Connector: Streaming from Cosmos DB
The Source Connector is equally important for keeping downstream systems in sync with your database. This is frequently used for microservices that need to react to data changes.
Configuration for Source
The source connector requires the connect.cosmos.source.connector.CosmosSourceConnector class. You must also specify the change feed configuration, such as the starting point for reading changes.
{
"name": "cosmos-source-connector",
"config": {
"connector.class": "com.azure.cosmos.kafka.connect.source.CosmosSourceConnector",
"tasks.max": "1",
"connect.cosmos.connection.endpoint": "https://your-account.documents.azure.com:443/",
"connect.cosmos.connection.key": "your-secret-key",
"connect.cosmos.database.name": "analytics-db",
"connect.cosmos.container.name": "user-events",
"connect.cosmos.source.changefeed.startFromBeginning": "true",
"kafka.topic": "cosmos-changes-topic"
}
}
Managing Change Feed Offsets
The Kafka Connect framework tracks the progress of the source connector using offsets. This ensures that if a worker node crashes, it can resume reading the change feed from the exact point where it left off. This mechanism is built-in and highly reliable, provided that your Kafka Connect cluster has a persistent storage location for its offset data.
Callout: Reliability and At-Least-Once Delivery Both the Sink and Source connectors provide at-least-once delivery guarantees. This means that in the event of a network failure or a crash, a message might be processed more than once. Your downstream applications and your Cosmos DB document schemas should be designed to be idempotent—meaning that processing the same message multiple times does not result in incorrect state.
Operational Best Practices
Maintaining a production-grade data pipeline involves more than just getting the initial configuration correct. You must plan for monitoring, scaling, and handling errors.
Monitoring and Observability
- JMX Metrics: Kafka Connect exposes extensive metrics via JMX. You should monitor
request-latency-avg,record-error-rate, andbatch-size-avg. These metrics will tell you if the connector is struggling to keep up with the incoming data volume. - Azure Monitor: Since the connector interacts directly with Cosmos DB, keep an eye on the
Total Request Units (RU)consumption in the Azure portal. If the connector is causing RU spikes, you may need to increase the throughput of your container or optimize the batching settings. - Dead Letter Queues (DLQ): Kafka Connect supports DLQs. If a message cannot be processed (e.g., due to a schema mismatch or a serialization error), it can be sent to a dedicated "dead letter" Kafka topic instead of stopping the entire connector. This is a vital feature for maintaining uptime.
Handling Schema Evolution
Data formats change over time. If your Kafka messages are in Avro or JSON format, ensure that you use a Schema Registry. The Kafka Connector can leverage the Schema Registry to validate messages before attempting to write them to Cosmos DB. Without this, a single malformed message could cause the connector to enter a crash loop.
Scaling the Connector
If you find that the lag in your Kafka topics is growing, the first step is to increase the number of Kafka partitions. Since each task in Kafka Connect is assigned a subset of partitions, you can then increase tasks.max to match the partition count. This parallelizes the ingestion process and allows you to consume data at a much higher rate.
Common Pitfalls and How to Avoid Them
Even with a solid understanding of the mechanics, engineers often run into specific, preventable issues.
1. Incompatible Partition Keys
- The Problem: The most frequent cause of "400 Bad Request" errors is an attempt to insert a document that lacks the required partition key or has a partition key value that does not match the metadata.
- The Fix: Always validate the schema of your incoming Kafka messages against your Cosmos DB container schema. Use a Kafka Connect Transform (SMT) if you need to modify the message structure before it reaches the sink.
2. Throughput Throttling
- The Problem: The connector attempts to write data faster than the provisioned throughput of the Cosmos DB container, resulting in
429 Too Many Requestserrors. - The Fix: While the connector handles retries automatically, excessive throttling indicates a design flaw. Consider using Autoscale throughput on your Cosmos DB container or implementing a more aggressive backoff strategy in your connector configuration.
3. Misconfigured Offset Storage
- The Problem: The Kafka Connect cluster is configured with an ephemeral offset storage, causing the connector to re-read the entire change feed from the beginning every time a worker restarts.
- The Fix: Ensure that your
connect-distributed.propertiesfile points to a durable Kafka topic foroffset.storage.topic. This topic should have a high replication factor and be compacted to prevent data loss.
4. Ignoring the "Max Request Size" Limit
- The Problem: Attempting to send a single batch that exceeds the 2MB limit allowed by the Cosmos DB SDK.
- The Fix: Use the
connect.cosmos.sink.batch.sizeproperty to limit the number of documents per batch, and ensure that your individual document sizes are well under the limit. If you are dealing with large documents, you may need to implement a pre-processing step to split them.
Quick Reference: Connector Configuration Parameters
| Parameter | Purpose | Recommended Action |
|---|---|---|
tasks.max |
Controls parallelism | Set to match Kafka partition count |
connect.cosmos.sink.bulk.enabled |
Enables bulk API | Always set to true |
connect.cosmos.sink.batch.size |
Documents per batch | Start at 100, tune based on size |
errors.tolerance |
Error handling | Set to all or dlq for production |
connect.cosmos.source.changefeed.startFromBeginning |
Change feed offset | Set true for historical load |
Advanced Integration Patterns
As your system grows, you may find that the standard sink/source patterns are not enough. Here are two advanced scenarios that often arise in production environments.
Pattern 1: Filtering and Transformation with SMTs
Kafka Connect supports Single Message Transforms (SMTs). These are small code snippets that run inside the connector to modify or filter messages before they are processed. For example, if you have a Kafka topic containing multiple types of events, you can use a Filter transform to discard events that aren't relevant to a specific Cosmos DB container. This saves on storage costs and RU consumption.
Pattern 2: Multi-Region Writes
If your Cosmos DB account is globally distributed with multi-region writes enabled, the Kafka Connector can be configured to write to the local region. This reduces latency significantly. To achieve this, ensure your connector is running on infrastructure within the same Azure region as your target Cosmos DB endpoint.
Troubleshooting Checklist
When things go wrong, follow this systematic approach to isolate the issue:
- Check Connector Status: Use
GET /connectors/{name}/statusto see if the connector is in aFAILEDstate. The status response will often contain the stack trace of the error. - Inspect Kafka Connect Logs: The logs on the worker nodes are your primary source of truth. Look for
CosmosExceptionto identify database-level errors. - Verify Authentication: Ensure the connection string and key have not expired and that the Kafka Connect node has network connectivity to the Cosmos DB endpoint (check for firewall rules or VNET restrictions).
- Test Connectivity: Use a simple
curlcommand from the worker node to the Cosmos DB endpoint to ensure there are no network-level blocks. - Review Throughput: Check the Cosmos DB metrics in Azure to see if the container is hitting its RU limit during the time of the failure.
Best Practices for Long-Term Maintenance
- Version Management: Keep your connector JAR files updated. The Azure team frequently releases updates that include performance improvements and bug fixes for the Cosmos DB SDK.
- Automated Deployments: Treat your connector configurations as code. Use a CI/CD pipeline to deploy connector definitions via the Kafka Connect REST API rather than manual configuration.
- Graceful Shutdowns: When performing maintenance on your Kafka Connect cluster, ensure you perform a graceful shutdown of the connectors. This allows the connector to finish its current batch and commit its offsets, preventing duplicate processing upon restart.
- Capacity Planning: Periodically review the throughput of your Kafka topics. If the volume of data is increasing due to business growth, you must proactively scale the Kafka Connect cluster and the Cosmos DB throughput to avoid data ingestion lag.
- Security Audits: Regularly rotate your Cosmos DB keys used by the connector. Use a secret manager that supports programmatic rotation to minimize downtime.
Key Takeaways
- Connectivity: The Kafka Connector acts as a vital bridge for bi-directional data flow, enabling event-driven architectures that combine the strengths of Kafka's streaming and Cosmos DB's NoSQL storage.
- Performance Tuning: Bulk operations and appropriate batch sizing are not optional for high-throughput systems; they are mandatory configurations that define your ability to scale.
- Operational Resilience: Always use Dead Letter Queues and robust offset storage to ensure that your data pipeline is resilient to transient failures and can recover automatically from crashes.
- Observability: Treat the connector as a first-class application. Monitor JMX metrics and Cosmos DB RU consumption to proactively identify bottlenecks before they impact your end-users.
- Idempotency: Because the connector guarantees "at-least-once" delivery, design your downstream logic to be idempotent. This is the single most important design principle for building reliable distributed data systems.
- Scaling: Align your Kafka partitions with your connector
tasks.maxsettings to ensure that data ingestion is evenly distributed across your infrastructure. - Configuration Management: Store configurations in version control and use automated deployment processes to maintain consistency across development, staging, and production environments.
By mastering these concepts, you transition from simply "connecting" two services to architecting a reliable, scalable, and maintainable data movement strategy. The Kafka Connector for Azure Cosmos DB is a powerful tool, and when handled with the right operational rigor, it forms the backbone of highly responsive, event-driven applications.
Reach the last section to complete this lesson and earn points — you're on section 1 of 12.
- Introduction to Cosmos DB Data Modeling
- Introduction to Cosmos DB Data Modeling Quiz5q
- Multiple Entity Types in Same Container
- Multiple Entity Types in Same Container Quiz5q
- Storing Related Entities in Same Document
- Storing Related Entities in Same Document Quiz5q
- Denormalizing Data Across Documents
- Denormalizing Data Across Documents Quiz5q
- Referencing Between Documents
- Referencing Between Documents Quiz5q
- Partition Keys and Document IDs
- Partition Keys and Document IDs Quiz5q
- Time to Live (TTL) Configuration
- Time to Live (TTL) Configuration Quiz5q
- Document Versioning Strategies
- Document Versioning Strategies Quiz5q
- Schema Versioning Patterns
- Schema Versioning Patterns Quiz5q
- Choosing Partition Strategies
- Choosing Partition Strategies Quiz5q
- Partition Key Selection Best Practices
- Partition Key Selection Best Practices Quiz5q
- Transactions and Partition Keys
- Transactions and Partition Keys Quiz5q
- Cross-Partition Query Costs
- Cross-Partition Query Costs Quiz5q
- Data Distribution Analysis
- Data Distribution Analysis Quiz5q
- Throughput Distribution Planning
- Throughput Distribution Planning Quiz5q
- Synthetic Partition Keys
- Synthetic Partition Keys Quiz5q
- Hierarchical Partition Keys
- Hierarchical Partition Keys Quiz5q
- Throughput and Storage Requirements
- Throughput and Storage Requirements Quiz5q
- Serverless vs Provisioned Throughput
- Serverless vs Provisioned Throughput Quiz5q
- Database-Level Provisioned Throughput
- Database-Level Provisioned Throughput Quiz5q
- Granular Scale Units
- Granular Scale Units Quiz5q
- Global Distribution Costs
- Global Distribution Costs Quiz5q
- Configuring Throughput in Portal
- Configuring Throughput in Portal Quiz5q
- Gateway vs Direct Connectivity Mode
- Gateway vs Direct Connectivity Mode Quiz5q
- Creating Database Connections
- Creating Database Connections Quiz5q
- Azure Cosmos DB Emulator
- Azure Cosmos DB Emulator Quiz5q
- Connection Error Handling
- Connection Error Handling Quiz5q
- Singleton Pattern for Clients
- Singleton Pattern for Clients Quiz5q
- Global Distribution Regions
- Global Distribution Regions Quiz5q
- Threading and Parallelism
- Threading and Parallelism Quiz5q
- Arrays and Nested Objects Queries
- Arrays and Nested Objects Queries Quiz5q
- Correlated Subqueries
- Correlated Subqueries Quiz5q
- Array and Type-Checking Functions
- Array and Type-Checking Functions Quiz5q
- Mathematical and String Functions
- Mathematical and String Functions Quiz5q
- Date Functions in Queries
- Date Functions in Queries Quiz5q
- Point Operations vs Query Operations
- Point Operations vs Query Operations Quiz5q
- CRUD Point Operations
- CRUD Point Operations Quiz5q
- Patch Operations for Updates
- Patch Operations for Updates Quiz5q
- Transactional Batch Operations
- Transactional Batch Operations Quiz5q
- Bulk Operations with SDK
- Bulk Operations with SDK Quiz5q
- Optimistic Concurrency with ETags
- Optimistic Concurrency with ETags Quiz5q
- Query Pagination and Continuation
- Query Pagination and Continuation Quiz5q
- Cosmos DB Mirroring for Fabric
- Cosmos DB Mirroring for Fabric Quiz5q
- Mirroring vs Spark Connector
- Mirroring vs Spark Connector Quiz5q
- Enabling Analytical Store
- Enabling Analytical Store Quiz5q
- Synapse Spark and SQL Queries
- Synapse Spark and SQL Queries Quiz5q
- Change Data Capture in Analytical Store
- Change Data Capture in Analytical Store Quiz5q
- Azure Functions and Event Hubs Integration
- Azure Functions and Event Hubs Integration Quiz5q
- Denormalization with Change Feed
- Denormalization with Change Feed Quiz5q
- Referential Integrity with Change Feed
- Referential Integrity with Change Feed Quiz5q
- Azure AI Search Integration
- Azure AI Search Integration Quiz5q
- Azure Functions Change Feed Trigger
- Azure Functions Change Feed Trigger Quiz5q
- Consuming Change Feed with SDK
- Consuming Change Feed with SDK Quiz5q
- Change Feed Estimator
- Change Feed Estimator Quiz5q
- Denormalization via Change Feed
- Denormalization via Change Feed Quiz5q
- Aggregation Persistence with Change Feed
- Aggregation Persistence with Change Feed Quiz5q
- Read-Heavy vs Write-Heavy Indexing
- Read-Heavy vs Write-Heavy Indexing Quiz5q
- Index Type Selection
- Index Type Selection Quiz5q
- Custom Indexing Policies
- Custom Indexing Policies Quiz5q
- Composite Index Implementation
- Composite Index Implementation Quiz5q
- Index Performance Optimization
- Index Performance Optimization Quiz5q
- Response Status Codes and Metrics
- Response Status Codes and Metrics Quiz5q
- Normalized RU Consumption Monitoring
- Normalized RU Consumption Monitoring Quiz5q
- Server-Side Latency Metrics
- Server-Side Latency Metrics Quiz5q
- Data Replication Monitoring
- Data Replication Monitoring Quiz5q
- Azure Monitor Alerts Configuration
- Azure Monitor Alerts Configuration Quiz5q
- Resource Logs Implementation
- Resource Logs Implementation Quiz5q
- Partition Throughput Monitoring
- Partition Throughput Monitoring Quiz5q
- Encryption Key Management
- Encryption Key Management Quiz5q
- Network-Level Access Control
- Network-Level Access Control Quiz5q
- Data Encryption Configuration
- Data Encryption Configuration Quiz5q
- Azure RBAC for Control Plane
- Azure RBAC for Control Plane Quiz5q
- Microsoft Entra ID for Data Plane
- Microsoft Entra ID for Data Plane Quiz5q
- CORS Settings Configuration
- CORS Settings Configuration Quiz5q
- Customer-Managed Keys
- Customer-Managed Keys Quiz5q
- Always Encrypted Implementation
- Always Encrypted Implementation Quiz5q
- Data Movement Strategy Selection
- Data Movement Strategy Selection Quiz5q
- SDK Bulk Operations for Data Movement
- SDK Bulk Operations for Data Movement Quiz5q
- Azure Data Factory Pipelines
- Azure Data Factory Pipelines Quiz5q
- Kafka Connector Integration
- Kafka Connector Integration Quiz5q
- Azure Stream Analytics Integration
- Azure Stream Analytics Integration Quiz5q
- Cosmos DB Spark Connector
- Cosmos DB Spark Connector Quiz5q
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles the points you earn on every lesson and quiz so you progress twice as fast, unlocks half of every practice exam — plus full case studies — with the Learn & Exam study modes, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× points per lesson & quiz
- ✓ 50% of every exam unlocked
- ✓ Learn & Exam modes
- ✓ Distraction-free lessons