Bird
Raised Fist0
Kafkadevops~5 mins

Retention policies (time-based, size-based) in Kafka - Commands & Configuration

Choose your learning style10 modes available
Introduction
Kafka retention policies control how long or how much data is kept in a topic. This helps manage disk space and ensures old data is removed automatically.
When you want to keep messages only for a certain number of hours or days to save disk space.
When you want to limit the total size of data stored in a topic to avoid filling up your server.
When you need to automatically delete old messages after a set time without manual cleanup.
When you want to keep data until it reaches a size limit, then remove the oldest messages first.
When you want to balance between data availability and storage costs by controlling retention.
Config File - server.properties
server.properties
log.retention.hours=168
log.retention.bytes=1073741824

log.retention.hours sets the time in hours to keep messages before deletion (168 hours = 7 days).

log.retention.bytes sets the maximum size in bytes for the log before old data is deleted (1073741824 bytes = 1 GB).

Kafka deletes the oldest data when either limit is reached.

Commands
Create a new Kafka topic named 'example-topic' with 1 partition and 1 replica to start using retention policies.
Terminal
kafka-topics.sh --create --topic example-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
Expected OutputExpected
Created topic example-topic.
→
--topic - Name of the topic to create
→
--partitions - Number of partitions for the topic
→
--replication-factor - Number of replicas for fault tolerance
Set a time-based retention policy of 7 days (604800000 milliseconds) on 'example-topic'.
Terminal
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name example-topic --alter --add-config retention.ms=604800000
Expected OutputExpected
No output (command runs silently)
→
--entity-type topics - Specify that the config is for a topic
→
--entity-name example-topic - Target the specific topic
→
--alter - Change the existing configuration
→
--add-config retention.ms=604800000 - Set retention time to 7 days in milliseconds
Set a size-based retention policy of 1 GB (1073741824 bytes) on 'example-topic'.
Terminal
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name example-topic --alter --add-config retention.bytes=1073741824
Expected OutputExpected
No output (command runs silently)
→
--entity-type topics - Specify that the config is for a topic
→
--entity-name example-topic - Target the specific topic
→
--alter - Change the existing configuration
→
--add-config retention.bytes=1073741824 - Set retention size to 1 GB
Check the current retention settings for 'example-topic' to verify the policies are applied.
Terminal
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name example-topic --describe
Expected OutputExpected
Configs for topic 'example-topic' are retention.ms=604800000,retention.bytes=1073741824
→
--describe - Show current configuration for the topic
Key Concept

If you remember nothing else from this pattern, remember: Kafka deletes old messages automatically when either the time limit or size limit you set is reached.

Common Mistakes
Setting retention.ms or retention.bytes to zero or negative values
This disables retention and can cause Kafka to delete all messages immediately or behave unexpectedly.
Always set retention.ms and retention.bytes to positive values representing your desired limits.
Not verifying the retention settings after applying them
You might think the policy is applied but it could fail silently or be overridden by defaults.
Use kafka-configs.sh --describe to confirm the retention policies are active on the topic.
Setting both retention.ms and retention.bytes to very high values without considering disk space
Kafka will keep data until one limit is reached; if both are very high, disk can fill up causing failures.
Set realistic retention limits based on your storage capacity and data needs.
Summary
Create a Kafka topic to apply retention policies.
Use kafka-configs.sh to set time-based retention with retention.ms.
Use kafka-configs.sh to set size-based retention with retention.bytes.
Verify retention settings with kafka-configs.sh --describe.

Practice

(1/5)
1. What does the retention.ms setting control in Kafka?
easy
A. The maximum size of a Kafka topic
B. How long Kafka keeps data before deleting it
C. The number of partitions in a topic
D. The replication factor of a topic

Solution

  1. Step 1: Understand retention.ms meaning

    retention.ms sets the time duration Kafka retains data before deletion.
  2. Step 2: Compare with other options

    Other options relate to size, partitions, or replication, not time retention.
  3. Final Answer:

    How long Kafka keeps data before deleting it -> Option B
  4. Quick Check:

    retention.ms = time-based retention [OK]
Hint: Remember ms means milliseconds, so it's about time [OK]
Common Mistakes:
  • Confusing retention.ms with retention.bytes
  • Thinking it controls topic size
  • Mixing it with partition count
