Kafka Topic Retention Policy Configuration Tips
Q: How do you configure the retention policy for a Kafka topic?
- Kafka
- Junior level question
Explore all the latest Kafka interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Kafka interview for FREE!
To configure the retention policy for a Kafka topic, you would typically set the retention configuration parameters for that topic. The two main parameters related to retention are:
1. `retention.ms`: This parameter defines the time in milliseconds to retain messages in a topic. Once the messages exceed this time limit, they are eligible for deletion. For example, setting `retention.ms` to `604800000` (which equals 7 days) means that messages older than 7 days will be deleted.
2. `retention.bytes`: This parameter controls the total size of log segments for a topic. If the total size of the messages exceeds this limit, the oldest log segments are deleted to ensure that the total size remains within the limit. For instance, setting `retention.bytes` to `1073741824` (which equals 1 GB) means that if the total size of the messages exceeds 1 GB, the oldest messages will be deleted.
To configure these parameters for a specific topic, you can use the `kafka-topics.sh` command. For example, to set the retention policy for a topic named `my-topic` to a retention time of 7 days, you would run:
```bash
kafka-topics.sh --zookeeper: --alter --topic my-topic --config retention.ms=604800000
```
Alternatively, to set a retention size limit of 1 GB, you would run:
```bash
kafka-topics.sh --zookeeper: --alter --topic my-topic --config retention.bytes=1073741824
```
It's also possible to set these configurations at the broker level, which would apply to all topics that do not have specific overrides. This can be done by modifying the `server.properties` file with settings like:
```properties
retention.ms=604800000
retention.bytes=1073741824
```
After changing the broker configuration, restart the Kafka broker for the changes to take effect.
In summary, by using `retention.ms` and `retention.bytes`, you can effectively manage how long Kafka retains data, ensuring optimal resource usage based on your application’s requirements.
1. `retention.ms`: This parameter defines the time in milliseconds to retain messages in a topic. Once the messages exceed this time limit, they are eligible for deletion. For example, setting `retention.ms` to `604800000` (which equals 7 days) means that messages older than 7 days will be deleted.
2. `retention.bytes`: This parameter controls the total size of log segments for a topic. If the total size of the messages exceeds this limit, the oldest log segments are deleted to ensure that the total size remains within the limit. For instance, setting `retention.bytes` to `1073741824` (which equals 1 GB) means that if the total size of the messages exceeds 1 GB, the oldest messages will be deleted.
To configure these parameters for a specific topic, you can use the `kafka-topics.sh` command. For example, to set the retention policy for a topic named `my-topic` to a retention time of 7 days, you would run:
```bash
kafka-topics.sh --zookeeper
```
Alternatively, to set a retention size limit of 1 GB, you would run:
```bash
kafka-topics.sh --zookeeper
```
It's also possible to set these configurations at the broker level, which would apply to all topics that do not have specific overrides. This can be done by modifying the `server.properties` file with settings like:
```properties
retention.ms=604800000
retention.bytes=1073741824
```
After changing the broker configuration, restart the Kafka broker for the changes to take effect.
In summary, by using `retention.ms` and `retention.bytes`, you can effectively manage how long Kafka retains data, ensuring optimal resource usage based on your application’s requirements.


