Kafka
Messaging Systems
Data Streaming
Programming
Key-based Retrieval

How to get message by key from kafka topic

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In Apache Kafka, messages are published to topics which are distributed, partitioned, and replicated across multiple nodes in a Kafka cluster. Each message within a partition retains an order assigned by an incremental id called an "offset". Kafka does not support fetching a message directly by a key without additional configurations or external tools. However, you can design your Kafka setup or process to locate messages by key through various approaches.

Understanding Kafka Message Keys

Each Kafka message consists of a key and value. The key is optional and is used primarily to determine the partition to which a message is sent within a topic. By default, Kafka uses the key to apply a consistent hashing function to route messages to specific partitions.

Kafka does not index messages by keys, which means there’s no direct method to retrieve a message using its key akin to database systems. Messages must be read in sequence within a partition to locate a specific key. However, there are methods and tools that can help achieve this more efficiently.

Techniques to Retrieve Messages by Key

1. Consumer Groups and Manual Partition Management

You can create a Kafka consumer that reads from a specific partition and scans each message for the desired key. This approach is straightforward but can be inefficient if the key is rare or the volume of messages is large.

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test-group");
4props.put("enable.auto.commit", "true");
5props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
6props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7
8try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
9    consumer.assign(Collections.singletonList(new TopicPartition("your-topic", 0)));
10    consumer.seekToBeginning(Collections.singletonList(new TopicPartition("your-topic", 0)));
11
12    while (true) {
13        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
14        for (ConsumerRecord<String, String> record : records) {
15            if (record.key().equals("your-search-key")) {
16                System.out.println("Found message: " + record.value());
17                break;
18            }
19        }
20    }
21}

2. Kafka Streams API

Kafka Streams is a client library for building applications and microservices where the input and output data are stored in Kafka topics. You can use Kafka Streams to process records in real-time and maintain a local store (KTable or GlobalKTable) which allows stateful processing. If the key appears frequently, you can use a KTable to maintain the latest value by key.

java
StreamsBuilder builder = new StreamsBuilder();
KTable<String, String> kTable = builder.table("your-topic");

You can then query this KTable for your key to get the latest value.

3. External Indexing Services

Leverage external systems such as Elasticsearch for storing and indexing keys for quick lookup. You need to set up a Kafka Connect connector that sinks messages from Kafka to Elasticsearch. Once the data is in Elasticsearch, you can use its powerful search capabilities to quickly find messages based on keys.

Summary Table

ApproachProsCons
Manual Partition ManagementDirect, simple to implementInefficient for large data volumes
Kafka Streams APIEfficient for frequent keysRequires stream processing infrastructure
External Indexing ServicesFast searches, scalableAdds complexity and external dependencies

Conclusion

Retrieving a message by key directly from Kafka is not supported natively due to its design as a distributed log with an append-only structure. However, by using consumer groups carefully, utilizing Kafka Streams, or integrating with external indexing services, it is possible to efficiently retrieve messages by key.

For effective data retrieval and management, consider the frequency of key access and data volume to choose the most suitable approach. Each method has trade-offs concerning performance, complexity, and architectural impact.


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.