Kafka
Data Segments
System Behavior
Database Management
Bug Fixes

Kafka Deletes segments even before segment size is reached

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Fundamentally, Kafka is designed for fault tolerance, reliability, and scalability within modern data-driven applications. One of the key aspects of Kafka's architecture is its approach to data storage, particularly how and when data is deleted. Understanding these behaviors—such as deleting segments before the max segment size is reached—is vital for optimal Kafka performance and reliability.

Understanding Kafka Segments

Kafka stores its messages in a segmented log format. Each topic partition is broken down into segments. A segment in Kafka is a log file accompanied by an index file. The log file stores the actual messages (key-value pairs), while the index file holds pointers to these messages, aiding in fast retrieval.

Here's why segments are essential:

  • Segmentation improves performance: By dividing logs into segments, Kafka keeps the operations manageable. Segmentation makes it easier to delete old data and speeds up recovery in case of server restarts, as only a few segments might need recovery.
  • Efficient data eviction: Older segments can be deleted or compacted more efficiently, ensuring that the store doesn't grow indefinitely and remains performant.

Criteria for Deleting Segments

Kafka provides two primary parameters to control the deletion of log segments, which ensure that the log storage behaves both in predictable and configurable manners:

  1. Retention Period (log.retention.hours): Log files older than this period are eligible for deletion. The default retention period is 168 hours (7 days).
  2. Segment Size (log.segment.bytes): The maximum size of a single log file (segment). Once this size is reached, Kafka rolls over to a new segment. The default size is typically set to 1GB.

Although one would expect that segments are deleted strictly based on these two parameters, it’s important to note that Kafka might delete the log segments before reaching the maximum segment size. This could happen under several scenarios.

Reasons for Early Segment Deletion

1. Retention Policy Checks: Kafka periodically checks if log segments meet the deletion criteria based on time retention or size. This check doesn't wait for the segment to reach its maximum size if the retention time has already expired.

2. Log Compaction: In topics configured with the cleanup policy set to compact, Kafka might delete segments as part of its compaction process, which removes old duplicates of keys.

3. Broker Configurations and Overrides: Broker-level settings or topic-level overrides can influence when and why segments are deleted earlier than the anticipated segment size.

4. Administrative Actions: Administrators might manually trigger deletion or compaction either for maintenance purposes or reacting to specific incidents (e.g., disk space issues).

Technical Examples

Example: If a Kafka broker is set with log.retention.hours=168 and log.segment.bytes=1073741824 (1GB), but the log.retention.check.interval.ms is short, it could check and enforce the retention policy before the segment reaches 1GB size. If messages within a segment become older than 168 hours before the segment fills, they'll be deleted upon the next retention check:

properties
log.retention.hours=168
log.segment.bytes=1073741824  # 1GB
log.retention.check.interval.ms=300000  # 5 minutes

Summary Table

ParameterDefault ValueDescription
log.retention.hours168 hoursMaximum time Kafka keeps a log file before deleting it.
log.segment.bytes1GBMaximum size of a log file before a new segment is created.
log.retention.check.interval.ms300000 msHow often Kafka checks if log segments can be deleted.

Conclusion

Understanding Kafka's segment management and deletion criteria facilitates better application and infrastructure planning. This knowledge is crucial for maintaining performance and meeting compliance or business requirements in data retention and storage management. Always remember to adjust these settings based on specific application needs and usage patterns to optimize the functionality of your Kafka deployment.


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.