Retention and compaction

Kafka removes events by age with the delete policy, seven days by default, or keeps the latest value for each key with log compaction, where a tombstone deletes a key.

GazeekKAFKA FUNDAMENTALS: EVENT STREAMING·LESSON 4
LESSON 4

Retention and compaction

Kafka removes events by age with the delete policy, seven days by default, or keeps the latest value for each key with log compaction, where a tombstone deletes a key.

0:00 / 3:34

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: Compaction, step by step (new tab)

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Explain time and size retention and their defaults
  • Say what a segment is
  • Explain how log compaction keeps the latest value per key
  • Explain what a tombstone does
Transcript

Retention and compaction

Space Llama here. Kafka doesn't delete events when you read them. So when does it? Today, retention and log compaction. Two ways a log stays a sensible size. By the end, you'll know which one to pick for each topic. It's all core Apache Kafka.

Retention: delete by age

The default cleanup policy is called delete. Kafka removes old events once they pass the retention time. Out of the box, a broker keeps events for a hundred and sixty-eight hours. That's seven days. There's a size limit too, but by default it's switched off, so only time counts. Those are broker defaults. You can set retention per topic. Keep payments for a year and clicks for a day, if you like. On a topic, the settings are retention dot M S and cleanup dot policy. Set them when you create the topic, or change them later. On disk, each partition's log is stored in chunks called segments, up to about a gigabyte each by default. Retention works on whole segments, not single events. Kafka deletes an old segment once it's past the limit, so some events stay a little longer.

Compaction: keep the latest

The other cleanup policy is compact. Instead of looking at age, it looks at keys. Log compaction makes sure Kafka always keeps at least the last known value for each key. Think of a customer's address. You only need the latest one, not every place they've ever lived. And a record with a key but a null value is special. It's called a tombstone, and it marks that key as deleted.

Compaction, step by step

Here's one partition of a compacted topic, before and after cleaning. Each event shows its key and its value. Watch the keys, not the positions. Before, the log has repeated keys. A and B change three times each. C gets one value. Then a tombstone. Key C, with a null value. That's how you tell Kafka: delete C. Compaction cleans the log. You get it by setting the topic's cleanup policy to compact. After, the latest value for each key survives. A is three, B is three. The older values are gone. So a compacted topic becomes a handy record of current state. One value for each key. And C? Only its tombstone is left, so readers can still see that C was deleted. But not forever. After delete retention M S, one day by default, compaction removes the tombstone too.

Delete or compact?

So which one? Delete keeps every event, for a while. That suits a history where each event matters, like clicks or payments. Compact keeps the latest value for each key, however old. That suits current state, like addresses or settings. Delete is the default. Compaction is something you choose, topic by topic. One detail. Compaction keeps at least the latest value, so a reader may still see some older ones.

Remember

Let's recap. The delete policy removes events by age, after seven days by default. The compact policy keeps the latest value for every key. And a tombstone, a key with a null value, deletes that key. Pick delete for history, and compact for current state.

Practice and what's next

That's how Kafka keeps its logs tidy. Next lesson, Brokers and replication. Where partitions live, and the copies that keep them safe. See you there.

Gazeek Flash · free

Exam questions on your phone

Free questions on How Kafka holds events: quick-fire batches and timed exams, on the web or Android.