This article is heavily inspired by the Kafka section on design around log compaction. You can think of it as the cliff notes about Kafka design around log compaction.
Kafka can delete older records based on time or size of a log. Kafka also supports log compaction for record key compaction. Log compaction means that Kafka will keep the latest version of a record and delete the older versions during a log compaction.
Kafka Log Compaction
Log compaction retains at least the last known value for each record key for a single topic partition. Compacted logs are useful for restoring state after a crash or system failure.
They are useful for in-memory services, persistent data stores, reloading a cache, etc. An important use case of data streams is to log changes to keyed, mutable data changes to a database table or changes to object in in-memory microservice.
Log compaction is a granular retention mechanism that retains the last update for each key. A log compacted topic log contains a full snapshot of final record values for every record key not just the recently changed keys.
Kafka log compaction allows downstream consumers to restore their state from a log compacted topic.
Kafka Log Compaction Structure
With a compacted log, the log has head and tail. The head of the compacted log is identical to a traditional Kafka log. New records get appended to the end of the head.
All log compaction works at the tail of the log. Only the tail gets compacted. Records in the tail of the log retain their original offset when written after being rewritten with compaction cleanup.
Kafka Log Compaction Structure
Kafka Log Compaction Basics
All compacted log offsets remain valid, even if record at offset has been compacted away as a consumer will get the next highest offset.
Kafka log compaction also allows for deletes. A message with a key and a null payload acts like a tombstone, a delete marker for that key. Tombstones get cleared after a period. Log compaction periodically runs in the background by re-copying log segments. Compaction does not block reads and can be throttled to avoid impacting I/O of producers and consumers.
Kafka Log Compaction Process
Kafka Log Compaction Cleaning
If a Kafka consumer stays caught up to head of the log, it sees every record that is written.
min.compaction.lag.ms gets used to guarantee a minimum period that must pass before a message can be compacted. The consumer sees all tombstones as long as the consumer reaches head of a log in a period less than the topic config
delete.retention.ms (the default is 24 hours). Log compaction will never re-order messages, just remove some. Partition offset for a message never changes.
Any consumer reading from the start of the log sees at least final state of all records in the order they were written.
Kafka Log Cleaner
Recall that a Kafka topic has a log. A log is broken up into partitions and partitions are divided into segments which contain records which have keys and values.
The Kafka Log Cleaner does log compaction. The Log cleaner has a pool of background compaction threads. These threads recopy log segment files, removing older records whose key reappears recently in the log. Each compaction thread chooses topic log that has the highest ratio of log head to log tail. Then the compaction thread recopies the log from start to end removing records whose keys occur later in the log.
As the log cleaner cleans log partition segments, the segments get swapped into the log partition immediately replacing the older segments. This way compaction does not require double the space of the entire partition as additional disk space required is just one additional log partition segment - divide and conquer.
Topic Config for Log Compaction
To turn on compaction for a topic, use topic config
To set a delay to start compacting records after they are written, use topic config
log.cleaner.min.compaction.lag.ms. Records won’t get compacted until after this period. The setting gives consumers time to get every record.
Log Compaction Review
What Are Three Ways Kafka Can Delete Records?
Kafka can delete older records based on time or size of a log. Kafka also supports log compaction for record key compaction.
What Is Log Compaction Good For?
Since Log compaction retains last known value it is a full snapshot of the latest records it is useful for restoring state after a crash or system failure for an in-memory service, a persistent data store, or reloading a cache. It allows downstream consumers to restore their state.
What Is the Structure of a Compacted Log? Describe the Structure.
With a compacted log, the log has head and tail. The head of the compacted log is identical to a traditional Kafka log. New records get appended to the end of the head. All log compaction works at the tail of the compacted log.
After compaction, do log record offsets change? No.
What Is a Partition Segment?
Recall that a topic has a log. A topic log is broken up into partitions and partitions are divided into segment files which contain records which have keys and values. Segment files allow for divide and conquer when it comes to log compaction. A segment file is part of the partition. As the log cleaner cleans log partition segments, the segments get swapped into the log partition immediately replacing the older segment files. This way compaction does not require double the space of the entire partition as additional disk space required is just one additional log partition segment.