liveBook · Manning

Log compaction

As explained in Kafka Streams in Action, Second Edition

Log compaction is Kafka's process of removing older records for a key while retaining the most recent record for that key.

Log compaction preserves the newest record for each key while removing older records with the same key. It suits keyed updates such as stock prices, where an application restarting needs the latest value for every symbol rather than the full update history. section 2.6.2

Use compaction when the latest keyed state matters more than every historical update:

  • Enable it per topic with cleanup.policy=compact; a background log-cleaner pool recopies segments and removes superseded records. section 2.6.2
  • Combine compact and delete when a large key space could otherwise let compacted segments keep growing. section 7.4.7
  • Treat a null value as a deletion marker when permanently removing a key from a Kafka Streams state store. section 7.4.7

Recovering current prices

A topic receives updates such as AAPL=190, MSFT=410, and later AAPL=192. After compaction, the log retains AAPL=192 and MSFT=410, allowing a restarted application to rebuild current prices. section 2.6.2 section 8.1.1

Listing 7.22 Setting a cleanup policy using Materialized
Map<String, String> changeLogConfigs = new HashMap<>();
changeLogConfigs.put("cleanup.policy", "compact,delete");

builder.stream("input")
       .groupByKey()
       .count(Materialized.as("counting-store")
              .withLoggingEnabled(changeLogConfigs))       #1
       .toStream()
       .to("output")

Configure a Kafka Streams changelog with compact and delete policies through Materialized.withLoggingEnabled. section 7.4.7

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains per-key cleanup, background segment rewriting, policies, and tombstone deletion, while deferring broader compaction details.
Examples
Uses stock-price updates, duplicate-key logs, compacted changelogs, and a Materialized Java configuration.
What it assumes
Assumes keyed records and Kafka topics; explains compaction’s role in state recovery and changelog storage.
What you can do after
Operate/configure compacted topics and Kafka Streams changelogs for recoverable keyed state.
How it connects
Connects Kafka topics with state stores, changelog topics, Kafka Streams, KTables, event streams, and database tables.

Also explained in

Compare how 3 books explain log compaction →

Related concepts

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