What if your data log could clean itself and always show the freshest info instantly?
Why Log compaction in Kafka? - Purpose & Use Cases
Imagine you have a huge notebook where you write down every change to your contacts list. Each time you update a phone number, you add a new line instead of erasing the old one. Over time, the notebook becomes bulky and hard to find the latest number.
Manually searching through all entries to find the latest update is slow and tiring. It's easy to make mistakes by reading old data or missing the newest info. Also, the notebook grows endlessly, wasting space and making backups slow.
Log compaction automatically keeps only the latest update for each key, like having a smart notebook that erases old phone numbers and keeps just the newest one. This way, you always get the current data quickly without sifting through old entries.
append all changes to log
search entire log for latest key valueenable log compaction Kafka keeps only latest record per key
Log compaction lets systems efficiently store and retrieve the latest state of data streams, saving space and speeding up access.
In a messaging app, log compaction ensures the server keeps only the latest status of each user (online/offline), so clients quickly get current info without processing all past status changes.
Manual logs grow large and slow to search.
Log compaction keeps only the newest update per key.
This improves storage efficiency and data retrieval speed.
Practice
Solution
Step 1: Understand log compaction concept
Log compaction keeps the latest message per key, removing older duplicates.Step 2: Compare with other options
Deleting old messages is retention, compression is different, replication is for fault tolerance.Final Answer:
To keep only the latest message for each key in a topic -> Option DQuick Check:
Log compaction = latest message per key [OK]
- Confusing log compaction with time-based retention
- Thinking compression means log compaction
- Mixing replication with compaction
Solution
Step 1: Identify correct cleanup policy
Log compaction is enabled by setting cleanup.policy=compact on the topic.Step 2: Eliminate incorrect options
cleanup.policy=delete is default retention, log.compaction.enabled is invalid, retention.ms controls time retention.Final Answer:
cleanup.policy=compact -> Option BQuick Check:
Enable compaction with cleanup.policy=compact [OK]
- Using cleanup.policy=delete instead of compact
- Assuming a separate 'log.compaction.enabled' setting exists
- Confusing retention.ms with compaction setting
key1: v1, key2: v2, key1: v3, key3: v4What messages remain after log compaction?
Solution
Step 1: Identify latest message per key
For key1, latest is v3; key2 is v2; key3 is v4.Step 2: List messages after compaction
Only latest per key remain: key1:v3, key2:v2, key3:v4.Final Answer:
key1: v3, key2: v2, key3: v4 -> Option AQuick Check:
Latest message per key kept [OK]
- Keeping all messages instead of only latest per key
- Confusing order and keeping first message per key
- Dropping keys incorrectly
cleanup.policy=compact on a topic but notice old messages are not removed. What is a likely cause?Solution
Step 1: Understand log compaction requirement
Log compaction requires messages to have keys to identify duplicates.Step 2: Analyze why old messages remain
If messages lack keys, compaction cannot remove duplicates, so old messages stay.Final Answer:
The topic has no keys in messages -> Option AQuick Check:
Compaction needs keys to remove old messages [OK]
- Assuming retention.ms=0 disables compaction
- Blaming debug mode for compaction issues
- Thinking partition count affects compaction directly
Solution
Step 1: Identify requirements for storing current state
Current state needs latest update per user, so keys must be user IDs.Step 2: Choose correct topic configuration
Log compaction enabled with cleanup.policy=compact keeps latest per key.Step 3: Eliminate wrong options
Delete policy removes old messages by time, no keys prevent compaction, random keys break state tracking.Final Answer:
Use a topic with cleanup.policy=compact and produce messages with user ID as key -> Option CQuick Check:
Compaction + keys = current state storage [OK]
- Using delete policy for state storage
- Producing messages without keys
- Using random keys that break compaction logic
Ready to prove it?
Pass the test and this pattern moves unknown
