Kafka
Retention Length
Topic Configuration
Data Storage
Message Retention

Can different Kafka topics have different retention lengths?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a distributed streaming platform that can process large volumes of data in real time. One of its core components is the topic, a category or feed name to which records are published. Topics in Kafka are multi-subscriber; that is, they can have multiple producers and consumers. An essential feature of Kafka topics is the ability to specify different retention policies for each topic, which can be crucial for managing storage and ensuring that data is available as long as needed but no longer.

Understanding Kafka Retention Policies

Retention policies in Kafka dictate how long data should be kept in a topic before it is discarded. Kafka can delete old data based on time or size:

  • Time-based retention: Data is kept for a specified duration (e.g., 7 days) and then deleted.
  • Size-based retention: Data is retained until the total size of the topic logs reaches a specified threshold (e.g., 10GB).

Each Kafka topic can have its own retention policy, allowing flexibility in how different data types are retained based on relevance and compliance requirements. For instance, a topic with critical audit logs might need a longer retention period compared to a topic used for temporary debugging information.

Configuring Topic-Level Retention

To set or alter the retention policy on a Kafka topic, you use the topic-level configuration parameters:

  • retention.ms - This controls the maximum duration that records will remain in the topic log before being removed.
  • retention.bytes - This controls the maximum size of the log on disk of the topic before older records are purged.

You can configure these settings at the topic creation or adjust them later using Kafka's command-line tools. Below is an example with the Kafka command-line:

bash
1# Creating a topic with a custom retention policy
2kafka-topics --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 3 --topic example-topic --config retention.ms=3600000 --config retention.bytes=1073741824
3
4# Altering a topic to change its retention settings
5kafka-configs --bootstrap-server localhost:9092 --entity-type topics --entity-name example-topic --alter --add-config retention.ms=7200000

Practical Examples

Assume you are running a financial application that processes transactions and logs. You might have several topics with different data retention requirements:

  • Transactions: Critical for audit and regulatory needs, might have a retention policy of 180 days.
  • Application Logs: Useful for short-term troubleshooting, might have a retention period of 7 days.

Why Differing Retention Periods?

Using different retention lengths for different topics can significantly help manage storage costs, ensure compliance with data policies, and optimize performance. Storing less critical data for shorter periods can reduce storage needs and costs. Conversely, keeping critical data longer ensures that you meet business and regulatory requirements.

Comparison Table: Example Retention Policies for Different Topics

TopicRetention Purposeretention.msretention.bytesApplicable Scenario
TransactionRecordsRegulatory compliance15552000000Not specifiedFinancial transactions
DebugLogsTroubleshooting604800000536870912Temporary debug information
AuditLogsSecurity and audit trails315360000001073741824Security auditing

Key Considerations

  • Storage Management: Always monitor the disk usage and adjust retention policies as needs evolve.
  • Performance Impact: High retention periods or very large size limits can potentially slow down your Kafka brokers. It's essential to strike a balance based on what the infrastructure can handle and the criticality of the data.
  • Compliance Requirements: Be aware of any legal or regulatory requirements that dictate how long certain data types must be retained.

In conclusion, yes, different Kafka topics can and often should have different retention lengths depending on their use case and criticality. This flexibility is one of Kafka's strengths, allowing it to be used effectively in diverse scenarios from logging to data warehousing while managing costs and performance.


Course illustration
Course illustration

All Rights Reserved.