Compare time-based and size-based retention in Kafka. How do consumer offsets and compaction differ?
Instruction: Explain retention.ms, per-partition retention.bytes and cleanup policies. Clarify why committing an offset does not delete a record, and discuss replay and capacity risks.
Updated
Official answer
For a topic using the delete cleanup policy, Kafka normally removes old log segments based on time (retention.ms) or size (retention.bytes). These policies are independent of consumer-group commits. Kafka does not provide a standard topic policy meaning “retain the last N offsets” or “delete everything all consumers acknowledged.”
Time-based retention supports a planned replay horizon, but storage demand grows with traffic. Byte-based retention constrains retained log size per partition and can shorten the replay window during a burst. If both limits are enabled, either can make old segments eligible for deletion. Segment rolling and background cleanup mean the configured limits are not exact instantaneous record-age or physical-disk ceilings.
For example, a four-partition topic with 1 GiB retention per partition has a nominal 4 GiB logical retention budget before replication, active-segment/cleanup overhead and other storage. It is not a 1 GiB limit for the whole topic. Tiered-storage local and total retention settings require separate planning.
Compaction is a different policy: it eventually removes superseded values for a key while preserving the latest surviving state, subject to tombstone handling. It does not keep a fixed number of records. Combining compact and delete also permits age/size deletion; do not assume permanent history.
Set retention using the longest required outage/replay horizon and measured byte rate. Monitor consumers approaching the log start. If a required offset is gone, handle the configured offset-reset or error behavior explicitly; silently resetting to the latest position can skip needed data. Use an external archive when the business needs a longer immutable history.