Log Compaction
Message-per-Key
Data Storage
Database Management
Kafka

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.

Practice system design

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:

  1. Identifying a Key: Determining what represents a unique identifier (key) for records.
  2. Scanning the Log: The system periodically scans through the log.
  3. Maintaining a Table: A table of keys and offsets (the location of the record in the log) is maintained.
  4. 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:

 
1: User1 - {Name: John Doe, Age: 30}
2: User2 - {Name: Jane Doe, Age: 25}
3: User1 - {Name: John Doe, Age: 31}

After log compaction, this would reduce to:

 
2: User2 - {Name: Jane Doe, Age: 25}
3: User1 - {Name: John Doe, Age: 31}

Entries 1 and 3 both pertain to User1. Since entry 3 is the latest update, entry 1 is discarded.

Benefits of Log Compaction

BenefitDescription
Reduced Disk SpaceOnly the latest entries are stored, which substantially reduces the amount of disk space needed.
Improved Read TimesLess data to scan leads to faster read times, enhancing overall system performance.
Data FreshnessEnsures 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.