Kafka · senior
Kafka retention, compaction and lag that disappears
Retention is your recovery window. Delete policy removes old segments by time or size. Compaction keeps the latest record per key. A consumer that falls behind retention loses data.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Delete policy (the default cleanup.policy): whole segments are removed after retention.ms or when a partition is bigger than retention.bytes. Only closed segments are deleted, so data can live a little longer.
- Compaction (cleanup.policy=compact): a background cleaner keeps at least the latest record for each key. Offsets never change; you only see gaps. A new consumer reading from the start gets the latest value of every key.
- Delete a key with a tombstone (the key with a null value). It is kept for delete.retention.ms (1 day by default), so a consumer offline longer than that misses the delete.
- Old values can linger: the active segment is never compacted, and the cleaner waits for min.cleanable.dirty.ratio (0.5 by default) and min.compaction.lag.ms.
- Every record on a compacted topic needs a key. compact,delete keeps the latest value per key but also drops very old data.
- Kafka uses compaction for __consumer_offsets. Kafka Streams changelogs and CDC topics use it too. It is not a database: no queries or indexes.
- If a slow consumer falls behind retention, the unread segments are deleted. The next fetch is out of range, and auto.offset.reset=latest jumps to the end. Lag falls to near zero and data is silently skipped.
- Alert on lag measured in time, and on how close the oldest unread message is to retention. Recover from the source of truth, with idempotent processing.
The red pen
Common traps: mistake → fix
Mistake: Compaction keeps exactly one record per key at all times
Fix: older values can linger for a while.
Mistake: A sudden lag drop means the consumer caught up
Fix: it may have jumped past deleted data.
Mistake: Reset offsets to earliest to get the data back
Fix: data already deleted cannot return.
Mistake: A delete reaches every consumer
Fix: not one that was away longer than the tombstone retention.
Easy to hard
Questions interviewers ask, easy to hard
- How does log compaction work, and what are its limits?
- Lag fell from 41,000 to 60 in two minutes and invoices are missing. What happened?
- How do you delete a key in a compacted topic?
- How long should retention be?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.