Kafka
Message Configuration
Topic Distribution
Software Development
Programming Solutions

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.

Practice system design

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:

java
1public class DynamicTopicProducer {
2    private static KafkaProducer<String, String> producer;
3    private static Properties properties = new Properties();
4
5    static {
6        properties.put("bootstrap.servers", "localhost:9092");
7        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
8        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
9    }
10
11    public DynamicTopicProducer() {
12        this.producer = new KafkaProducer<>(properties);
13    }
14
15    public void sendMessage(String key, String value, String topicName) {
16        ProducerRecord<String, String> record = new ProducerRecord<>(topicName, key, value);
17        producer.send(record);
18    }
19
20    public static void main(String[] args) {
21        DynamicTopicProducer producer = new DynamicTopicProducer();
22        producer.sendMessage("order123", "Order Placed", "OrderNotifications");
23        producer.sendMessage("order123", "Order Shipped", "ShippingNotifications");
24        producer.sendMessage("product456", "Low Stock", "InventoryAlerts");
25    }
26}

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:

FeatureDescription
Dynamic Topic AssignmentProducers can decide at runtime which topic a message should be routed to.
ScalabilitySeparate topics can be scaled independently as per the load.
MaintenanceEasier to maintain and extend as new topics can be added without modifying existing codebases.
FlexibilityBusiness rules about where messages go can be changed without redeployment of producers.
Consumer SimplicityConsumers 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.