How long the messages will be kept stored in topic/partition using kafka 0.9
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, introduced by LinkedIn and maintained by the Apache Software Foundation, is a distributed streaming platform that facilitates high-throughput, fault-tolerant messaging. Kafka organizes messages into topics, which are further subdivided into partitions. The scalability and performance of Kafka are achieved through this partitioning mechanism, as it allows concurrent reads and writes across multiple servers.
In Kafka 0.9, the retention of messages within a topic’s partitions can be controlled and configured using several parameters. Message retention is pivotal to managing storage and ensuring the message availability for consumers that may read at different speeds or might need access to historical data. Here we explore the mechanisms and configurations that dictate how long messages are stored in a topic/partition in Kafka 0.9.
Retention Policies
Kafka 0.9 supports two primary methods of message retention:
- Time-based retention: Messages can be retained based on time, where messages older than a configured time period are deleted.
- Size-based retention: Messages can also be retained based on the total size of messages in a log. Logs exceeding a certain size will have the older messages deleted to stay within limits.
These retention policies ensure that Kafka manages disk space while making messages available for a defined period or data size, per the requirements.
Relevant Configuration Settings
The retention policy is implemented through the following Kafka configuration settings:
log.retention.hours: This is the default setting that determines the number of hours to keep a log file before it is discarded. The default value is typically 168 hours, or 7 days.log.retention.minutes: This setting provides more granular control over log retention by specifying the duration in minutes.log.retention.bytes: This configures the maximum size in bytes of the log file before older messages are truncated. The setting applies per partition.
These settings can be applied globally or can be overridden at the topic level. For example, one might have a default retention policy for most topics but require a longer or shorter retention period for specific topics based on the business or technical needs.
Example Configuration
Suppose there's a requirement to configure a Kafka topic that requires storing messages for 3 days rather than the default of 7 days. This could be achieved by setting the following in the topic-level configuration:
Or, if a topic needs to limit storage to 1GB per partition:
Effect of Topic and Partition Count
It's important to note that increasing the number of topics or partitions can significantly affect storage requirements, as each partition might retain messages according to the specified retention configurations. Hence, planning the number of partitions and topics needs to consider the retention settings to avoid excessive usage of disk space.
Table: Summary of Key Retention Parameters
| Parameter | Description | Default Value | Scope |
log.retention.hours | Determines how long messages are retained in a topic's partition based on time. | 168 hours | Topic/Global |
log.retention.minutes | Minute level control over how long messages are kept. | None | Topic/Global |
log.retention.bytes | Limits the maximum size of a log before older messages are truncated. | None | Topic/Global |
Conclusion
The retention settings in Kafka 0.9 are foundational for managing the lifecycle of messages within a system. By understanding and configuring these parameters effectively, organizations can balance their needs for message availability with the limitations of system storage. Correct configuration ensures that consumers have necessary access to data and that the Kafka cluster remains performant and manageable over time.
Related reading
- how many consumer groups can a kafka topic handle?
- How many Kafka consumers does a streaming query use for execution?
- How many producers to create in kafka?
- how Message Queue System Works?
- how many consecutive elements are smaller before each item in the array
- How many hash functions are required in a minhash algorithm
- How much data can Kafka topic store?
- How much memory Kafka cluster needs?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.