Kafka
Topic Log Retention
Data Storage
Permanent Storage
Technology Management

Make Kafka Topic Log Retention Permanent

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 powerful distributed streaming platform that can handle high volumes of data and allows numerous producers and consumers to read and write data simultaneously. Among its core features is the capacity for managing log data retention, critical for effective data management and compliance with data storage requirements. This article explains how to make Kafka topic log retention permanent, ensuring that data stored in Kafka topics is retained indefinitely unless explicitly deleted by policies or operations.

Understanding Kafka Topic Log Retention

In Kafka, every topic consists of partitions, where partitions are essentially append-only logs. Each record within these partitions is assigned a sequential ID called offset. Kafka stores these logs for a configurable duration before they are deleted, which helps in managing the storage and ensuring the system does not run out of space.

The retention policy in Kafka is controlled by a set of parameters that dictate how long the data should be retained in a topic. These settings can be specified at the broker level or overridden at the individual topic level.

Key Configuration Parameters:

  • log.retention.hours: This is the default retention policy that determines how long Kafka logs are retained in hours. It defaults to 168 hours (or 7 days).
  • log.retention.bytes: Defines the maximum size in bytes before old data is discarded from a topic.
  • log.retention.minutes and log.retention.ms: Alternative configurations for time-based retention in minutes and milliseconds, respectively.

Setting Up Permanent Retention

To set up permanent retention for a Kafka topic, it’s critical to adjust these parameters correctly. Setting up permanent retention effectively means data should never be automatically deleted based on time or size. Here’s how you can configure it:

Step 1: Disable Time-Based Retention

Set the time-based retention setting to a value that indicates data should never be deleted. You can use the special value -1:

properties
log.retention.hours=-1
log.retention.minutes=-1
log.retention.ms=-1

These settings can be applied globally to all topics on a broker or they can be set per topic using topic-specific configuration.

Step 2: Disable Size-Based Retention

Similarly, disable size-based retention by setting:

properties
log.retention.bytes=-1

This setting ensures that logs are not deleted based on their size. Like time-based settings, this can also be specified globally or per topic.

Step 3: Apply Configuration Changes

Changes can be applied without downtime using Kafka's kafka-configs.sh utility. To modify topic-level configurations, you can use:

bash
bin/kafka-configs.sh --zookeeper <zookeeper-host>:<port> --entity-type topics --entity-name <topic-name> --alter --add-config log.retention.bytes=-1,log.retention.ms=-1

Replace <zookeeper-host>:<port> with your ZooKeeper configuration, and <topic-name> with the name of your Kafka topic.

Considerations and Best Practices

While configuring permanent retention, consider the implications:

  • Storage Management: Ensure your system has adequate storage to handle ever-growing data as Kafka will no longer automatically delete logs.
  • Performance: More data in topics can lead to longer recovery times and could impact performance. Regular monitoring and optimization of performance are crucial.
  • Compliance and Legal: Always consider legal and compliance aspects related to data retention.

Retention vs. Compaction

Another concept in Kafka retention is log compaction, which keeps only the latest value for each key in a topic. This might be useful if you need to maintain a permanent record but only care about the most recent state.

Summary

ParameterSettingDescription
log.retention.hours-1Disables time-based retention (never delete based on time).
log.retention.bytes-1Disables size-based retention (never delete based on size).
log.retention.minutes-1Alternative time-based configuration in minutes.
log.retention.ms-1Alternative time-based configuration in milliseconds.

Permanent retention in Kafka is crucial for scenarios where data loss cannot be tolerated. By carefully tweaking Kafka's retention settings, organizations can ensure that their data persists indefinitely, however, it's essential to manage the resultant data growth effectively to maintain system health and performance.


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.