How to store only latest key values in a kafka topic
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with Kafka, a common requirement is to manage the lifecycle of data such that only the latest value for a given key is retained within the topic. This can be particularly useful for use cases like maintaining the current state of an object or reducing storage costs. Kafka itself offers several mechanisms to manage how data is stored and how long it is retained. This article will outline a practical approach to configuring Kafka so that only the latest key values are stored in a Kafka topic.
Understanding Log Compaction in Kafka
Kafka topics are divided into partitions and each partition is essentially a log of records. By default, Kafka appends records to the end of a log partition and retains all records for a configurable period of time. However, Kafka also offers a feature called "log compaction" which ensures that only the latest value for each key is retained in the log.
Log compaction is particularly useful in scenarios where the entire history of record changes is not required, but rather only the current state or most recent value for each key matters.
How Log Compaction Works
Log compaction works by retaining the last known value for each key within the partition log, even if the retention period has expired for older records. When compacting a log, Kafka will:
- Keep all records except those for which a newer record with the same key exists.
- Delete records when there is a newer record with the same key in the log.
Setting Up a Topic with Log Compaction
To implement log compaction in Kafka, you must configure your topic accordingly. Here are the steps to set up a topic with log compaction:
- Create a Topic with Compaction Enabled You can create a compacted topic by setting the
cleanup.policytocompactwhen you create the topic:
- Producer Writes Producers simply write records to this topic as usual. The keys in your messages should accurately reflect the entities for which you want the latest state stored.
- Kafka Compacts the Log Kafka will continuously compact the log in the background, retaining only the latest value for each key according to the records it has seen.
Configurations Affecting Log Compaction
A few additional configurations are important when setting up log compaction:
min.cleanable.dirty.ratio: This setting determines how much of the log can be "dirty" (i.e., contains records that could be compacted) before Kafka actually triggers the compaction process. It is a fraction (0 to 1) and the default is0.5.delete.retention.ms: Sets the time to retain delete markers for a key that has been deleted. Default is24 hours.
Examples
Assuming a Kafka topic configured for log compaction, here's how different series of key-value pairs might appear in Kafka over time, after compaction:
| Key | Initial Values | New Values | After Compaction |
| Key1 | Value1, Value2 | Value3 | Value3 |
| Key2 | ValueA, ValueB | ValueB, ValueC | ValueC |
| Key3 | (no previous values) | ValueX, ValueY | ValueY |
Benefits of Log Compaction
- Efficient Storage Utilization: Only the latest values are stored.
- Immediate State Recovery: Ideal for scenarios like recovering the latest state of an entity in event sourcing.
Monitoring and Managing Compaction
Monitor log compaction in your Kafka environment using Kafka's performance metrics. Look for metrics related to log cleaner performance and backlog. Efficient management of log compaction ensures that your applications achieve optimal performance and data consistency.
In conclusion, Kafka's log compaction feature is a powerful tool for managing state in distributed systems, providing a straightforward mechanism to only retain the latest values for each key in a topic. Proper configuration and management of this feature helps in optimizing storage and improving data retrieval operations.
Related reading
- How to stream data from Kafka to MongoDB by Kafka Connector
- How to stream data from Kafka topic to Delta table using Spark Structured Streaming
- How to stream large files through Kafka?
- How to subscribe multiple topic using @KafkaListner annotation
- How to subscribe to a list of multiple kafka wildcard patterns using kafka-python?
- How to support multiple KeyBy in Flink
- How to suppress window using wall clock time instead of event time in Kafka streams?
- How to sync data for a particular user, when reading from kafka?

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.