Put() vs Flush() in Kafka Connector Sink Task
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, a distributed event streaming platform, allows developers to build robust data pipelines for streaming and processing data in real-time. Kafka Connect is a component of Kafka that simplifies integrating Apache Kafka with other systems like databases, key-value stores, search indexes, and file systems. In the context of Kafka Connect, Sink tasks are responsible for pulling messages from Kafka topics and pushing them to external systems.
Understanding Put() and Flush() in Kafka Connector Sink Tasks
Kafka Sink Connectors use the SinkTask class to manage the flow of data from Kafka to the target system. Two fundamental methods in the SinkTask class are put() and flush(). Understanding how put() and flush() work is crucial for developing efficient and reliable sink connectors.
The Put() Method
The put() method is called by the Kafka Connect framework to deliver a batch of records to the sink task. This batch is represented as a collection of SinkRecord objects. Each SinkRecord contains the topic, partition, and offset information of the original Kafka record, as well as the key and value deserialized as per the configuration of the Connector.
The primary role of the put() method is to take these records and execute whatever processing or transformation is necessary before pushing them to the destination system. For most connectors, this involves converting the records into a format suitable for the destination system and then writing them to the system (either directly or via a staging mechanism).
Example of put() usage:
The Flush() Method
The flush() method is used to ensure that all records received in the put() method, which have been written to the destination system but not yet confirmed, are committed. It is crucial for managing the consistency and durability of records during a Kafka Connect Sink Task's operation. This method is typically triggered before committing offsets back to Kafka, serving as a checkpoint to guarantee that the data has landed safely in the target system before acknowledging the consumption of messages.
Example of flush() usage:
How put() and flush() Interact
put() is intended for processing and transiently storing or moving records into the destined system, while flush() is mainly about ensuring data integrity and consistency by confirming all pending records have been permanently written and acknowledged by the destination system.
The interaction between these two methods is precisely what allows Kafka Connect to provide reliable, fault-tolerant data streaming to diverse systems with different capabilities and requirements.
Summary Table
| Method | Purpose | Invocation Frequency | Key Note |
put() | Processes and transfers records to the destination system | Triggered continuously as records are polled from Kafka | Mainly responsible for efficient data processing and transfer |
flush() | Ensures all transferred records are committed in the destination system | Called periodically, especially before committing offsets | Critical for data integrity and consistency |
Conclusion
Developing effective Kafka Connect sink tasks requires a solid understanding of both put() and flush() methods. As these methods are pivotal in managing data flow and ensuring data integrity, developers should design their implementations of these methods to maximize throughput while also safeguarding against data loss and inconsistencies due to system failures or other issues.
By effectively leveraging both put() and flush(), you can ensure that your Kafka Connect Sink Tasks are robust, efficient, and reliable, capable of handling large volumes of data in real-time data pipelines.
Related reading
- pyspark.sql.utils.AnalysisException Failed to find data source kafka
- Python-Kafka Keep polling topic infinitely
- Python - Exit Kafka queue once all messages have been read
- Python and RabbitMQ - Best way to listen to consume events from multiple channels?
- Python how to mock a kafka topic for unit tests?
- Python Kafka multiprocess vs thread
- Python librdkafka producer perform against the native Apache Kafka Producer
- Python Mocking out Kafka for integration tests

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.