When would you prefer using a compacted topic over a regular topic, and what are the trade-offs?

Log compaction is a Kafka retention policy that keeps only the latest record for each key instead of deleting records purely by age, making compacted topics suitable for representing current state rather than a full event history.

Key Points

Example

A "user-profile" topic keyed by user ID and configured with compaction only needs to retain each user's latest profile snapshot, whereas an "order-events" topic recording every step of an order's lifecycle should stay a regular topic so the full history can be replayed.

# Compacted topic: keeps at least the latest value for every key
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic user-profiles \
  --partitions 6 --replication-factor 3 \
  --config cleanup.policy=compact \
  --config min.cleanable.dirty.ratio=0.1 \
  --config delete.retention.ms=86400000

// Update a profile: only the newest value per key survives compaction
kafkaTemplate.send("user-profiles", "user-42", "{\"city\":\"Pune\"}");
kafkaTemplate.send("user-profiles", "user-42", "{\"city\":\"Mumbai\"}");

// Delete a key: send a tombstone (null value)
kafkaTemplate.send("user-profiles", "user-42", null);

Interview Tip

A concise interview answer is

I'd reach for a compacted topic whenever I only need the latest value per key — like a changelog or current-state table — because it keeps the topic size bounded, but I'd avoid it whenever the full history of events matters, since compaction discards intermediate values over time.

Last updated: 1 November 2026

Previous question: Is it possible to lose data in Kafka despite having replication set up? If so, how?

Next question: Kafka guarantees ordering of messages, but under what conditions could this guarantee be broken?

More Kafka (Optional) interview questions

All Kafka (Optional) questions