Log compaction with cleanup.policy compact,delete and keyless message handling
0 reputation · 24 Mar 2020, 16:57 UTC
0 reputation · 24 Mar 2020, 16:57 UTC
Design goal is to configure a Kafka topic for an integration test that uses log compaction together with time-based deletion, without access to production credentials.
The topic should retain only the latest value per key under compaction, while messages without keys should be subject to delete retention. The cleanup.policy is intended to be compact,delete and min.cleanable.dirty.ratio is left at its default.
Uncertainty remains about the permission boundary for keyless records under a combined policy and about the timing and resource impact of compaction under sustained write load. The interaction with segment.ms is also not strictly guaranteed.
What is the documented retention behavior for keyless records when cleanup.policy is compact,delete? Under combined policy, does compaction precedence affect delete retention for keyless messages? How should the test configuration bound expected compaction timing and I/O impact without production metrics?
34275 reputation · 24 Mar 2020, 18:32 UTC
When a topic uses cleanup.policy=compact,delete, the broker’s log cleaner follows a two‑phase approach. Compaction only processes records that carry a non‑null key; records with a null key are ignored by the compaction engine. Consequently, keyless messages are never rewritten or merged. They survive only as long as the segment that contains them remains below the delete‑retention thresholds (retention.ms or retention.bytes).
segment.ms and where at least min.cleanable.dirty.ratio (default 0.5) of the records are “dirty” (i.e., have a newer version for the same key). The cleaner rewrites the segment, keeping the latest value per key.retention.ms or exceeds retention.bytes and simply removes the entire segment, regardless of key presence.Because keyless records are excluded from compaction, the delete‑retention path is the only mechanism that can purge them. Compaction may run on the same segment earlier if the dirty‑ratio condition is met, but it does not affect the eventual deletion of keyless messages; the segment will be deleted once it becomes old enough for retention.
\n\nIn an integration test without production credentials, you can influence both timing and disk impact by adjusting a few broker configs:
\nsegment.ms (e.g., 60 s). This forces frequent segment roll‑overs, giving the cleaner a short window to operate on the previous segment.min.cleanable.dirty.ratio at its default 0.5 or raise it if your test writes few records per segment, otherwise compaction may be postponed.log.segment.bytes (e.g., 1 MiB). Smaller segments mean the cleaner rewrites less data per cycle, keeping I/O bounded to a few megabytes.retention.ms (e.g., 120 s) for quick keyless‑record purge. Adjust downwards if you need faster cleanup.You can monitor the cleaner’s activity through JMX: org.apache.kafka.server:type=LogCleaner counters bytesCompacted, bytesDeleted, and cleanedSegments. Sampling these before and after a write burst confirms that compaction and delete retention fire as expected.
kafka-topics.sh --create \\
--bootstrap-server localhost:9092 \\
--replication-factor 1 \\
--partitions 1 \\
--topic test-compact-delete \\
--config cleanup.policy=compact,delete \\
--config retention.ms=120000 \\
--config segment.ms=60000 \\
--config log.segment.bytes=1048576\nWith this setup:
\nretention.ms (≈2 min).The documented behavior is consistent across Kafka 3.x and 4.x. The only version‑sensitive point is that min.cleanable.dirty.ratio was introduced in Kafka 0.10.0; newer releases keep the same default of 0.5. If you run a broker older than 0.10.0, compaction effectively never runs (ratio behaves as 1).
To confirm the behavior in your specific test environment, could you share the Kafka broker version you plan to use? That will help tighten the timing expectations and ensure the cleanup.policy interaction matches the documented logic.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.