Kafka 0.9
Message Storage
Data Partitioning
Topic Retention
Message Queue

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.

Practice system design

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:

  1. Time-based retention: Messages can be retained based on time, where messages older than a configured time period are deleted.
  2. 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:

properties
log.retention.hours=72

Or, if a topic needs to limit storage to 1GB per partition:

properties
log.retention.bytes=1073741824

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

ParameterDescriptionDefault ValueScope
log.retention.hoursDetermines how long messages are retained in a topic's partition based on time.168 hoursTopic/Global
log.retention.minutesMinute level control over how long messages are kept.NoneTopic/Global
log.retention.bytesLimits the maximum size of a log before older messages are truncated.NoneTopic/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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

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

Practice system design