How to pass topics dynamically to a kafka listener?
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 streaming platform, allows for building real-time data pipelines and streaming applications. A core feature of Kafka is its use of topics to categorize and manage messages efficiently. As developers, there may be scenarios where you need a Kafka consumer to dynamically subscribe to various topics based on runtime decisions or external configurations, rather than subscribing to a static list defined at startup. This capability is especially useful in multi-tenant environments, configurable systems, or applications that must adjust to changes in the data landscape dynamically.
Understanding the Basics
Before diving into how to handle dynamic topic subscriptions in Kafka listeners, let's clarify a few basic concepts:
- Kafka Consumer: A consumer pulls data from Kafka topics. It subscribes to one or more Kafka topics and reads data from them.
- Kafka Topic: A topic is a category or feed to which records are published. Topics in Kafka are multi-subscriber, and they can have zero or many consumers that subscribe to the data.
Implementing Dynamic Topic Subscriptions
In many Kafka clients, including popular ones in Java, Python, and more, consumers usually subscribe to topics as they initialize. Adjusting this to accommodate dynamic topics requires additional considerations.
1. Using Spring Kafka
In the Java world, Spring Kafka provides robust integration with the Apache Kafka ecosystem. Below is a practical example using Spring Kafka to dynamically adjust topic subscriptions.
Example Code:
This code showcases a Spring component with a Kafka listener that starts listening to a predefined topic ("staticTopic1"). It includes a method (addTopic) to dynamically add new topics to the subscription list. The listener can be adjusted at runtime without needing to stop the entire application.
2. Poll Loop Management in Native Kafka Clients
When not using Spring or similar frameworks, you may manage the Kafka consumer directly. This involves a loop where you poll the server for new data. To subscribe to new topics dynamically, use the consumer API as shown below:
Example Code:
This polling loop checks for new topics and updates the subscription dynamically. newTopicsAvailable() and fetchNewTopics() should be implemented according to your application's logic to obtain new topic names.
Summary
Here's a quick recap of key points discussed:
| Feature | Description |
| Static Subscription | Kafka listeners can subscribe to pre-defined static topics upon initialization. |
| Dynamic Subscription | Using mechanisms like Spring Kafka or the native Kafka API, listeners can subscribe to new topics dynamically at runtime. |
| Implementation | In dynamic implementations, you must manage the consumer's subscription list carefully, especially considering synchronization and state consistency. |
Conclusion
Dynamic subscription to topics in Kafka allows applications to be responsive and adaptive to changes in data requirements and system configurations. It flexibly adjusts to new business needs and data sources, making it a powerful feature for robust, scalable applications.
Related reading
- How to pause a kafka consumer?
- how to pause and resume @KafkaListener using spring-kafka
- How to peek at messages in the queue
- How to pick a Kafka transaction.id
- How to post a task on a celery-rabbitmq queue in PHP?
- How to post messages to RabbitMQ from SQL Server?
- How to prevent duplicate SQS Messages?
- How to Process a kafka KStream and write to database directly instead of sending it another topic

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.