Log compaction là cơ chế Kafka chỉ giữ lại message mới nhất cho mỗi message key, xóa các phiên bản cũ — thay vì xóa theo thời gian (log.retention.hours).
- Cấu hình:
cleanup.policy=compact. Ứng dụng: KTable trong Kafka Streams (compacted topic lưu state mới nhất), changelog topic của Kafka Connect, event sourcing snapshot (chỉ cần state cuối). Tombstone record: message có key nhưng value=null — báo hiệu cho log compaction xóa tất cả message có key đó khỏi compacted log. - Dùng để xóa data khỏi KTable hoặc downstream systems (GDPR right-to-erasure).
- Log compaction không chạy real-time:
log.cleaner.min.cleanable.ratiovàlog.cleaner.backoff.mscontrol tần suất. - Active segment (segment đang ghi) không bao giờ bị compact — chỉ inactive segments.
- Consumer đọc compacted topic có thể thấy cả message cũ và mới trong một scrub cycle.