liveBook · Manning

Compacted topic

As explained in Kafka Streams in Action, Second Edition

A compacted topic is a Kafka topic whose log retains the latest record for each key rather than retaining every historical record indefinitely.

A compacted topic retains the latest record for each key instead of deleting complete log segments by time or size. This preserves current state for applications recovering after a crash or restart. section 2.6.1 section 2.6.2

The implementation model is:

  • A background log cleaner recopies log segments and removes an older record when a later record has the same key. section 2.6.2
  • Enable compaction for a topic with the cleanup.policy=compact property. section 2.6.2
  • Delete a key through compaction by writing a null value, which creates a tombstone marker. section 2.6.2
  • Combine compact and delete policies when a large key space could otherwise allow log segments to keep growing. section 7.4.7

Recovering stock prices

Use a stock ticker symbol as the key and its current price as the value. After a crash or restart, the application can rebuild the latest known price for every ticker without replaying every historical update. section 2.6.2

Figure 2.7 On the left is a log before compaction. You’ll notice duplicate keys with different values. These duplicates are updates. On the right is after compaction, which retains the latest value for each key, but it’s smaller in size.
figure

Compare duplicate key updates before compaction with the smaller log retaining only each key’s latest value. section 2.6.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains key-based cleanup, background log cleaning, configuration, tombstones, and combined policies, while deferring broader compaction details.
Examples
Uses stock ticker prices, duplicate keyed updates, tombstones, and a Kafka Streams changelog configuration.
What it assumes
Assumes familiarity with Kafka records, keys, values, log segments, and topics.
What you can do after
Configure Kafka topics with compaction and choose tombstones or combined cleanup policies for keyed state retention.
How it connects
Connects log retention, log segments, log cleaners, tombstone markers, Kafka Streams, and state stores.

Also explained in

Compare how 2 books explain compacted topic →

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.