liveBook · Manning

Log cleaner

As explained in Apache Kafka in Action

The Kafka log cleaner is a Kafka broker subsystem that compacts or removes records from partition logs according to configured cleanup rules.

The Kafka log cleaner compacts partition logs by removing outdated records while preserving the latest record for each key. Cleanup is asynchronous: the cleaner checks periodically rather than processing every new message immediately. section 10.4.1 section 10.4.2

A partition becomes eligible when its dirty-segment ratio reaches the configured threshold. The default ratio is 0.5; lowering it reduces stale-storage overhead but increases resource usage because compaction reads the entire partition. section 10.4.1

During compaction, the cleaner:

  • Finds the latest offset for each key in uncompacted segments. section 10.4.2
  • Reads older messages and omits entries superseded by newer values. section 10.4.2
  • Writes retained records into a new segment, then replaces the old segments when the new segment is full. section 10.4.2
  • Leaves the newest segment uncompacted, so it may contain multiple records for one key. section 10.4.2
  • Preserves offsets and message order while removing outdated entries. section 10.4.2

Compacting repeated prices

Producing energy drink:5 followed by energy drink:6 leaves the latest value in a compacted segment, while the newest uncompacted segment can temporarily contain both records. kafka-dump-log.sh can inspect these segments and verify the retained offset.

How Apache Kafka in Action covers it

How deep it goes
Explains eligibility thresholds, segment replacement, uncompacted newest segments, and preserved offsets, but not complete deletion policies.
Examples
Demonstrates repeated keyed prices with kafka-console-producer.sh and verifies segments with kafka-dump-log.sh.
What it assumes
Assumes familiarity with Kafka topics, partition logs, segments, keyed records, and offsets.
What you can do after
Operate compaction by adjusting dirty-ratio and compaction-lag settings, then inspect resulting segments and records.
How it connects
Connects log compaction with log retention, partition logs, segments, keys, offsets, and message ordering.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Apache Kafka in Action. The section links point to the parts of the book it draws on.