kafka + how to calculate the value of log.retention.byte
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. It was originally developed by LinkedIn and subsequently open-sourced through the Apache Software Foundation. Kafka is designed to provide high throughput, built-in partitioning, replication, and fault tolerance which makes it an excellent backbone for application architectures that require processing real-time data streams.
Understanding Kafka's Log Retention Policy
Kafka stores records in topics, and each topic is split into partitions. These partitions are distributed across different brokers in the Kafka cluster to balance the load. For each partition, Kafka appends records to a commit log. For purposes of durability and recovery, these logs must be retained even after the records have been consumed. However, retaining all logs indefinitely is impractical due to storage constraints, so Kafka allows you to configure log retention policies.
Log Retention by Bytes (log.retention.bytes)
One of the configurations available is log.retention.bytes, which sets the maximum size of the log files (combining all partitions) that are retained on disk. If the total size of the log exceeds this configuration, the oldest log files are deleted until the total size is under the limit. This configuration helps manage and cap the storage used by Kafka.
Calculating the optimal size for log.retention.bytes depends on various factors:
- Throughput: The rate at which data is being produced and consumed.
- Data Size: The size of an individual message/event.
- Retention Needs: How long data needs to be accessible via Kafka.
- Available Storage: The total disk space available for Kafka logs.
- Performance Considerations: Larger log files might mean more time seeking the correct position, impacting performance.
Calculation Example:
Suppose you have a Kafka topic with a high volume of incoming data and you want to retain data for approximately one day. The average size of each message is 1 KB, and the system processes about 10,000 messages per minute:
Therefore, setting log.retention.bytes to 15 GB should suffice, allowing some buffer.
Best Practices for Setting log.retention.bytes
- Monitoring: Regularly monitor the disk usage and adjust the retention settings accordingly.
- Scaling: When scaling the system, reassess the storage needs.
- Data Importance: More critical data might require longer retention or even backup to other systems.
Other Log Management Configurations
Besides byte-based retention, Kafka also offers:
log.retention.hours: Sets the maximum time Kafka will retain a log before deleting it, irrespective of the size.log.segment.bytes: Configures the size of a single log file. Once this size is reached, a new log file is created.log.retention.check.interval.ms: Determines the frequency in milliseconds for checking if a log needs to be deleted based on retention policies.
Summary
Here is a table summarizing the key configurations for log management in Kafka:
| Configuration | Description | Typical Usage |
log.retention.bytes | Max size of log files to retain | Capping the storage usage |
log.retention.hours | Max time to retain logs | Ensuring data is not retained too long |
log.segment.bytes | Size per log file | Manage size and rollover of logs |
log.retention.check.interval.ms | Frequency of log retention checks | Performance tuning |
Employing effective log retention settings in Kafka ensures a balance between availability of historical data and resource management, crucial for maintaining an efficient and reliable streaming platform.
Related reading

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.