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.
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.
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.
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
| Approach | Pros | Cons |
| Manual Partition Management | Direct, simple to implement | Inefficient for large data volumes |
| Kafka Streams API | Efficient for frequent keys | Requires stream processing infrastructure |
| External Indexing Services | Fast searches, scalable | Adds 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
- How to get message from a kafka topic with a specific offset
- How to get rid of negative consumer lag in Kafka
- How to get Spring RabbitMQ to create a new Queue?
- how to get the all messages in a topic from kafka server
- how to get the group commit offset from kafka(0.10.x)
- How to get the latest offset from the Kafka topic in Confluent kafka C# library?
- How to get topic list from kafka server in Java
- How to get topics list from Kafka using C#

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.