Monedula

Scenario 05.2.1 · Compaction

Kafka log compaction: cleanup.policy=compact explained

Storage · Intermediate · about 2 min · Apache Kafka 4.3

cleanup.policy=compact · keep the latest value per key, purge tombstones after the grace window.

Run this scenario in the playground

Tombstone purged. t+1.600s

After the grace window the tombstone is removed too. Only the latest record per live key remains.

Open at this step: step 3, Tombstone purged

What you start with

cleanup.policy=compact retains records by key instead of deleting by age. A compaction run rewrites the log, keeping only the latest value for each key, dropping the superseded versions, and purging tombstones once their grace window passes.

Step by step

  1. Dirty log · 6 records, 3 keys t+0.000s A compacted topic. Six records; user-1 appears three times, user-2 ends with a tombstone. Press play to compact. Open at this step: step 1, Dirty log · 6 records, 3 keys
  2. Compacted · older keys collapsed t+0.600s Each key keeps only its latest offset; offsets 0-2 are removed and survivors keep their original offsets (gaps). The user-2 tombstone lingers during delete.retention.ms. Real Kafka never compacts the active segment, so these records would first have to roll into a closed one. Open at this step: step 2, Compacted · older keys collapsed
  3. Tombstone purged t+1.600s After the grace window the tombstone is removed too. Only the latest record per live key remains. Open at this step: step 3, Tombstone purged

The takeaway

Compaction turns a topic into a key→latest-value log - duplicate keys collapse to their most recent value, so log size tracks the number of distinct keys, not total write volume.

Configs in this scenario

Embed this scenario

Paste this into a blog post, a course page or your team's docs. The scenario plays inside your page, and the link under it points readers to this walkthrough.

<iframe src="https://monedula.dev/kafka-simulator/embed/?scenario=05.2.1" title="Kafka Simulator: Compaction" width="100%" height="520" style="border:1px solid #d0d0d0;border-radius:8px" loading="lazy"></iframe>
<p><a href="https://monedula.dev/kafka-simulator/scenarios/compaction/">Kafka log compaction: cleanup.policy=compact explained</a></p>

Further reading

Apache Kafka documentation

More in Storage

All scenarios