Send message to different Kafka topics based on configuration
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since being open-sourced by LinkedIn in 2011, Kafka has become one of the most popular platforms for handling real-time data streams in a reliable and scalable way.
One of the powerful features of Kafka is the ability to send messages to different topics dynamically, based on configurable criteria. This functionality can greatly enhance the capability of a system's architecture by decoupling the data producers from consumers and enabling more complex processing pipelines that can react to business needs or data types dynamically.
Understanding Kafka Topics
Kafka topics are categories or feed names where data is stored and published. Each topic is split into partitions for scalability and each message within a partition has a sequential id number, known as offset.
Configuring Kafka Producers to Send Messages to Specific Topics
Kafka producers are applications that publish data streams to Kafka topics. The target topic can be specified dynamically at run-time based on certain conditions in the messaging application. Here’s how this can be typically configured:
Example Scenario
Imagine an application that processes e-commerce orders and sends notifications. You might want to route messages to different topics based on the type of notification—order confirmations and shipping notifications might go to one topic, while inventory alerts might go to another.
Configuring Topic Selection in the Producer Application
This might be implemented in a Kafka producer application written in Java as follows:
In this example, the sendMessage method takes an additional parameter for the topic name, which allows the sender to specify dynamically where the message should go.
Table Summary of Key Points
Here’s a summary of the key attributes of using different topics based on configuration:
| Feature | Description |
| Dynamic Topic Assignment | Producers can decide at runtime which topic a message should be routed to. |
| Scalability | Separate topics can be scaled independently as per the load. |
| Maintenance | Easier to maintain and extend as new topics can be added without modifying existing codebases. |
| Flexibility | Business rules about where messages go can be changed without redeployment of producers. |
| Consumer Simplicity | Consumers can subscribe to specific topics that they are interested in rather than filtering messages themselves. |
Advanced Considerations
Security
Ensure that producer applications have the necessary permissions to write to multiple topics, governed by Kafka’s ACLs (Access Control Lists).
Monitoring and Operations
Using diverse topics increases the complexity of monitoring and operations. It's important to have a robust monitoring system to track the activity and performance of messages across multiple topics.
Configuration Management
To maintain flexibility, consider externalizing topic configurations, possibly in a centralized configuration service, where changes can be rolled out dynamically without the need to redeploy or restart the producer applications.
Conclusion
Using Kafka to route messages dynamically to different topics based on a configuration is a powerful pattern that provides a lot of flexibility and scalability for modern data-driven applications. As with any architectural decision, it requires careful planning and consideration, particularly in areas such as security, monitoring, and configuration management. When implemented effectively, it can greatly enhance the responsiveness and efficiency of your data handling strategies.
Related reading
- Sending a persistent message in RabbitMQ via HTTP API
- Sending Apache Kafka data on web page
- Sending binary file through RabbitMQ
- Sending data with kafka-python only working when briefly delaying code
- Sending designated logs to the Kafka sink using Uber-Zap logger in Go
- Sending Large CSV to Kafka using python Spark
- Sending metrics from kafka to grafana
- Sending RabbitMQ messages via websockets

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.