Kafka
Message Production
Null Value
Tombstone
Console Commands

Producing a Kafka message with a Null Value (Tombstone) from the Console

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka supports a concept known as tombstone messages, which are used to represent the deletion of a key from the log in Kafka topics which support log compaction. A tombstone message is a message with a key that is not null, but has a null value. This can be useful in a setup where Kafka acts as a storage or caching mechanism that supports read and delete operations.

Basic Concepts

Before diving into how to produce a tombstone message from the Kafka console, it's essential to understand two key concepts: log compaction and key-value pairs in Kafka messages.

  1. Log Compaction: Log compaction ensures that Kafka retains at least the last known value for each key within a compacted topic. Old records with the same key are compacted to save space. A tombstone (a record with a key and a null value) marks a key for deletion, and when the compactor runs, it will remove all records with that key.
  2. Key-Value Pairs in Messages: Kafka messages consist of a key and a value. The key is used to decide the partition within a topic where the message will be placed. This is crucial for maintaining the order of messages with the same key.

Producing a Tombstone Message from the Console

Producing messages, including tombstones, from the Kafka console requires the Kafka command-line tools that come with a standard Kafka installation. The primary tool for this task is the kafka-console-producer.

Here’s how you can send a tombstone message:

  1. Start the Kafka Environment: Ensure that your Kafka and Zookeeper instances are running. These are typically started using the zkServer.sh start and kafka-server-start.sh scripts.
  2. Create a Kafka Topic with Log Compaction: You need a topic configured with log compaction to demonstrate the tombstone. Create a topic with the following command:
bash
   kafka-topics.sh --create --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 --topic your_topic_name --config cleanup.policy=compact
  1. Produce a Tombstone Message: To produce a tombstone, you need to provide a key and a null value. Use the kafka-console-producer with the parse.key and key.separator options:
bash
   kafka-console-producer.sh --broker-list localhost:9092 --topic your_topic_name --property "parse.key=true" --property "key.separator=:"

You can then enter a message like someKey:null. The key here is someKey and the value is null, represented as a tombstone.

  1. Verify the Tombstone: To ensure that the tombstone has been added, you can consume messages from the topic using kafka-console-consumer:
bash
   kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic your_topic_name --from-beginning --property print.key=true --property key.separator="-"

This should show someKey-null among the output, indicating a message with someKey and a null value.

Why Use Tombstone Messages?

Tombstone messages are useful for scenarios involving deleting entries in a stateful application or cache that uses Kafka as a backend store. This is critical in event sourcing techniques where state changes are captured as a series of events.

Summary Table

FeatureDescription
Log CompactionCompacts old records in a topic based on keys.
Tombstone MessageA special Kafka message where the key has a non-null value and the value is null.
Use CaseSuitable for deleting entries in stateful applications or caches.
Kafka Tools for Tombstoneskafka-console-producer.sh and kafka-console-consumer.sh
Production of Tombstone MessageDone via console producers with key parsing and separators.
Verification of TombstoneConsumable with key printing and custom separators.

Conclusion

Using tombstone messages in Kafka is an efficient way to manage deletions in topics configured for log compaction. They are particularly useful in applications that require a consistent and compact view of the data, by ensuring that deletes are properly propagated and stored states are correctly maintained. Producing these messages from the Kafka console requires a good understanding of Kafka command-line tools and topic configuration parameters.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.