2. Which of the following is the correct way to set a size-based retention policy in Kafka's topic configuration?
easy
A. "retention.bytes" = 1073741824
B. "retention.time" = 604800000
C. "retention.size" = 1073741824
D. "retention.ms" = 1073741824

Solution

  1. Step 1: Identify correct property for size-based retention

    The correct property is retention.bytes for size limits.
  2. Step 2: Check option values and names

    "retention.bytes" = 1073741824 uses retention.bytes with a size value (1GB). Others use wrong keys or time-based keys.
  3. Final Answer:

    "retention.bytes" = 1073741824 -> Option A
  4. Quick Check:

    Size retention uses retention.bytes [OK]
Hint: Size retention uses bytes, so look for retention.bytes [OK]
Common Mistakes:
  • Using retention.time instead of retention.ms
  • Using retention.size which is invalid
  • Confusing retention.ms with size setting
3. Given a Kafka topic with retention.ms=60000 and retention.bytes=1048576, what happens when the topic data reaches 2MB before 60 seconds pass?
medium
A. Kafka waits until 60 seconds pass before deleting any data
B. Kafka deletes data only if both time and size limits are reached
C. Kafka deletes old data immediately because size limit is reached
D. Kafka ignores retention settings and keeps all data

Solution

  1. Step 1: Understand retention.ms and retention.bytes interaction

    Kafka deletes data when either time or size limit is reached, whichever comes first.
  2. Step 2: Analyze given values

    Size limit is 1MB, data reached 2MB before 60 seconds, so size limit triggers deletion immediately.
  3. Final Answer:

    Kafka deletes old data immediately because size limit is reached -> Option C
  4. Quick Check:

    Retention triggers on earliest limit reached [OK]
Hint: Retention deletes when time OR size limit is reached [OK]
Common Mistakes:
  • Thinking both limits must be reached to delete
  • Assuming time limit always overrides size
  • Ignoring size limit when time not reached
4. You set retention.ms to a negative value in Kafka topic config. What is the likely outcome?
medium
A. Kafka treats it as infinite retention (never deletes data)
B. Kafka throws a configuration error and refuses to start
C. Kafka deletes data immediately after creation
D. Kafka uses default retention time instead

Solution

  1. Step 1: Check Kafka behavior for negative retention.ms

    Kafka interprets negative retention.ms as infinite retention, meaning no data deletion.
  2. Step 2: Confirm other options

    Kafka does not throw error or delete immediately; it does not revert to default automatically.
  3. Final Answer:

    Kafka treats it as infinite retention (never deletes data) -> Option A
  4. Quick Check:

    Negative retention.ms means infinite retention [OK]
Hint: Negative retention.ms disables deletion, keeps data forever [OK]
Common Mistakes:
  • Expecting an error on negative value
  • Assuming immediate deletion
  • Thinking default value applies automatically
5. You want to configure a Kafka topic to keep data for 2 hours or up to 500MB, whichever limit is reached first. Which configuration is correct?
hard
A. "retention.ms"=7200000 only, size limit is not needed
B. "retention.ms"=7200000 and "retention.bytes"=5000000000
C. "retention.ms"=120 and "retention.bytes"=524288000
D. "retention.ms"=7200000 and "retention.bytes"=524288000

Solution

  1. Step 1: Convert 2 hours to milliseconds

    2 hours = 2 * 60 * 60 * 1000 = 7,200,000 ms.
  2. Step 2: Convert 500MB to bytes

    500MB = 500 * 1024 * 1024 = 524,288,000 bytes.
  3. Step 3: Match values to options

    "retention.ms"=7200000 and "retention.bytes"=524288000 matches both correct values; others have wrong size or time values.
  4. Final Answer:

    "retention.ms"=7200000 and "retention.bytes"=524288000 -> Option D
  5. Quick Check:

    2 hours = 7200000 ms, 500MB = 524288000 bytes [OK]
Hint: Convert hours to ms and MB to bytes carefully [OK]
Common Mistakes:
  • Using seconds instead of milliseconds for retention.ms
  • Confusing MB with bytes without conversion
  • Setting only one limit when both are needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard