---
title: "Kafka log compaction: cleanup.policy=compact explained"
description: "Six records, three keys, one tombstone. Kafka compaction keeps the latest value per key, leaves offset gaps and purges the tombstone after delete.retention.ms."
url: https://monedula.dev/kafka-simulator/scenarios/compaction/
---

# Kafka log compaction: cleanup.policy=compact explained

Scenario 05.2.1 · Compaction. 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 it: https://monedula.dev/kafka-simulator/playground/?scenario=05.2.1

## 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](https://monedula.dev/kafka-simulator/playground/?scenario=05.2.1&t=0)
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](https://monedula.dev/kafka-simulator/playground/?scenario=05.2.1&t=1)
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](https://monedula.dev/kafka-simulator/playground/?scenario=05.2.1&t=2)

## 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`](https://kafka.apache.org/43/configuration/topic-configs/#topicconfigs_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](https://monedula.dev/kafka-simulator/configs/cleanup-policy/)
- [`delete.retention.ms`](https://kafka.apache.org/43/configuration/topic-configs/#topicconfigs_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](https://monedula.dev/kafka-simulator/configs/delete-retention-ms/)

## Further reading

- [Apache Kafka documentation](https://kafka.apache.org/43/design/design/#log-compaction)
