Kafka Topic
Data Storage
Key Values
Data Management
Information Technology

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.

Practice system design

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:

  1. Create a Topic with Compaction Enabled You can create a compacted topic by setting the cleanup.policy to compact when you create the topic:
bash
   kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic my-compacted-topic --config cleanup.policy=compact
  1. 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.
  2. 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 is 0.5.
  • delete.retention.ms: Sets the time to retain delete markers for a key that has been deleted. Default is 24 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:

KeyInitial ValuesNew ValuesAfter Compaction
Key1Value1, Value2Value3Value3
Key2ValueA, ValueBValueB, ValueCValueC
Key3(no previous values)ValueX, ValueYValueY

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
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.