Inferensys

Glossary

Log Compaction

A data retention mechanism that ensures the last known value for each message key is retained by removing older records with the same key, useful for restoring state.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
STATE RESTORATION MECHANISM

What is Log Compaction?

A data retention strategy for distributed event logs that selectively removes obsolete records to preserve storage space while maintaining the latest state for each unique key.

Log compaction is a data retention mechanism in distributed streaming platforms that ensures only the most recent record for each message key is retained by purging older records with identical keys. Unlike time-based retention, which deletes data after a fixed duration, compaction operates on a per-key basis, transforming the log from a complete history into a snapshot of the latest state. This process is essential for restoring state after a failure or for bootstrapping a new consumer without replaying the entire event history.

The mechanism works by identifying duplicate keys across a log's segments and removing older entries during a background cleanup process, leaving a compacted log that contains the last known value for every key. This is particularly critical for use cases like maintaining a **KTable** in Apache Kafka or restoring a state store in stream processors, where only the current state matters, not the sequence of changes that led to it. A tombstone record—a message with a key and a null value—can also be written to explicitly delete a key from the compacted log.

STATE RETENTION MECHANISM

Key Characteristics of Log Compaction

Log compaction is a data retention strategy that transforms a stream of updates into a snapshot of the latest state by removing obsolete records sharing the same key.

01

Snapshot Semantics

Unlike time-based retention, log compaction retains the last known value for every unique message key. It operates by removing older records with identical keys, effectively turning the log into a key-value store of current state. This is critical for restoring stateful services after failure or scaling operations.

  • Guarantees at least one record per key
  • Deletes are signaled by a tombstone record (a record with a null value)
  • Compaction runs as a background process on immutable segments
02

Tombstone Records and Deletion

To explicitly delete a key from the compacted log, a producer sends a tombstone—a record with the key and a null value. The compaction process retains this tombstone for a configurable period (delete.retention.ms) before physically removing it.

  • Prevents deleted keys from reappearing on partition leader changes
  • Consumers must handle null values as explicit delete signals
  • Tombstone retention must be longer than the maximum expected consumer downtime
03

Offset Management Implications

Log compaction breaks the strict sequential offset guarantee. Since records with duplicate keys are removed, offsets become non-contiguous. Consumers cannot assume that offset N+1 exists just because offset N was consumed.

  • Consumers must not rely on offset gaps for logic
  • Re-consuming a compacted topic from offset 0 yields only the latest value per key
  • Ideal for KTable state restoration in Kafka Streams
04

Use Cases and Anti-Patterns

Log compaction excels for state restoration and change data capture scenarios where the current value matters more than the full history.

  • Ideal: Database changelogs, Kafka Streams state stores, shipping addresses
  • Anti-pattern: Immutable event logs (e.g., financial transactions, clickstreams) where every event must be retained
  • Anti-pattern: Topics with infinite unique keys, as compaction cannot delete any records
05

Compaction vs. Retention

Standard time-based retention deletes entire segments after a time threshold. Log compaction operates on key granularity within segments, merging non-compacted segments into compacted ones.

  • Time-based: Deletes all records older than T
  • Compaction: Deletes older records with the same key, regardless of age
  • Both policies can be combined: a topic can have both retention.ms and cleanup.policy=compact
06

Segment and Cleaner Mechanics

The Log Cleaner thread builds an in-memory offset map for each compacted segment. It scans from the latest segment backward, retaining only the highest offset for each key.

  • Operates on immutable segments; never modifies the active segment
  • Dirty ratio (min.cleanable.dirty.ratio) controls when cleaning triggers
  • A segment is eligible when the ratio of dirty (obsolete) to total records exceeds the threshold
LOG COMPACTION

Frequently Asked Questions

Clear, technically precise answers to the most common questions about log compaction, a critical data retention mechanism in distributed streaming platforms.

Log compaction is a data retention mechanism in distributed streaming platforms that selectively removes older records from a log based on their key, ensuring that only the last known value for each unique message key is retained. Unlike time-based or size-based retention policies that delete entire segments of a log wholesale, compaction operates granularly. When the compaction process runs, it scans the log, identifies all records sharing the same key, and physically deletes all but the most recent one. This transforms the log from a complete chronological history into a snapshot of the latest state for every key. The mechanism relies on a background thread, often called the Log Cleaner, which iterates over inactive log segments, builds an in-memory offset map of the latest occurrence of each key, and recopies the segment omitting the stale duplicates. This process is non-blocking for active producers and consumers, operating on closed segments only. The result is a compacted topic that serves as a persistent, key-indexed state store, ideal for restoring state after a failure or bootstrapping a new service instance.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.