Software Development Community Question

What are the best practices for 'Compacted Topics' in Kafka?

Posted on 2025-09-22 • In Software Development • By Thomas Anderson

I'm trying to use Kafka as a "Database" by using Log Compaction. The idea is to only keep the latest value for a specific key, like a user's current address. However, I noticed that my disk usage isn't going down as fast as I expected. How does the "Cleaner" thread work? Are there specific "Segment" settings I need to tune to make sure old data is actually deleted in a timely manner?

3 Answers 10,922 Views

All answers to this question 3 Answers

Margaret Moore Answered on 2025-09-24
Answered

Log compaction is great but it’s not "Instant." Kafka doesn't delete old keys as they come in; it waits for a "Segment" to be closed. If your segments are too large (default is 1GB), the cleaner won't touch them for a long time. You should tune segment.ms or segment.bytes to close segments more frequently. Also, check min.cleanable.dirty.ratio. If it's set to 0.5, the cleaner won't start until 50% of the data in the log is "Dirty" (updated). Lowering this to 0.1 will make the cleaner more aggressive, but it will consume more CPU. It’s a balance between "Disk Space" and "System Resources."

Brian Allen Answered on 2025-09-26
Answered

If we use compacted topics, how do we handle "Deletions"? If the bot only keeps the latest value, how do we tell it that a key should be removed entirely?

Kevin Scott Commented on 2025-09-28
Commented

You use what’s called a "Tombstone." You produce a message with the Key you want to delete and a "Null" payload. When the cleaner thread sees this null value, it knows to eventually remove that key and the tombstone itself from the log during the next compaction cycle. Be aware of delete.retention.ms—this setting determines how long the tombstone stays in the log. If your consumer is offline for longer than this period, it might miss the deletion entirely and keep the old data in its local state, leading to a "Data Ghost" issue.

Nancy King Answered on 2025-09-30
Answered

Compacted topics are perfect for "KTable" lookups in Kafka Streams. It makes "State Store" recovery much faster after a pod restart.

Thomas Anderson Commented on 2025-10-02
Commented

Exactly, Nancy. That’s the most common use case. Thomas, definitely look into those segment settings Margaret mentioned; they are usually the culprit for "leaky" disk space.

Write a Comment

Your email address will not be published. Required fields are marked (*)

Join the LearnersEra Community

Ask questions, share knowledge and learn from professionals across technology, project management and certification topics.

Change Country and Nearest City

Select your country and nearest city to see courses available in your region.