Retention policies (time-based, size-based) in Kafka - Time & Space Complexity
When Kafka manages data retention, it must decide which messages to keep or delete.
We want to understand how the work Kafka does grows as data or time limits change.
Analyze the time complexity of Kafka's retention check process.
// Pseudocode for retention check
for each partition in topic:
for each segment in partition:
if segment is older than retention time or segment size exceeds limit:
delete segment
This code checks each data segment in partitions to decide if it should be deleted based on time or size limits.
Look at what repeats in this process.
- Primary operation: Looping over all segments in all partitions.
- How many times: Once per segment, for every partition in the topic.
As the number of partitions and segments grows, the checks grow too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 segments | 10 checks |
| 100 segments | 100 checks |
| 1000 segments | 1000 checks |
Pattern observation: The work grows directly with the number of segments Kafka must check.
Time Complexity: O(n)
This means the time Kafka spends checking retention grows linearly with the number of data segments.
[X] Wrong: "Kafka checks only a few segments, so retention checks are always fast."
[OK] Correct: If there are many partitions and segments, Kafka must check each one, so the work grows with data size.
Understanding how Kafka's retention policies scale helps you explain system behavior and resource use clearly.
"What if Kafka used a more efficient index to track segments needing deletion? How would the time complexity change?"
Practice
retention.ms setting control in Kafka?Solution
Step 1: Understand retention.ms meaning
retention.mssets the time duration Kafka retains data before deletion.Step 2: Compare with other options
Other options relate to size, partitions, or replication, not time retention.Final Answer:
How long Kafka keeps data before deleting it -> Option BQuick Check:
retention.ms = time-based retention [OK]
- Confusing retention.ms with retention.bytes
- Thinking it controls topic size
- Mixing it with partition count
Solution
Step 1: Identify correct property for size-based retention
The correct property isretention.bytesfor size limits.Step 2: Check option values and names
"retention.bytes" = 1073741824 usesretention.byteswith a size value (1GB). Others use wrong keys or time-based keys.Final Answer:
"retention.bytes" = 1073741824 -> Option AQuick Check:
Size retention uses retention.bytes [OK]
- Using retention.time instead of retention.ms
- Using retention.size which is invalid
- Confusing retention.ms with size setting
retention.ms=60000 and retention.bytes=1048576, what happens when the topic data reaches 2MB before 60 seconds pass?Solution
Step 1: Understand retention.ms and retention.bytes interaction
Kafka deletes data when either time or size limit is reached, whichever comes first.Step 2: Analyze given values
Size limit is 1MB, data reached 2MB before 60 seconds, so size limit triggers deletion immediately.Final Answer:
Kafka deletes old data immediately because size limit is reached -> Option CQuick Check:
Retention triggers on earliest limit reached [OK]
- Thinking both limits must be reached to delete
- Assuming time limit always overrides size
- Ignoring size limit when time not reached
retention.ms to a negative value in Kafka topic config. What is the likely outcome?Solution
Step 1: Check Kafka behavior for negative retention.ms
Kafka interprets negative retention.ms as infinite retention, meaning no data deletion.Step 2: Confirm other options
Kafka does not throw error or delete immediately; it does not revert to default automatically.Final Answer:
Kafka treats it as infinite retention (never deletes data) -> Option AQuick Check:
Negative retention.ms means infinite retention [OK]
- Expecting an error on negative value
- Assuming immediate deletion
- Thinking default value applies automatically
Solution
Step 1: Convert 2 hours to milliseconds
2 hours = 2 * 60 * 60 * 1000 = 7,200,000 ms.Step 2: Convert 500MB to bytes
500MB = 500 * 1024 * 1024 = 524,288,000 bytes.Step 3: Match values to options
"retention.ms"=7200000 and "retention.bytes"=524288000 matches both correct values; others have wrong size or time values.Final Answer:
"retention.ms"=7200000 and "retention.bytes"=524288000 -> Option DQuick Check:
2 hours = 7200000 ms, 500MB = 524288000 bytes [OK]
- Using seconds instead of milliseconds for retention.ms
- Confusing MB with bytes without conversion
- Setting only one limit when both are needed
Ready to prove it?
Pass the test and this pattern moves unknown
