Log compaction to keep exactly one message per key
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Log compaction is a critical process in the management of data in systems that use a log-structured approach to data storage and retrieval. This approach is particularly prevalent in distributed systems and databases like Apache Kafka, where efficiency and data consistency are paramount. Log compaction ensures that the log or the record of events in a system is kept manageable by reducing redundancy, thereby making the data storing system more efficient and quicker in data retrieval.
What is Log Compaction?
Log Compaction is a method of cleaning up log files or data streams by retaining only the latest update for each unique key in the log. This means that for each key, no matter how many updates were recorded, exactly one value—the latest—will be preserved. Older values are discarded, which not only saves space but also enhances performance in terms of data retrieval times.
How Log Compaction Works
Imagine a log that records changes to user profiles in a system. As users update their profiles, each change is appended to the log. Over time, this log grows. Without compaction, the log would keep every change, including many updates for the same user. With log compaction, however, only the most recent update for each user is kept.
Technically, log compaction involves:
- Identifying a Key: Determining what represents a unique identifier (key) for records.
- Scanning the Log: The system periodically scans through the log.
- Maintaining a Table: A table of keys and offsets (the location of the record in the log) is maintained.
- Discarding Duplicates: Older records for a given key are removed, keeping only the newest entry.
Example of Log Compaction
Consider a log consisting of the following entries:
After log compaction, this would reduce to:
Entries 1 and 3 both pertain to User1. Since entry 3 is the latest update, entry 1 is discarded.
Benefits of Log Compaction
| Benefit | Description |
| Reduced Disk Space | Only the latest entries are stored, which substantially reduces the amount of disk space needed. |
| Improved Read Times | Less data to scan leads to faster read times, enhancing overall system performance. |
| Data Freshness | Ensures that the data stored is the most recent and relevant, enhancing data accuracy. |
Additional Subtopics
Compaction Triggers
Log compaction can be triggered based on different criteria:
- Time-based: Occurs at regular time intervals.
- Size-based: Triggered when the log reaches a certain size.
- Event-based: Initiated by specific events or conditions.
Handling Deletes
A special record, commonly known as a "tombstone", is used to mark keys that should be deleted. When the compaction process encounters a tombstone, it can remove the record entirely.
Configuring Log Compaction
In systems like Apache Kafka, log compaction is configurable:
- min.compaction.lag.ms: The minimum time a message will remain uncompacted.
- delete.retention.ms: Controls how long tombstone markers are retained.
Conclusion
Log compaction plays a vital role in modern distributed systems and databases that use logging for data management. This process ensures that data stays relevant, manageable, and efficient for real-time processing tasks. By understanding and efficiently implementing log compaction, organizations can enhance their data handling capabilities, ultimately leading to better performance and lower costs.
Related reading
- Login module control flag is not available in the JAAS config - Scala Kafka
- Logstash with Kafka Unable to decode avro
- Logstash with multiple kafka inputs
- Magic byte in Apache Kafka
- Log viewer for binary logs
- Logging all queries with cassandra-python-driver
- Make Kafka Topic Log Retention Permanent
- Make RabbitMQ durable/persistent queues survive Kubernetes pod restart

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.