Monedula

Scenario 05.2.1 · Compaction

Watch Kafka log compaction in a live simulation

Storage · Intermediate · about 2 min · Apache Kafka 4.4

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

  • cleanup.policy Whether old data is removed by age/size (delete), by keeping only the latest value per key (compact), or both - it decides whether the topic behaves like a stream or a keyed changelog. How cleanup.policy works
  • delete.retention.ms On a compacted topic, how long a tombstone (a record with a null value) stays in the log after compaction before it is removed too (default 86400000, 1 day); a consumer reading from the start must finish within it to see every delete. How delete.retention.ms works

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" referrerpolicy="no-referrer-when-downgrade"></iframe>
<p><a href="https://monedula.dev/kafka-simulator/scenarios/compaction/">Watch Kafka log compaction in a live simulation</a></p>

Further reading

Apache Kafka documentation

More in Storage

All scenarios